Java 使Zip任务在解决依赖项后运行

Java 使Zip任务在解决依赖项后运行,java,gradle,Java,Gradle,我有一个子项目B,它依赖于其他子项目a。我在子项目B的“build.gradle”中包含了子项目a dependencies { compile project(':projA') } 我的两个子项目A和B都会在发布时创建一个捆绑的zip。我想在不再次引用子项目A的情况下,将属于子项目A的一些文件复制到子项目B。根项目的“build.gradle”脚本包含以下任务 subprojects { task bundleBin(type: Zip) { descrip

我有一个子项目B,它依赖于其他子项目a。我在子项目B的“build.gradle”中包含了子项目a

dependencies {
    compile project(':projA')
}
我的两个子项目A和B都会在发布时创建一个捆绑的zip。我想在不再次引用子项目A的情况下,将属于子项目A的一些文件复制到子项目B。根项目的“build.gradle”脚本包含以下任务

subprojects {
    task bundleBin(type: Zip) {
        description 'Creates "bin.zip" bundle.'

        dependsOn build

        def bundleName = "$outputName-bin"

        /// THIS DOES NOT WORK
        def deps = configurations.runtime.getAllDependencies().findAll { it instanceof ProjectDependency }
        println "GROOT: " + deps

        into("$bundleName/dep") {
            /// THE LINE BELOW WORKS
            /// I do not want a fixed reference since it is already defined in each subproject's "build.gradle" file
            //from project(':projA').file('conf/')
            for (dep in deps) {
                def proj = dep.getDependencyProject()
                from (proj.projectDir) {
                    include "conf/"
                    include "scripts/"
                }
            }
        }

        into(bundleName) {
            from(".") {
                include "conf/"
                include "scripts/"
            }
        }

        into("$bundleName/lib") {
            from configurations.runtime.allArtifacts.files
            from configurations.runtime
        }

        archiveName = "${bundleName}.zip"
    }
}
我不想再次引用子项目A的原因是,我有一个依赖于其他项目的项目列表,我不想单独维护每个依赖项

我想要上面的脚本做的是,当运行for B时,在A和B中取“conf/”和“scripts/”,并将它们放在“B-bin.zip”中。然而,如果我有一个子项目C依赖于a和B,上面的脚本将在a、B和C中使用“conf/”和“scripts/”,并将它们放在“C-bin.zip”中

当我运行上述脚本时,依赖项不会出现,除非我将其封装在“doLast”中。但是,这在Zip任务中不起作用


我的问题是,如何解决此问题?

您需要确保首先解决配置问题。 您可以通过使用来实现这一点,但请注意,在配置时解析意味着无论调用什么任务,都将完成此操作,并且应该避免这样做。 建议您可以通过直接迭代配置来实现同样的效果


只有在即将执行任务时,才可以使用延迟解析配置。您仍然可以在那里配置任务

正如@Alpar所提到的,这是由于Zip任务在配置阶段处理依赖项造成的。为了解决这个问题,我遵循了这个原则

因此,我的捆绑包代码现在看起来像:

task bundleBin << {
    task bundleBin_childTask(type: Zip) {
        def bundleName = "$outputName-bin"
        def deps = configurations.runtime.getAllDependencies().findAll { it instanceof ProjectDependency }

        into(bundleName) {
            for (dep in deps) {
                def proj = dep.getDependencyProject()
                from (proj.projectDir) {
                    include "conf/"
                    include "scripts/"
                }
            }
        }

        into(bundleName) {
            from(".") {
                include "conf/"
                include "scripts/"
            }
        }

        into("$bundleName/lib") {
            from configurations.runtime.allArtifacts.files
            from configurations.runtime
        }

        archiveName = "${bundleName}.zip"
    }

    bundleBin_childTask.execute()
}
任务bundleBin