When I run gradle test, it output as following:
:test
> Building 80% > :test > 86 tests completed
and it rewrite the line > Building 80% > :test > 86 tests completed as it's progressing forward.
What I want is to prevent gradle from replacing this line and make it output line by line, for example:
:test
:test > 86 tests completed
:test > 87 tests completed
:test > 88 tests completed
Is there any way to do this?
You can configure the test block
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
testCompile 'junit:junit:4.12'
}
test {
// show standard out and standard error of the test JVM(s) on the console, without this
// the text output printed by the tests won't show.
// ref: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html
testLogging.showStandardStreams = true
def testCount = 0
afterTest { descriptor, result ->
// descriptor is of type TestDescriptor, result is of type TestResult
// ref: https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestDescriptor.html
// ref: https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestResult.html
logger.lifecycle("Test {count: ${++testCount} name: $descriptor.name result: $result.resultType}")
}
}
Then you will see your output during the test task. Of course you could us another logging level depending on what you want but info, debug, error are also available. The lifecycle will always show, unless -q command line arg is passed.
$ ./gradlew clean test
Configuration on demand is an incubating feature.
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
Test {count: 1 name: testSomeLibraryMethod result: SUCCESS}
BUILD SUCCESSFUL
Total time: 1.229 secs
Related
apply plugin: 'java'
task taskA <<{
println 'task A'
}
task taskB (type:P){
namee='B'
}
task taskC (type:P){
namee='C'
}
#ParallelizableTask
class P extends DefaultTask {
String namee
#TaskAction
def broccoliBuild() {
println "Task "+namee
}
}
task taskBC (dependsOn:['taskB','taskC'])<<{
println 'BC'
}
taskBC.mustRunAfter taskA
task taskD <<{
println'task D'
}
taskD.mustRunAfter taskBC
task taskE (type:Qtask){
nname='E'
}
task taskF (type:Qtask){
nname='F'
}
#ParallelizableTask
class Qtask extends DefaultTask {
String nname
#TaskAction
def broccoliBuild() {
println "Task "+nname
}
}
task taskEF (dependsOn:['taskE','taskF'])<<{
println 'task EF'
}
taskEF.mustRunAfter taskD
task main(dependsOn: ['taskA','taskBC','taskD','taskEF'])<<{
println 'Build Completed'
}
When i execute the main task in parallel mode:
gradle main --parallel -Dorg.gradle.parallel.intra=true
taskEF executes along with taskBC (Both of them is parallel tasks) which violates the flow i have mentioned. taskD must be executed before taskEF (I have used mustRunAfter ) . Please can any one help me out of this. (Used Gradle 2.12)
After the contents of your build.gradle file and adding the wrapper task:
task wrapper(type: Wrapper) {
gradleVersion = '2.12'
}
I ran the following command: gradlew wrapper and then checked the version, which shows I am properly running the expected Gradle 2.12 version
E:\Data\Test>gradlew --version
------------------------------------------------------------
Gradle 2.12
------------------------------------------------------------
Build time: 2016-03-14 08:32:03 UTC
Build number: none
Revision: b29fbb64ad6b068cb3f05f7e40dc670472129bc0
Groovy: 2.4.4
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.8.0_111 (Oracle Corporation 25.111-b14)
OS: Windows 10 10.0 amd64
Then, running the command you provided, here is the results:
E:\Data\Test>gradlew main --parallel -Dorg.gradle.parallel.intra=true
Parallel execution is an incubating feature.
:taskA
task A
:taskB
:taskC
:taskE
:taskF
Task C
Task B
Task F
Task E
:taskBC
BC
:taskD
task D
:taskEF
task EF
:main
Build Completed
BUILD SUCCESSFUL
Total time: 2.609 secs
So, with Gradle 2.12 the output does run as you expect and the taskEF correctly runs after taskBC.
It appears that you are either not running the proper version of Gradle you intend or the version of the build.gradle you are actually running against is different than the one you provided here.
As a side note: I would suggest always run Gradle using the wrapper task as I have shown, which makes updating or choosing a specific Gradle version a snap.
Gradlew newb here, trying to figure out why when I invoke my task it's missing it's expected sources. I was under the impression that the tasks are executing in the correct order based on gradle explodeJars --info
$ gradle explodeJars --info
...
:copyJarsOver (Thread[main,5,main]) completed. Took 0.051 secs.
:explodeJars (Thread[main,5,main]) started.
:explodeJars
Skipping task ':explodeJars' as it has no source files.
:explodeJars UP-TO-DATE
:explodeJars (Thread[main,5,main]) completed. Took 0.0 secs.
The tasks in question:
task copyJarsOver(type: Copy, dependsOn: ["buildAll"]) {
from 'some/folder/cool.jar'
from 'another/folder/fancy.jar'
from 'best/folder/awesome.jar'
into 'build/modules'
rename { String fileName ->
fileName.replace('.jar', '.zip')
}
}
// Explodejars doesn't work when first ran, works second time <?>
task explodeJars(type: Copy, dependsOn: ["copyJarsOver"]) {
fileTree(dir: 'build/modules').each({ zipModule ->
def module = zipTree(zipModule)
from module
})
into 'build/explodedJars'
}
explodeJars.mustRunAfter copyJarsOver
Ok so I got confused about the life-cycle of the tasks...
Code inside the task gets executed at configuration time - at which point the files haven't been copied to the folder yet. If I wanted it ran at execution time I should've put it in a doLast.
Either way, from accepts a closure like: from { fileTree(...).collect { zipModule -> zipTree(zipModule) } }
I have the below gradle script from the Jhipster framework. It is used to run Gatling tests and due to the command line limit on windows I have tried using a manifest only jar approach. A detailed discussion regarding this can be found here Jhipster issue
But the below script seems to generate the Manifest jar properly but JavaExec is unable to find the main class, some pointer on this would be really helpfull. A console log of error is below as well
I have posted this on the gradle forum here, but no luck so far
apply plugin: 'scala'
sourceSets {
test {
scala {
srcDirs = ['src/test/gatling/simulations']
output.classesDir = 'target/test-classes'
}
}
}
task manifestJar(dependsOn:'compileTestScala',type: Jar) {
dependsOn configurations.runtime
classifier 'pathing'
doFirst {
manifest {
// uri is just needed for Windows-compatibility
attributes 'Class-Path': configurations.runtime.files.collect{ project.uri(it) }.join(' ')
}
}
}
task gatlingRun(dependsOn:'manifestJar', type: JavaExec) {
//dependsOn configurations.runtime
group = "gatling"
standardInput = System.in
final def sourceSet = sourceSets.test
File configFile = file('src/test/gatling/conf/gatling.conf')
def String gatlingDataFolder = "$project.rootDir.absolutePath/src/test/gatling/data"
def String gatlingReportsFolder = "$project.buildDir.absolutePath/reports/gatling"
def String gatlingBodiesFolder = "$project.rootDir.absolutePath/src/test/gatling/bodies"
def String gatlingSimulationsFolder = "$project.rootDir.absolutePath/src/test/gatling/simulations"
//classpath sourceSet.output + sourceSet.runtimeClasspath + files("src/test/gatling/conf")
classpath sourceSet.output + files(manifestJar.archivePath) + files("src/test/gatling/conf")
//classpath = files(pathingJar.archivePath)
main = "io.gatling.app.Gatling"
environment GATLING_HOME:''
args '-df', gatlingDataFolder
args '-rf', gatlingReportsFolder
args '-bdf', gatlingBodiesFolder
args "-sf", gatlingSimulationsFolder
args "-rd", ""
}
Console log:
:compileTestJava UP-TO-DATE
:compileTestJava (Thread[main,5,main]) completed. Took 0.973 secs.
:compileTestScala (Thread[main,5,main]) started.
:compileTestScala
Skipping task ':compileTestScala' as it is up-to-date (took 0.325 secs).
:compileTestScala UP-TO-DATE
:compileTestScala (Thread[main,5,main]) completed. Took 0.344 secs.
:manifestJar (Thread[main,5,main]) started.
:manifestJar
Skipping task ':manifestJar' as it is up-to-date (took 0.012 secs).
:manifestJar UP-TO-DATE
:manifestJar (Thread[main,5,main]) completed. Took 0.038 secs.
:processTestResources (Thread[main,5,main]) started.
:processTestResources
Skipping task ':processTestResources' as it is up-to-date (took 0.013 secs).
:processTestResources UP-TO-DATE
:processTestResources (Thread[main,5,main]) completed. Took 0.041 secs.
:testClasses (Thread[main,5,main]) started.
:testClasses
Skipping task ':testClasses' as it has no actions.
:testClasses UP-TO-DATE
:testClasses (Thread[main,5,main]) completed. Took 0.019 secs.
:gatlingRun (Thread[main,5,main]) started.
:gatlingRun
Executing task ':gatlingRun' (up-to-date check took 0.001 secs) due to:
Task has not declared any outputs.
Starting process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe''. Working directory: D:\Projects\jh5 Command: C:\Program Files\Java\jdk1.8.
0_45\bin\java.exe -Dfile.encoding=windows-1252 -Duser.country=SG -Duser.language=en -Duser.variant -cp D:\Projects\jh5\target\test-classes;D:\Projects
\jh5\build\resources\test;D:\Projects\jh5\build\libs\jhipster-pathing.jar;D:\Projects\jh5\src\test\gatling\conf io.gatling.app.Gatling -df D:\Projects
\jh5/src/test/gatling/data -rf D:\Projects\jh5\build/reports/gatling -bdf D:\Projects\jh5/src/test/gatling/bodies -sf D:\Projects\jh5/src/test/gatling
/simulations -rd
Successfully started process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe''
Error: Could not find or load main class io.gatling.app.Gatling
:gatlingRun FAILED
:gatlingRun (Thread[main,5,main]) completed. Took 0.166 secs.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':gatlingRun'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.
BUILD FAILED
Total time: 28.134 secs
Stopped 0 compiler daemon(s).
Edit:
Your manifest jar contains the wrong classpath. Try using testCompile files:
task manifestJar(dependsOn:'compileTestScala',type: Jar) {
dependsOn configurations.testCompile
archiveName 'gatlingBooter.jar'
doFirst {
manifest {
// uri is just needed for Windows-compatibility
attributes 'Class-Path': configurations.testCompile.files.collect{ project.uri(it) }.join(' ')
}
}
}
OLD:
If the manifest jar only works java -jar #Deepu maybe we should try the following:
task runJar(dependsOn:jar) << {
javaexec { main="-jar"; args jar.archivePath }
}
At least it looks good. Will try to test it during the weekend.
References:
http://blog.denevell.org/gradle-run-jar-file.html
https://issues.gradle.org/browse/GRADLE-1274
I want to automatically add a serverRun task when doing functional tests in Gradle, so I add a dependency :
funcTestTask.dependsOn(serverRun)
Which results in the task running whether or not the funcTestTask even runs
:compile
:serverRun
:funcTestTask (and associate compile tasks... etc)
:serverStop
OR
:compile UP-TO-DATE
:serverRun <-- unnecessary
:funcTestTask UP-TO-DATE
:serverStop
The cost of starting the server is pretty high and I only want it to start if the functionalTest isn't UP-TO-DATE, I'd like to do or something :
if(!funcTestTask.isUpToDate) {
funcTestTask.dependsOn(serverRun)
}
So I know I can't know the up-to-date status of funcTestTask until all it's inputs/outputs are decided BUT can I inherit it's uptoDate checker?
serverRun.outputs.upToDateWhen(funcTestTask.upToDate)
The alternative is to "doFirst" the ServerRun in the FuncTest, which I believe is generally frowned upon?
funcTestTask.doFirst { serverRun.execute() }
Is there a way to conditionally run a task before another?
UPDATE 1
Tried settings inputs/outputs the same
serverRun.inputs.files(funcTestTask.inputs.files)
serverRun.outputs.files(funcTestTask.outputs.files)
and this seems to rerun the server on recompiles (good), skips reruns after successful unchanged functional tests (also good), but wont rerun tests after a failed test like the following
:compile
:serverRun
:funcTestTask FAILED
then
:compile UP-TO-DATE
:serverRun UP-TO-DATE <-- wrong!
:funcTestTask FAILED
Having faced the same problem I found a very clean solution. In my case I want an eclipse project setup to be generated when the build is run, but only at the times when a new jar is generated. No project setup should be executed when the jar is up to date. Here is how one can accomplish that:
tasks.eclipse {
onlyIf {
!jar.state.upToDate
}
}
build {
dependsOn tasks.eclipse
}
Since the task is a dependent tasks of the one you are trying to control then you can try:
tasks {
onlyIf {
dependsOnTaskDidWork()
}
}
I ended up writing to a 'failure file' and making that an input on the serverRun task:
File serverTrigger = project.file("${buildDir}/trigger")
project.gradle.taskGraph.whenReady { TaskExecutionGraph taskGraph ->
// make the serverRun task have the same inputs/outputs + extra trigger
serverRun.inputs.files(funcTestTask.inputs.files, serverTrigger)
serverRun.outputs.files(funcTestTask.outputs.files)
}
project.gradle.taskGraph.afterTask { Task task, TaskState state ->
if (task.name == "funcTestTask" && state.failure) {
serverRun.trigger << new Date()
}
}
With information from an answer to my question on the Gradle forums :
http://forums.gradle.org/gradle/topics/how-can-i-start-a-server-conditionally-before-a-functionaltestrun
I had the same problem but the solution I came up is much simpler.
This starts up the server only if testing is necessary
test {
doFirst {
exec {
executable = 'docker/_ci/run.sh'
args = ['--start']
}
}
doLast {
exec {
executable = 'docker/_ci/run.sh'
args = ['--stop']
}
}
}
Assuming you have task1 and task2 which depends on task1 and you need to run task2 only if task1 is not up-to-date, the following example may be used:
task task1 {
// task1 definition
}
task task2(dependsOn: task1) {
onlyIf { task1.didWork }
}
In this case task2 will run only when task1 is not up-to-date. It's important to use didWork only for tasks which are defined in dependsOn, in order to ensure that didWork is evaluated after that task (task1 in our example) had chance to run.
I have a very simple build script like so
task hello{
println("hello World")
}
task bye {
println("bye")
}
On the command line I run
gradle hello and I get the following output:
hello World
bye
:hello UP-TO-DATE
Why is it executing the task "bye" (I'm assuming it gets executed since "bye" gets printed)? Thanks.
It's a common pitfall:
task hello {
println("Any code in here is about *configuring* the\
task. By default, all tasks always get configured.")
doLast {
println("Any code in here is about *executing* the task.\
This code only gets run if and when Gradle decides to execute the task.")
}
}
The distinction between configuration phase and execution phase is probably the single most important concept to understand in Gradle. It can be confusing at first, and may go away in the future. A kind of analogue in the Ant/Maven world is that these tools first parse XML build scripts and build an object model (perhaps resolving some properties along the way), and only then execute the build.
Adding to Peter answer, If you want to execute all task , you can specify the defaultTasks list.
defaultTasks 'clean', 'run'
task clean {
doLast {
println 'Default Cleaning!'
}
}
task run {
doLast {
println 'Default Running!'
}
}
task other {
doLast {
println "I'm not a default task!"
}
}
Output
Output of gradle -q
> gradle -q
Default Cleaning!
Default Running!
More details can be found here
https://docs.gradle.org/current/userguide/tutorial_using_tasks.html