我在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')
}

最佳答案

您不能直接调用任务,但是还有许多其他方法可以对代码进行抽象。例如:

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任务,但这是不同的讨论。

09-20 07:17