Home > Mobile >  How to run certain task with gradle
How to run certain task with gradle

Time:03-18

I try to investigate a Gradle and follows some tutorials, but I have confused with the following:

I created are a couple of simple tasks:


task startProcess{
    println 'startProcess'
}

task doStep2{
    println 'Step2'
}

task doStep3{
    println 'Step3'
}

task finishProcess{
    println 'finishProcesss'
}

And try to execute one of them:

gradle finishProcess

Or with defaultTasks with command gradle build:

defaultTasks `finishProcess`

task startProcess{
    println 'startProcess'
}

task doStep2{
    println 'Step2'
}

task doStep3{
    println 'Step3'
}

task finishProcess{
    println 'finishProcesss'
}

In both options, I got the same result:


> Configure project :
startProcess
Step2
Step3
finishProcesss

BUILD SUCCESSFUL in 1s

How to execute exactly one of them?

CodePudding user response:

You have to use register, I think if you did not use it, You're only asking Gradle to execute these tasks.

for example

tasks.register('startProcess') {
    doLast {
        println 'startProcess'
    }
}


tasks.register('doStep2') {
    doLast {
        println 'Step2'
    }
}

tasks.register('doStep3') {
    doLast {
        println 'Step3'
    }
}

tasks.register('finishProcess') {
    doLast {
        println 'finishProcesss'
    }
}
tasks.named("build") { finalizedBy("finishProcess") }

Registering these tasks, you will be able to call each one indivadually.

If you want to link a specific task, with a build task for example.

Then you can use finalizedBy like the following.

tasks.named("build") { finalizedBy("finishProcess") }

This will call the finishProcess task, whenever build is triggered.

I strongly recommend the official gradle documintation for more information about tasks.

  • Related