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

前端 未结 2 701
失恋的感觉
失恋的感觉 2021-01-17 11:25

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 testN         


        
相关标签:
2条回答
  • 2021-01-17 11:31

    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()
        }
    }
    
    0 讨论(0)
  • 2021-01-17 11:53

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

    0 讨论(0)
提交回复
热议问题