How to pass arguments to Gradle task in build.gradle - 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']
}

Related

How to get the exact copy of original Run task in Gradle?

Does anybody know what the simplest way to register several run tasks that represent exact copy of original one with changed app’s arguments ? Or may be how to supply run task with an optional argument that would represent app’s arguments.
Basically, I want my build to contain some pre-defined options how to run an application and don’t want to declare new JavaExec that requires its manual configuring while I have already had ready-to-go run task supplied by default.
gradle run --args='--mode=middle' ---> gradle run || gradle runDefault || gradle run default
gradle run --args='--mode=greed' ---> gradle runGreed || gradle run greed
gradle run --args='--mode=lavish' ---> gradle runLavish || gradle run lavish
As for now I came up only with the option that suggests implementing my own JavaExec_Custom task class with supplied arguments property. And it seems to be too complex and boilerplating as for my goal.
You can create tasks that modify the configuration of the actual run task in a doFirst or doLast closure:
// Groovy-DSL
task runGreed {
doFirst {
run.args '--mode=greed'
}
finalizedBy run
}
// Kotlin-DSL
register("runGreed") {
doFirst {
run.get().args = listOf("--mode=greed")
}
finalizedBy(run)
}
You can use a property for the args
task run(type: JavaExec) {
args property('run.args').split(' ')
...
}
Usage
gradle run -Prun.args='foo bar baz'

Gradle - Skip test in custom task

When running a gradle build from the command I can skip tests like so:
./gradlew build -x test
I have a custom task that cleans, builds and releases to Maven local defined like so:
task releaseLocal(type: GradleBuild) {
tasks = ['clean', 'build', 'publishToMavenLocal']
}
When I call this, neither of the following tasks skip testing:
./gradlew releaseLocal
./gradlew releaseLocal -x test
What can I add to the task to skip testing?
Tasks of type GradleBuild provide a property called startParameter. This property is of type StartParameter and can be used to pass configuration otherwise passed via command line parameters. Task names passed using the -x option are stored in the excludedTaskNames property, so you may use the following code to exclude the task named test from your build:
task releaseLocal(type: GradleBuild) {
tasks = ['clean', 'build', 'publishToMavenLocal']
startParameter.excludedTaskNames = ['test']
}
However, this will exclude the task test for every invocation of releaseLocal, so you may try to use the following code to pass command line parameters from your current build:
task releaseLocal(type: GradleBuild) {
startParameter = gradle.startParameter.newInstance()
tasks = ['clean', 'build', 'publishToMavenLocal']
}
This should copy the command line parameters from your current build, so now you should be able to skip any task by calling gradle releaseLocal -x <task>.
Please note, that you cannot change the order of the two configuration statements in the second example. Internally, the property tasks of GradleBuild will be applied to its startParameter property, so you must define the tasks after overwriting the startParameter property.
Try these two commands:
gradle -q releaseLocal
gradle -q releaseLocal -x 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.

Passing conflicting parameters in gradle - which one will be used?

Let's say we are trying to run gradle task with some additional parameters.
For example:
./gradlew -Pfeature=true -Pfeature=false
Which one of those properties will be passed to build?
I made an experiment using simple gradle task
task printProps {
doLast {
println props
}
}
I've run it twice:
./gradlew printProps -Pprops=false -Pprops=true -> true
./gradlew printProps -Pprops=true -Pprops=false -> false
So gradle uses the last parameter passed.

Skip a task when running another task

I added a task to my gradle project:
task deploy() {
dependsOn "build"
// excludeTask "test" <-- something like this
doFirst {
// ...
}
}
Now the build task always runs before the deploy task. This is fine because the build task has many steps included. Now I want to explicitly disable one of these included tasks.
Usually I disable it from command line with
gradle deploy -x test
How can I exclude the test task programmatically?
You need to configure tasks graph rather than configure the deploy task itself. Here's the piece of code you need:
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(deploy)) {
test.enabled = false
}
}
WARNING: this will skip the actions defined by the test task, it will NOT skip tasks that test depends on. Thus this is not the same behavior as passing -x test on the command line
I don't know what your deploy task does, but it probably just shouldn't depend on the 'build' task. The 'build' task is a very coarse grained lifecycle task that includes tons of stuff you probably don't want.
Instead it should correctly define its inputs (probably the artifacts that you wanna deploy) and then Gradle will only run the necessary tasks to build those inputs. Then you no longer need any excludes.
I ran into a similar problem. Here is how I prevent "test" from running when I run "intTest" and want the ITs to run alone:
test {
onlyIf { !gradle.startParameter.taskNames.contains("intTest") }
}
An alternative that doesn't depend on certain tasks being run explicitly:
test {
onlyIf { !gradle.taskGraph.hasTask(":intTest") || gradle.taskGraph.hasTask(":check") }
}

Resources