I have a task which copies reports:
task copyReports(type: Copy) {
dependsOn = ['cleanTest', 'test -i']
from 'build/reports/tests/test/classes'
into 'reports'
println 'FINISHED COPYING FILES'
}
This task depends on cleanTest and test. I want to run test task with the parameter -i to see output of the tests. How to pass this parameter into dependsOn method? At the moment I get the next error:
Task with path 'test -i' not found in root project
I also tried without single quotes. Like this:
dependsOn = [cleanTest, test -i]
Then I get the next error:
Could not get unknown property 'i' for task ':copyReports' of type org.gradle.api.tasks.Copy.
Any ideas?
Related
The
build.gradle
task hello (type: Copy) {
doLast {
println "print from within"
}
println "print from outside"
}
when run
gradle -q hello
the doLast{} closure is not even touched. change doLast to doFirst, I get the same result:
D:\>gradle -q hello
print from outside
without the doLast or doFirst:
task hello (type: Copy) {
//doLast{
println "print from within"
//}
println "print from outside"
}
it works fine:
D:\>gradle -q hello
print from within
print from outside
This seems happening only with tasks of type Copy. can you please help clarifying? I am using gradle 2.4.
I thought it would be helpful for the people who come to this place on the same question. Though the above explanation is conceptually making sense. There is no proper solution mentioned in the answer. Here is the solution on how i achieved Copy task with doLast execution.
task hello () {
doLast{
copy {
println "print from within"
}
}
println "print from outside"
}
This would provide the execution of copy task along with doLast.
$ gradle -q hello
print from outside
print from within
Hope this is useful.
If you run the task without -q switch you'll notice that the task is already marked as UP-TO-DATE - which means that no actions (and you add an action via doLast) were executed.
Why is that? In this particular example, you have configured no inputs and outputs for the task. Copy tasks resolve it's being up-to-date base on the inputs and outputs and since none were configured Gradle assumes there's no need for it to be run.
I have a gradle task for copying, like below:
task hello << {
println "hello"
}
task myCopy(type: Copy) {
println "copy"
from(file('srcDir'))
into(buildDir)
}
but "myCopy" task gets executed even when I execute "hello" like below:
gradle hello
Now I understand this is the intended behavior. I read thru the entire Gradle Task page here: https://docs.gradle.org/current/userguide/more_about_tasks.html . But I want to make "myCopy" task only execute when explicitly executed. In other words, I want to make it so that "myCopy" does not execute when I execute "hello", and only execute when I run the command:
gradle myCopy
Is there a way to do this? Thanks
It's not getting executed, but getting configured. Take a closer look, nothing get copied if you don't run your copy task. Configuration is happenning always and for all the tasks you have, in your case you are printing "copy" during the configuration. Move it into the doLast section to print it at the execution phase, as:
task myCopy(type: Copy) {
doLast {
println 'Copy'
}
from(file('srcDir'))
into(buildDir)
}
Note, that doLast closure is the same as a task closure with << sign and it is executed only if task executed at the execution phase.
It was my understanding that printlns get displayed during a task's configuration phase (unless they are for example placed within a doLast action). Therefore given the following task:
ext {
changeset = 'placeholderChangeset'
}
task getChangeset {
process = '/usr/bin/git describe --always'.execute()
process.waitFor()
changeset = process.getText()
println "Changeset obtained from GIT is: $changeset"
doLast {
println '\n++++++++++++++++++++++++++++++++++'
println '+++++ task getChangeset executed'
println '++++++++++++++++++++++++++++++++++++'
}
}
If I run this using ./gradlew then the output will be:
********************************
** task getChangeset
********************************
Changeset obtained from GIT is: 9bd49ca
If it run it using ./gradlew getChangeset then the output will be:
********************************
** task getChangeset
********************************
Changeset obtained from GIT is: 9bd49ca
...
:app:getChangeset
++++++++++++++++++++++++++++++++++
+++++ task getChangeset executed
++++++++++++++++++++++++++++++++++++
In the first run, the task has not been executed. Yet the value of the changeset is displayed, and therefore the process = ... execute() line has been executed.
Therefore I'm confused, what does it actually mean for a task to be "executed", if in this example, it obviously doesn't mean the code statements within the task haven't executed, as they most clearly have in order to get the value of the changeset.
What's the difference between a task not executing and a task executing if its not having an effect on the execution or non execution of the code within the task?
A println isn't somehow special or different from other code. Any code that is not inside the doLast{} block will be executed in the configuration phase, and the dolast{} block will be executed in the execution phase.
I am trying to write an exec task in gradle that would use a variable set on the project.
Something like so:
task upload(type: Exec) {
executable "echo"
args version
}
This always gives me "unspecified"
If I do a task like this
task upload << {
println version
}
It will print the value of the version variable
How can I use the value of version inside an Exec task ?
Thanks for your help Mark Vieira
I found that writing my own task that calls the exec task works the way I need it to.
Like so
task upload << {
exec {
executable "echo"
args version
}
}
A)
task build << {
description = "Build task."
ant.echo('build')
}
B)
task build {
description = "Build task."
ant.echo('build')
}
I notice that with type B, the code within the task seems to be executed when typing gradle -t - ant echoes out 'build' even when just listing all the various available tasks. The description is also actually displayed with type B. However, with type A no code is executed when listing out the available tasks, and the description is not displayed when executing gradle -t. The docs don't seem to go into the difference between these two syntaxes (that I've found), only that you can define a task either way.
The first syntax defines a task, and provides some code to be executed when the task executes. The second syntax defines a task, and provides some code to be executed straight away to configure the task. For example:
task build << { println 'this executes when build task is executed' }
task build { println 'this executes when the build script is executed' }
In fact, the first syntax is equivalent to:
task build { doLast { println 'this executes when build task is executed' } }
So, in your example above, for syntax A the description does not show up in gradle -t because the code which sets the description is not executed until the task executed, which does not happen when you run gradle -t.
For syntax B the code that does the ant.echo() is run for every invocation of gradle, including gradle -t
To provide both an action to execute and a description for the task you can do either of:
task build(description: 'some description') << { some code }
task build { description = 'some description'; doLast { some code } }