Java 向现有 Gradle 任务添加任务依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22054061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:16:47  来源:igfitidea点击:

Add task dependency to existing Gradle task

javagroovyintellij-ideagradle

提问by Giovanni Botta

I'm going to lose my mind about this. I have a build.gradlefile that looks something like:

我会失去理智的。我有一个build.gradle看起来像这样的文件:

apply plugin: 'idea'
task blah{
  // do something
}
idea{
  // some stuff
  dependsOn blah
}

and I'm getting this:

我得到了这个:

Could not find method dependsOn() for arguments [task ':blah'] on root project ...

I can't figure out what the right syntax is. Any help?

我无法弄清楚正确的语法是什么。有什么帮助吗?

采纳答案by evanwong

This should work:

这应该有效:

apply plugin: 'idea'
task blah{
  // do something
}
tasks.idea.dependsOn(blah)

回答by AppLend

Maybe my working example would be useful - fragments of build.gradle: (gradle version's 1.6)

也许我的工作示例会很有用 - build.gradle 的片段:(gradle 版本的 1.6)

ear {

    doFirst {
        tasks.buildWar.execute();
    }

    ...

}

task deployProj <<{
    tasks.ear.execute()
    tasks.copyEar.execute()
    tasks.copyJar.execute()
}

task buildWar(type: GradleBuild) {
    buildFile = 'mysubproject/build.gradle'
    tasks = ['war']
}

task copyEar(type: Copy) {
    from earPath
    into "$System.env.JBOSS_HOME" + deploymentPath
}

task copyJar(type: Copy) {
    from jarPath
    into libPath
}

copyEar.mustRunAfter 'ear'
copyJar.mustRunAfter 'ear'

回答by Ryan Shillington

I had a very similar error:

我有一个非常相似的错误:

Could not find method runEC2Step1DownloadSource() for arguments [{dependsOn=task ':docker:buildCopyEC2BuildFiles'}, task ':docker:runEC2SetupVariables', build_cwq7epb31twoanyxdhq1zosc7$_run_closure16@2a0fe301] on project ':docker' of type org.gradle.api.Project.

My problem was that my task runEC2Step1DownloadSourcewas defined like this:

我的问题是我的任务runEC2Step1DownloadSource是这样定义的:

task runEC2Step0CreateMachine(dependsOn: buildCopyEC2BuildFiles, runEC2SetupVariables) {
  doLast {
    // A bunch of stuff
  }
}

And the solution was to add an ARRAY to the list of dependsOn tasks:

解决方案是在dependsOn任务列表中添加一个ARRAY:

task runEC2Step0CreateMachine(dependsOn: [buildCopyEC2BuildFiles, runEC2SetupVariables]) {
  doLast {
    // A bunch of stuff
  }
}

Note the extra [] brackets around the list of dependsOn tasks.

请注意dependsOn 任务列表周围的额外[] 括号。