Execute builds only for some gradle modules in project - gradle

I have a project utilities with a build.gradle. utilities has some modules named util, util2, util3, ...
In a task I want to execute first :util2:build and :util5:build. But I do not know how to write such a task. This fails:
task executePreBuild() {
:util2:build
:util5:build
}
In commandline
gradlew clean :util2:build :util5:build
can be executed. But this is not my purporse.
I want to execute
gradlew clean executePreBuild someOtherTask build

This can be done by using dependsOn:
task executePreBuild {
dependsOn ":util2:build"
dependsOn ":util5:build"
}

Related

How to run custom task after gradle assemble command

Actually I have these lines as part of my build.gradle file (using gradle 7.3.3)
processResources {
dependsOn "buildAngular"
}
task buildAngular(type: Exec) {
...
my task "buildAngular" runs automaticly on ./gradlew assemble its perfect and exactly what I want. But it runs on ./gradlew test too.
How I can make it run only on ./gradlew assemble?
As already mentioned in my comment on the question:
Since tests require a complete runtime classpath and since processResources produces parts of that classpath, it wouldn’t make sense to not run it when test is run.
If you only want to run buildAngular as part of assemble, then simply make the former a dependency of the latter:
assemble {
dependsOn 'buildAngular'
}

How to pass arguments to Gradle task in build.gradle

For example, I would like to exclude tests from build here:
task foo(dependsOn: ['clean', 'build']) {
build.mustRunAfter clean
}
Instead of build I need build -x test.
How can I pass -x test to build in Groovy?
Start parameters like -x cannot be defined for single tasks. They are always part of a specific Gradle invocation.
You may however create a task that invokes Gradle from inside Gradle:
task foo(type: GradleBuild) {
tasks = ['clean', 'build']
startParameter.excludedTaskNames = ['test']
}

skip a task in gradle, but only if the task exists

I have a complex multi sub-project gradle project with a little kotlin multiplatform inside and a fex custom gradle plugin.
My issue is , when I want to run the build by skipping some tasks(mostly test), not all the project have the same test task name : for some it's call jsTest, for other nodeJsTest, for other jvmTest.
So when I call gradle build -x jsTest -x nodeJsTest I have error because sometime some of the tasks to skip don't exist.
How can I skip the task, and ignore-it if it don't exist?
You can modify your Gradle file to do something like (Kotlin DSL):
tasks.named("build") {
dependsOn.removeIf { it.toString().contains("flakyTest") }
}
Otherwise you will need to aggregate your tasks to what you want specifically either by doing ./gradlew myTask anotherTask anotherOne andAnotherOne or create a task that dependsOn all the tasks you want.
Instead of making your test tasks to depend on build directly, you can create generic test task task testType in the middle- build triggers task testType and then it triggers jsTest or whatever relevant test task you create in that module.
Now you can safely run gradle build -x testType.
For example (in your .gradle file):
task jsTest { ... }
task testType { dependsOn jsTest }
build.finalizedBy(testType)
Do the same for the rest of your test tasks files, you can also create task testType globaly if you want the solution to be cleaner.

gradle copy file task not working in build

I am new to gradle, I want copy the jar file generated by gradlew build to another dir.
task myCopyTask(type: Copy) {
from "build/libs/gs.jar"
into "D:/bin/gs"
}
I add above task to the build.gradle which belong to gs module which will generate gs.jar.
The problem is the command gradlew build will not do the copy and this task indeed executed(I add println in myCopyTask). However, the command gradlew myCopyTask works.
First I thought maybe the copy task running too early, so I change it to
task myCopyTask(type: Copy) {
doLast {
from "build/libs/gs.jar"
into "D:/bin/gs"
}
}
This is not working even by gradlew myCopyTask. Only first version can work by command gradlew myCopyTask, the terminal will show: 1 actionable task: 1 executed
What is the problem?
You haven't wired the task into Gradle's DAG so currently it will only executed when you do gradlew myCopyTask
You'll probably do something like
apply plugin: 'base' // adds build and assemble lifecycle tasks
task myJarTask(type:Jar) {...}
task myCopyTask(type: Copy) {
dependsOn myJarTask
...
}
assemble.dependsOn myCopyTask
See https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies

Gradle tasks dependency

Q: Can one task be dependent on another task with specific argument? If the answer is yes then how to accomplish that?
I opened Gradle for myself recently, and just got curious about next:
Let's say I'd like to have one of the build's subfolders to be copied each time I execute the copy task.
task copy (type: Copy) {
dependsOn build
from '[fromName]'
into '[intoName]'
}
In my case, I'd like to copy folder build/reports/profile, but the thing is that this folder is created by execution of the build --profile task.
So, is there any chance to accomplish the following:
task copy (...) {
dependsOn build --profile
from ...
into ...
}
Btw, plugin project-report doesn't create this folder, to be dependent on its task.
Note: It's not as important to have it done, I'm just interested if it is possible at all.
You can't pass arguments with dependsOn to a task directly but you can create a custom Exec task and pass arguments via the command line interpreter (cmd) using the gradle wrapper like this:
task buildWithArgument(type: Exec) {
commandLine 'cmd', '/c', 'gradlew.bat build --profile'
}
task copy (type: Copy) {
dependsOn buildWithArgument
from '[fromName]'
into '[intoName]'
}

Resources