多项目在分发ZIP中重复依赖关系

时间:2014-09-09 19:21:24

标签: gradle build.gradle

我有一个gradle多项目设置,我希望将所有依赖和输出JAR收集到顶层的ZIP中。我有一些工作,但我最终在ZIP文件中重复。我在official documentation on multi project setups

中找不到任何有用的内容
  • 如何删除重复项?
  • 我应该采取另一种方法吗?

结构

./multi-project
./multi-project/build.gradle
./multi-project/settings.gradle
./multi-project/bar
./multi-project/bar/build.gradle
./multi-project/foo
./multi-project/foo/build.gradle

顶级build.gradle

apply plugin: 'java'

allprojects {
  apply plugin: 'java'

  repositories {
    mavenCentral()
  }
}

task buildDist(type: Zip) {
    from subprojects.configurations.compile into 'jars'
    from subprojects.jar.outputs.files into 'jars'
}

Settings.gradle

include ':foo'
include ':bar'

foo和bar的低级build.gradle文件(两者都相同)

dependencies {
   compile 'org.springframework:spring-beans:4.1.0.RELEASE'
}

当我运行gradle:来自顶层的buildDist时,ZIP有重复

unzip -l build/distributions/multi-project.zip 

Archive:  build/distributions/multi-project.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2014-09-09 20:17   jars/
   701334  2014-09-09 19:53   jars/spring-beans-4.1.0.RELEASE.jar
    62050  2014-07-05 21:09   jars/commons-logging-1.1.3.jar
  1005039  2014-09-09 19:53   jars/spring-core-4.1.0.RELEASE.jar
   701334  2014-09-09 19:53   jars/spring-beans-4.1.0.RELEASE.jar
    62050  2014-07-05 21:09   jars/commons-logging-1.1.3.jar
  1005039  2014-09-09 19:53   jars/spring-core-4.1.0.RELEASE.jar
      301  2014-09-09 20:12   jars/bar.jar
      301  2014-09-09 20:12   jars/foo.jar

2 个答案:

答案 0 :(得分:10)

task buildDist(type: Zip) {
    into 'jars'
    from { subprojects.configurations.runtime }
    from { subprojects.jar }
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

要查看特定Gradle任务类型的所有配置选项,请参阅Gradle Build Language Reference

答案 1 :(得分:0)

我最好提出

  • 解析所有依赖项到File实例并放入HashSet。
  • 包装在gradle.taskGraph.whenReady闭包中,否则过早解析会导致配置在尝试添加依赖关闭时被锁定。

实施例

task buildDist(type: Zip) {
    gradle.taskGraph.whenReady { taskGraph ->
      def uniqueFiles = new HashSet()

      uniqueFiles.addAll(subprojects.configurations.compile.resolvedConfiguration.resolvedArtifacts.file)
      uniqueFiles.addAll(subprojects.jar.outputs.files)

      from uniqueFiles into 'jars'

   }
}