我创建了一个自定义herpderp
Gradle 任务:
task herpderp(type: HerpDerpTask)
class HerpDerpTask extends DefaultTask {
@TaskAction
def herpderp() {
println "Herp derp!"
}
}
有了这个,我可以将此任务添加到其他 Gradle 构建中,并在其他项目的构建调用中使用它:
gradle clean build herpderp
现在,我有以下多项目设置:
myapp/
myapp-client/
build.gradle
src/** (omitted for brevity)
myapp-shared/
build.gradle
src/** (omitted for brevity)
myapp-server
build.gradle
src/** (omitted for brevity)
build.gradle
settings.gradle
myapp/build.gradle
在哪里:
subprojects {
apply plugin: 'groovy'
sourceCompatibility = '1.7'
targetCompatibility = '1.7'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile (
'org.codehaus.groovy:groovy-all:2.3.7'
)
}
}
myapp/settings.gradle
在哪里:
include ':myapp-shared'
include ':myapp-client'
include ':myapp-server'
我希望能够导航到父myapp
目录,并运行gradler clean build herpderp
并仅在myapp-client
和myapp-shared
项目(而不是服务器项目)上运行herpderp
任务。
所以听起来我需要另一个自定义任务或某种类型的闭包/方法myapp/build.gradle
其中:
- 运行
clean build
; 然后 - 将(
cd
)滴入myapp-client
并运行herpderp
;然后 - 进入
myapp-shared
并运行herpderp
.
向任何文件添加什么才能从父构建命令调用herpderp
,但只能在客户端和共享子项目中执行?
以下一段代码可以做到这一点(应该放在myapp/build.gradle
):
allprojects.findAll { it.name in ['myapp-client', 'myapp-shared'] }. each { p ->
configure(p) {
task herpderp(type: HerpDerpTask)
}
}
class HerpDerpTask extends DefaultTask {
@TaskAction
def herpderp() {
println "Herp derp from ${project.name}!"
}
}
如 Gradle 文档中所述,您可以过滤子项目并按以下方式对其进行配置:
configure(subprojects.findAll { it.name.endsWith("server") }) {
apply plugin: 'com.google.cloud.tools.jib'
jib {
from {
image = 'openjdk:alpine'
}
}
...
}