3

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

3 回答 3

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

于 2012-12-04T16:41:58.830 回答
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()
    }
}
于 2016-01-26T09:04:44.420 回答
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")
于 2012-12-04T13:24:25.120 回答