如何在子项目任务结束时执行一次Gradle任务

时间:2013-04-23 10:07:17

标签: gradle multi-project

我正在使用以下项目结构

Project
|-Subproject1
|-Subproject2
|build.gradle
|settings.gradle

子模块包含在settings.gradle中,并在根项目的build.gradle中配置。

我有3个任务要做

  1. build(每个子项目都有此)
  2. deploy(这是每个子项目需要独立工作的打包机制)
  3. finalizeDeployment(这只需要调用一次)
  4. 我希望能够致电

    $gradle deploy< - 所有子项目都已部署,最终确定在最后被称为一次

    $gradle Subproject1:deploy< - 部署Subproject1并调用finalize

    的build.gradle

    configure(subprojects) {
    
        task build <<{
            println "Do Build "+ project.name
        }
    
        task deploy(dependsOn:build){
            println 'deploy '+project.name
    
            doLast{
                finalizeDeployment.execute()
            }
        }
    }
    
    task finalizeDeployment{
    
        dependsOn subprojects.deploy
    
        doLast{
            println 'Finalize Deployment'
        }
    }
    

2 个答案:

答案 0 :(得分:4)

它的工作方式如下

configure(subprojects) {

    task build << {
        println "Do Build " + project.name
    }

    task deployPrepare(dependsOn: build)<<{
            println 'deploy ' + project.name
    }

    task deployFinalize(dependsOn: deployPrepare)<<{
        parent.deployFinalize.execute()
    }
}

task deployFinalize {

    doLast {
        println 'Finalize Deployment'
    }
}

deployFinalize.dependsOn(subprojects.deployPrepare)

gradle subproject1:deployFinalize的控制台输出是:

Do Build subproject1
deploy subproject1
Finalize Deployment

gradle deployFinalize的控制台输出是:

Do Build subproject1
deploy subproject1
Do Build subproject2
deploy subproject2
Finalize Deployment

答案 1 :(得分:0)

看起来你可以使用相同的功能获得相同的功能,而无需使用parent.deployFinalize.execute()。

我工作的地方使用:

subprojects {

   task dist(type: Copy, dependsOn: assemble) {
       ...
   }
}

task dist
dist.dependsOn subprojects.dist

task finalizeDist(dependsOn : subprojects.dist) {
    ...
}
dist.dependsOn finalizeDist