I don't know if I'm not doing this right or if i have to handle builtin gradle tasks differently but i have a test task that i defined like this
task testNGTests(type: Test) {
useTestNG()
}
and am trying to use it in a doFirst closure like this
task taskA {
doFirst {
testNGTests.execute()
}
}
but it does not work for some reason, i have also tried
testNGTests.executeTests()
but that did not work either. Is there a special way that I have to handle the built in test task?
I am using gradle version 0.9.2
doFirst(action) Adds the given Action to the beginning of this task's action list. doLast(action) Adds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed.
Gradle has different phases, when it comes to working with the tasks. First of all, there is a configuration phase, where the code, which is specified directly in a task's closure, is executed. The configuration block is executed for every available task and not only, for those tasks, which are later actually executed.
Executing a task from another task isn't (and never was) officially supported. Try to use task dependencies instead, e.g. taskA.dependsOn(testNGTests)
.
I found a workaround to do this. In my scenario I have a task that reads an user input and depending on his anwser I need to create a EAR with different configurations. I used a task of type GradleBuild. Here is the code:
task createEar() << {
def wichTypeOfEar = System.console().readLine("Which EAR?? (a, b)\n")
if(wichTypeOfEar == 'a'){
tasks.earA.execute()
}else{
tasks.earB.execute()
}
}
task earA(type: GradleBuild) {
doFirst{
// Here I can do some stuffs related to ear A
}
tasks = ['ear']
}
task earB(type: GradleBuild) {
doFirst{
// Here I can do some stuffs related to ear B
}
tasks = ['ear']
}
ear {
//Here is the common built in EAR task from 'ear' plugin
}
In you case you could do the following:
task testNGTests(type: Test) {
useTestNG()
}
task testNGTestsWrapper(type: GradleBuild){
tasks = ['testNGTests']
}
task taskA {
doFirst {
testNGTestsWrapper.execute()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With