有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

解决依赖关系后运行java Make Zip任务

我有一个子项目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任务中不起作用

我的问题是,如何解决这个问题


共 (2) 个答案

  1. # 1 楼答案

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

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

  2. # 2 楼答案

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

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

    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()
    }
    

    此解决方案强制Zip任务在执行阶段解析其包含的文件