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

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.

Related

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']
}

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.

Why gradle clean task starts all other non-default tasks?

I have gradle set up and running. My build.gradle has 2 tasks defined inside:
task setVersion() {
println('setVersion')
//...
}
task setIntegrationEnv() {
println('setIntegrationEnv')
//...
}
When I run
./gradlew clean
gradle runs both tasks setVersion and setIntegrationEnv and then it runs clean for all my modules (app, cloud_module) in that project, output:
Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
setVersion
setIntegrationEnv
:cloud_module:clean
:app:clean
BUILD SUCCESSFUL
Total time: 14.18 secs
Why this happens, where this behavior is defined?
Could You please provide full build.gradle script? I'd be much easier to help You. You've probably mistaken gradle build phase with configuration phase - it's a common topic here.
General rule is that code You'd like to be run at build phase should be added as an action:
task someTask << {
println 'runtime'
}
while code You'd like to run at configuration phase should be added in task body:
task someTask {
println 'configuration
}
or all together:
task someTask {
println 'configuration'
doLast {
println 'runtime'
}
}
Additional info can be found here, here and here.

Resources