Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you execute a built in gradle task in a doFirst/doLast closure?

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

like image 980
AgentRegEdit Avatar asked Jan 23 '12 22:01

AgentRegEdit


People also ask

What does doLast mean in Gradle?

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.

How does Gradle task work?

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.


2 Answers

Executing a task from another task isn't (and never was) officially supported. Try to use task dependencies instead, e.g. taskA.dependsOn(testNGTests).

like image 111
Peter Niederwieser Avatar answered Oct 03 '22 22:10

Peter Niederwieser


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()
    }
}
like image 21
carlosgarbiatti Avatar answered Oct 03 '22 23:10

carlosgarbiatti