build.gradle is:
task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
from "src"
into "dest"
println "copy"
}
task hello {
println "hello"
}
And when I try with ./gradlew copy, both copy and hello are executed. Output like below:
> Configure project :
copy
hello
How can I only execute copy?
In your example, only the copy task is executed. However, both are configured.
A Gradle build has three distinct phases in the build lifecycles:
An initialization phase
A configuration phase
An execution phase
By default, Gradle configures all tasks at start-up, though many types of configurations can be deferred (lazy configuration).
The println statement you have in the hello task is part of the configuration and this is why you see it no matter what task you intend to execute. You can also see in the output that is under the > Configure project : header.
If you want to only print "hello" when it actually executes, move it into a doLast block like this:
task hello {
doLast {
println "hello"
}
}
Related
I have two task
task Hello {
println 'Hello'
}
task World {
println 'Hello1'
}
If I run World task Hellowill run also. If I modify my tasks in this way
task Hello {
doLast {
println 'Hello'
}
}
task World {
println 'Hello1'
}
then task Hellowon't run. How do doLast{} or doFirst{} sections affect running tasks in gradle?
I can't find information in gradle docs about that. Thx.
The task Hello doesn't run. It's configured.
The code inside the curly braces is the code that configures the task. This code is always executed, whatever the task you tell gradle to run. It must run so that gradle knows what the task does, on which other task it depends, which other task it finalizes, etc.
Once the configuration phase has finished, the execution phase starts. And in that phase, the task that you asked to execute is executed/ In that phase, the code passed to doLast is being executed.
Here's the documentation.
I am trying to figure out, how to set variables in build.gradle and access that varible in init.gradle script.
Please find below sample build script :-
gradle.allprojects{
task CommonTasks{
println "CommonTasks - loaded"
}
CommonTasks.doFirst{
println "CommonTasks - doFirst : here i want to use variable, that is declared in project build"
}
CommonTasks.doLast{
println "CommonTasks - doLast"
}
}
Also find below sample project build :-
apply plugin : 'java'
//some-where here i want to pass a variable in CommonTasks
clean.dependsOn CommonTasks
Please suggest, how to access project build.gradle variables(say, String or File type for example) on init.gradle page.
Largely, you just need to be careful about which phases all of the code runs in. Gradle has 3 phases when you run a gradle <some task> command.
Initialization - processes init.gradle scripts, settings.gradle scripts, and generally just gets the project structure defined
Configuration - processes build.gradle scripts for all projects
Execution - executes the requested tasks
Importantly you can delay execution of code in your init.gradle script into later phases. I included print statements in the example below to help illustrate when this code executes.
One option for what you want is to declare the property in the init phase on the task or project (my example shows it on the task). This way you are sure that the property is defined by the time you reference it in your doFirst. Your build.gradle just needs to set that variable at configuration time.
init.gradle
println 'init.gradle: In initialization phase'
gradle.allprojects {
task CommonTasks {
println 'init.gradle: In configuration phase'
ext.myVar = null
println "Value of myVar=${myVar}"
doFirst {
println 'init.gradle: In execution phase - doFirst'
println "Value of myVar=${myVar}"
}
doLast {
println 'init.gradle: In execution phase - doLast'
}
}
}
build.gradle
apply plugin: 'java'
// set the value
CommonTasks.myVar = 'buildValue'
clean.dependsOn CommonTasks
This should print out myVar=buildValue upon execution.
I'm very new to gradle and have a basic question.
When I add a custom task to my gradle.build file and call "gradlw build" or "gradle clean" or any other gradle command,
it automatically runs my custom task.
Is that how things work in gradle? run every task in the build file?
Is there way to run a task only when I want it manually?
task foo {
println 'hello'
}
That creates a task, and during the configuration of the task, it tells gradle to execute println 'hello'. Every task is configured at each build, because gradle needs to know what its configuration is to know if the task must be executed or not.
task foo << {
println 'hello'
}
That creates a task, and during the execution of the task, it tells gradle to execute println 'hello'. So the code will only be executed if you explicitly chose to run the task foo, or a task that depends on foo.
It's equivalent to
task foo {
doLast {
println 'hello'
}
}
You chose not to post your code, probably assuming that gradle was acting bizarrely, and that your code had nothing to do with the problem. So this is just a guess, but you probably used the first incorrect code rather than the second, correct one.
I've two gradle files :
build.gradle
apply from :'other.gradle'
task hiHelloWrapper(type: GradleBuild) {
buildFile = 'other.gradle'
tasks = ['hi','hello']
}
other.gradle
task hello<<{
println 'hello from other'
}
task hi<<{
println 'hi from other'
}
Now, when I execute
>gradle hiHelloWrapper
it results in both tasks hi and hello being executed and they are executed in their relative order in tasks list .
Is there any way that I can execute selectively one of them without creating another task which includes only one of them?
As far as I understood you just need to run:
gradle hi
or
gradle hello
All tasks from other.gradle are imported to build gradle and can be used out of the box.
I have multiple gradle files to run test suites. Each gradle file has multiple tasks with dependencies defined. And there will be a task with same name as file name to run all those tasks.
Example
foo_test.gradle
task testBlockA << {
// do some tests here
}
task testBlockB(dependsOn: testBlockC) << {
// do some tests here
}
task testBlockC << {
// do some stuff here
}
task foo_test(dependsOn: testBlockA, testBlockB)
Now I want to write a common test.gradle file which, based on argument provided, loads the given test gradle file and runs the task
gradle -b test.gradle -Ptest_to_run=foo_test
How to I create a task in test.gradle, which will run foo_test task of foo_test.gradle, along with its dependencies (testBlockA-C)
As I read tasks['foo_test'].execute() will not work as it does not execute the dependsOn tasks.
In your main build.gradle file, you can read the provided -P attribute in your build.gradle file:
if(project.hasProperty("test_to_run")){
apply from:test_to_run
defaultTasks test_to_run
}
this snippet applies a buildscript based on the input property and also declares a defaultTask to run.