Gradle 按顺序执行子项目的分级任务

Gradle 按顺序执行子项目的分级任务,gradle,Gradle,我们的项目结构如下: ParentProject projA projB projC projD ... 我们在父项目的这些项目上运行了一系列任务,调用gradle clean build make tag push remove clean: removes the build directory for each project build: compiles the source code make: uses the compiled code

我们的项目结构如下:

ParentProject
    projA
    projB
    projC
    projD
    ...
我们在父项目的这些项目上运行了一系列任务,调用
gradle clean build make tag push remove

clean: removes the build directory for each project build: compiles the source code make: uses the compiled code to create a docker image (custom task) tag: tags the docker image (custom task) push: pushes the tagged docker image to our nexus repo (custom task) remove: removes the local docker image (custom task)
project('projB') {
  task clean(dependsOn: ':projA:remove') {
    doLast {
        println 'clean'
    }
 }
}
经过这样的过程,所有的东西都试图在任何东西被移除之前建立起来。我们希望运行gradle的方式如下:

projA:clean
projA:build
projA:make
projA:tag
projA:push
projA:remove
projB:clean
projB:build
projB:make
projB:tag
projB:push
projB:remove
...
这样,在下一个项目开始构建之前,项目会自行清理并释放磁盘空间

有没有办法做到这一点


编辑:有时并非所有任务都需要运行。图像可能已经生成,只需重新标记即可。或者可能需要为本地测试而构建,但没有标记或推送。

看起来像任务
生成
标记
推送
删除
都是自定义任务

如果您希望任务之间存在依赖关系,例如每当启动taskY时都应执行taskX,则应使用task的
dependsOn
属性。它也称为任务依赖关系

下面显示了父项目上的
build.gradle
,该父项目使projA的任务依赖于projA的
clean
任务。对于projA的其余任务,您应该这样做

project('projA') {
 task make(dependsOn: ':projA:build') {
    doLast {
        println 'make'
    }
 }
}

project('projA') {
task build {
    doLast {
        println 'build'
    }
  }
}
。。。

然后,您应该继续执行剩余的项目和剩余的任务。例如,
ProjB:clean
应该依赖于
projA:remove

clean: removes the build directory for each project build: compiles the source code make: uses the compiled code to create a docker image (custom task) tag: tags the docker image (custom task) push: pushes the tagged docker image to our nexus repo (custom task) remove: removes the local docker image (custom task)
project('projB') {
  task clean(dependsOn: ':projA:remove') {
    doLast {
        println 'clean'
    }
 }
}
继续执行项目B和其余项目中的其他任务


有关更多详细信息,请遵循此操作。

我们发现可以创建“GradleBuild”类型的任务,在该任务中,我们可以指定要在单个任务中运行的任务

task deploy(type: GradleBuild){
  tasks = ['clean','build','make','tag','push','remove']
}
此解决方案防止了任务与依赖项的链接,因此我们仍然可以单独构建子项目,而不必担心其他任务被不必要地运行