我有两个子项目subproject1和subproject2。我想将一些类从subproject2添加到subproject1并获得subproject1.jar。以下是我的gradle文件:

task copyClasses (dependsOn: [  ':subproject1:clean',  ':subproject1:classes']) {
    println "copyClasses "
    doLast {
        Task study = tasks.getByPath(':subproject1:jar')
        study.doFirst {
            copy {
                println "copy ... "
                println sourceSets.main.output.classesDir
                println project(':subproject1').sourceSets.main.output.classesDir
                from sourceSets.main.output.classesDir
                into project(':subproject1').sourceSets.main.output.classesDir
            }
        }
    }
}

task jarUpdated (dependsOn: [ clean, classes, copyClasses, ':subproject1:jar']) {
    doLast {
        println "jarUpdated"
    }
}

但是我得到了如下的构建顺序:
$ gradle jarUpdated
copyClasses
:subproject1:compileJava
:subproject1:processResources UP-TO-DATE
:subproject1:classes
:subproject1:jar
:subproject2:compileJava
:subproject2:processResources UP-TO-DATE
:subproject2:classes
:subproject2:clean
:subproject1:clean
:subproject2:copyClasses
Calling Task.doFirst(Closure) after task execution has started has been deprecated and is scheduled to be removed in Gradle 2.0. Check the configuration of task ':subproject1:jar'.
:subproject2:jarUpdated
jarUpdated

BUILD SUCCESSFUL

我的期望是:
$ gradle jarUpdated
:subproject2:clean
:subproject2:compileJava
:subproject2:processResources UP-TO-DATE
:subproject2:classes
:subproject1:clean
:subproject1:compileJava
:subproject1:processResources UP-TO-DATE
:subproject2:copyClasses
copyClasses
copy ...
:subproject1:jar
:subproject2:jarUpdated
jarUpdated

BUILD SUCCESSFUL

您能建议还是指出我错过了什么?非常感谢!

最佳答案

完全按照您的要求进行操作的“最简单”方法可能是在subproject1构建文件中进行如下操作。

jar {
  from  tasks.getByPath(':subproject2:compileJava')
}

但这是一个非常简单的方法,例如,有很多警告
  • 子项目1无法针对子项目2进行编译
  • 不会包含子项目2的任何依赖项

  • 我实际上建议将subproject2声明为subproject1的依赖项,并使用Peter建议的插件之一。

    关于gradle - Gradle依赖项不符合我的期望,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23861908/

    10-09 13:45