How do you execute a built in gradle task in a doF

2019-06-15 14:15发布

问题:

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

回答1:

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



回答2:

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()
    }
}