我在build.gradle中有这个:
task cleanCommon(type: GradleBuild) {
buildFile = 'common/build.gradle'
tasks = ['clean']
}
task cleanCrawler(type: GradleBuild) {
buildFile = 'crawler/build.gradle'
tasks = ['clean']
}
task cleanPortlet(type: GradleBuild) {
buildFile = 'portlet/build.gradle'
tasks = ['clean']
}
task cleanAll(dependsOn: ['cleanCommon', 'cleanCrawler', 'cleanPortlet']) {
}
这太冗长了。有什么方法可以做下面的伪代码吗?
taskCleanAll {
GradleBuild.pleaseRunTask('common/build.gradle', 'clean')
GradleBuild.pleaseRunTask('crawler/build.gradle', 'clean')
GradleBuild.pleaseRunTask('portlet/build.gradle', 'clean')
}
答案 0 :(得分:7)
您不能直接调用任务,但还有很多其他方法可以抽象代码。例如:
def createBuildTask(name, buildFile) {
task "$name"(type: GradleBuild) {
buildFile = buildFile
tasks = ['clean']
}
}
createBuildTask("cleanCommon", "common/build.gradle")
createBuildTask("cleanCrawler", "crawler/build.gradle")
createBuildTask("cleanPortlet", "portlet/build.gradle")
我想知道为什么你首先使用GradleBuild
任务,但这是一个不同的讨论。
答案 1 :(得分:1)
def List buildFileList = ['common/build.gradle', 'crawler/build.gradle', 'portlet/build.gradle']
task cleanAll << {
buildFileList.each() {
def tempTask = tasks.create(name: "execute_$it", type: GradleBuild)
tempTask.buildFile="$it"
tempTask.tasks = ['clean']
tempTask.execute()
}
}
答案 2 :(得分:0)
这样的东西可以起作用......注意这个没有经过测试。但为什么你认为它是如此冗长?我觉得它很干净。
task cleanAll << {
StartParameter startParameter = project.gradle.startParameter.newBuild()
startParameter.tasks = ['clean']
runGradle('portlet/build.gradle', startParameter)
runGradle('crawler/build.gradle', startParameter)
runGradle('common/build.gradle', startParameter)
}
def runGradle(buildFile, params) {
params.buildFile = buildFile
GradleLauncher.newInstance(params).run().rethrowFailure();
}
另一种选择是提取任务创建:
task cleanAll
def createCleanTask(name) {
task "${name}Clean"(type: GradleBuild) {
tasks = ['clean']
buildFile = "${name}/build.gradle"
}
cleanAll.dependsOn("${name}Clean")
}
createCleanTask("crawler")
createCleanTask("portlet")
createCleanTask("common")