Is it possible to add in the java command to the standardOutput stream on a JavaExec command in Gradle?
Ie
task importSitesDef(dependsOn: init, type: JavaExec) {
main = 'com.x'
classpath = configurations.runE
standardOutput = new FileOutputStream(standardLog, true)
}
Will log the output but I want to see
java com.x -cp ... in the file too before the input.
This is due to using the same output stream/file for multiple tasks and its very hard to tell where the output from one task finished before another one starts.
I know this is an old question but all I did to get the Java command used was to get the output of the JavaExec commandLine method and join it's elements, like so:-
commandLine.collect().join(' ')
Then output it.
Related
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?
I need to transfer file names from Android application variants to Maven publication artifacts. The construct in my build.gradle is:
publishing {
publications {
maven(MavenPublication) {
groupId android.defaultConfig.applicationId
artifactId 'apk'
version = project.ext.version
artifacts = {
def list = []
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
list.add (output.outputFile.absolutePath)
println "Output " + output.outputFile.absolutePath + " list " + list
}
}
println "To deploy " + list
list.iterator()
}
}
}
}
No artifacts are ever deployed with this script, and it produces the output that looks like a raise of machines:
To deploy []
Output B:\ox\app\build\outputs\apk\app-debug-1.0.apk list [B:\ox\app\build\outputs\apk\app-debug-1.0.apk]
Output B:\ox\app\build\outputs\apk\app-release-1.0.apk list [B:\ox\app\build\outputs\apk\app-debug-1.0.apk, B:\ox\app\build\outputs\apk\app-release-1.0.apk]
So all files were nicely iterated through, but for some reason the last print statement runs before the terminal print statement, and all construct just returns the empty list.
Why does this happen, and how to fix this? I need to set the list of iterated files and the artifacts property for Maven publication.
Gradle is a declarative language, so order of statements is not important. The builds are executed in following way:
1) script is evaluated in order it's written. That's how Gradle find out about what tasks you have in your script and which plugins to load. Not that tasks themselves are not executed at that point.
2) Then Gradle constructs a DAG of all the tasks that need to be executed. Each task can declare dependencies which are the tasks that must be executed beforehand.
3) After DAG is constructed, the tasks are run. The tasks are run according to their declared dependencies. Tasks with no dependencies are run first, possibly in parallel. The tasks that had dependencies are scheduled to run after all their predecessors have finished. Again if possible Gradle will run things in parallel.
3a) Tasks themseleves are run in following order. First doFirst {} blocks are executed, then the task body and after that the doLast {} blocks.
So in your case if you want to compose the list of artifacts, you should enclose your iterator into doFirst {} block.
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.
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
}
}
In my build.gradle script, I have a lot of tasks, each depending on zero or more other tasks.
There are three 'main' tasks which can be called: moduleInstallation, backupFiles and restoreFiles.
Here's the question: I would like to be able to tell Gradle which tasks to execute and which don't need to execute. For example, when calling moduleInstallation, I want all depending tasks to execute (regardless of their UP-TO-DATE flag), but not the restore tasks. I've tried altering the phase in which the tasks get executed (e.g. config phase, execution phase,...) and a couple of other things, but all tasks just keep getting executed.
A solution I've thought of was just stating in the main tasks that, when this main task is called (f.e. moduleInstallation), we set the UP-TO-DATE flag of all non-related tasks to false, so they don't get executed. Is that possible?
EDIT: Here's an example:
When moduleInstallation is called (which depends on backupFiles), restoreFiles (which depends on restoreFromDate) is executed too.
First main action
task moduleInstallation << {
println "Hello from moduleInstallation"
}
task backupFiles {
doLast {
println "Hello from backupFiles"
}
}
Second main action
task restoreFiles {
println "Hello from restoreFiles"
}
task restoreFromDate {
println "Hello from restoreFromDate"
}
Dependencies:
moduleInstallation.dependsOn backupFiles
restoreFiles.dependsOn restoreFromDate
So when I type gradle moduleInstallation in the terminal, I get the following output:
Hello from restoreFromDate
Hello from restoreFiles
Hello from backupFiles
Hello from moduleInstallation
The second snippet has to use doLast (or its << shortcut) like the first snippet. Otherwise, the code is configuration code and will always be evaluated, no matter which tasks are eventually going to be executed. In other words, it's not the restoreFiles and restoreFromDate tasks that are being executed here (as one can tell from the bits of command line output that you don't show), but (only) their configuration code.
To better understand what's going on here (which is crucial for understanding Gradle), I recommend to study the Build Lifecycle chapter in the Gradle User Guide.