Why gradle clean task starts all other non-default

2019-07-18 11:42发布

问题:

I have gradle set up and running. My build.gradle has 2 tasks defined inside:

task setVersion() {
    println('setVersion')
    //...
}

task setIntegrationEnv() {
    println('setIntegrationEnv')
    //...
}

When I run

./gradlew clean

gradle runs both tasks setVersion and setIntegrationEnv and then it runs clean for all my modules (app, cloud_module) in that project, output:

Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
setVersion
setIntegrationEnv
:cloud_module:clean
:app:clean

BUILD SUCCESSFUL

Total time: 14.18 secs

Why this happens, where this behavior is defined?

回答1:

Could You please provide full build.gradle script? I'd be much easier to help You. You've probably mistaken gradle build phase with configuration phase - it's a common topic here.

General rule is that code You'd like to be run at build phase should be added as an action:

task someTask << {
   println 'runtime'
}

while code You'd like to run at configuration phase should be added in task body:

task someTask  {
   println 'configuration
}

or all together:

task someTask {
   println 'configuration'

   doLast {
      println 'runtime'
   }
}

Additional info can be found here, here and here.