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
Related
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']
}
I'm running my tests with gradle using the command line:
./gradlew testManager:uiTest -PignoreTestFailures=true" +
"-DCHROMEDRIVER_VERSION=${env.CHROMEDRIVER_VERSION}" +
"-DBASE_URL=${params.BASE_URL}"
I need to propagate passed properties (e.g. BASE_URL) to the JMV with tests from the gradle task.
I know I could do the following inside the task:
systemProperties System.properties
But I'd like to avoid passing the whole set, as it overrides some other required values in tests.
So the question is: is there a way to get the only properties passed via -D command line parameter, inside the gradle task?
Found this pretty easy way:
task uiTest(type: Test) {
doFirst {
/* Propagate only command line start properties (-D) to the tests */
project.gradle.startParameter.systemPropertiesArgs.entrySet().collect() {
systemProperty it.key, it.value
}
}
}
So in a case of
./gradlew testManager:uiTest -PincludeTests=saveReleaseState/** -DBASE_URL=foobar
The tests receive only BASE_URL=foobar
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'
For fust build project I use such command
gradle clean build -x checkstyleMain -x checkstyleTest -x findbugsMain -x findbugsTest -x test
How I can create short task for this?
Something like this
task short {
clean
// build-x checkstyleMain -x checkstyleTest -x findbugsMain -x findbugsTest -x test
}
I have error with -x
UPDATE
I add such
gradle.taskGraph.whenReady {
if (gradle.taskGraph.hasTask(":fastRun")) {
checkstyleMain.enabled = false
checkstyleTest.enabled = false
findbugsMain = fasle
findbugsTest = false
test = false
}
}
task fastRun {
// clean
// build
}
And run
gradle clean build fastRun
But all tasks run =(
Gradle is not lifecycle based the way Maven is. Instead of asking for a task that includes all these other tasks you do not want to do, you are better off finding a task that does what you want without including all these others.
For example, assuming you are using the java plugin:
assemble: will create all archives in the project, but not run any tests or checks
compileTestJava: will compile all main and test Java classes but will not run tests or create binaries. Unless their creation is required by a different project in a multi-project build.
???: some task that maybe does exactly what you want
And if point 3 has no answer for you, you can define a new task that will depend only on what you want to achieve and not the rest.
See the Java plugin documentation for an exhaustive list of the tasks added, including the high level ones.
Unfortunately, usual ways of skipping tasks won't work in your case just out of the box.
But you can use a TaskGraph to check whether your custom task will be executed and if it'll be, disable all the tasks you don't want to be executed. For that, you need to add such a configuration snippet:
gradle.taskGraph.whenReady {
if (gradle.taskGraph.hasTask(":short")) {
checkstyleMain.enabled = false
checkstyleTest.enabled = false
// any other task you want to skip...
}
}
This snippet should be placed into the root of the build skript. Just note, that task names could differ depending on the project structure you have.
It's waiting until the task graph is ready and if it has a task named short (that means, that this task will be executed), then it disables some other tasks.
You can add the following codes to skip the tasks,
gradle.startParameter.excludedTaskNames += "testClasses"
gradle.startParameter.excludedTaskNames += "test"
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.