Multiproject gradle duplicate dependencies in distribution ZIP

最后都变了- 提交于 2020-01-02 06:17:19

问题


I have a gradle multi-project setup for which I wish to collect all the dependent and output JARs into a ZIP at the top level. I've got something working, however I end up with duplicates in the ZIP file. I've not found anything useful in the official documentation on multi project setups

  • How can I remove the duplicates?
  • Is there another approach I should take?

Structure

./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

Top level 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'

Lower level build.gradle files for foo and bar (both same)

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

When I run gradle :buildDist from the top level the ZIP has duplicates

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

回答1:


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

To see all configuration options for a particular Gradle task type, consult the Gradle Build Language Reference.




回答2:


This is best I've come up with

  • resolve all dependencies to File instances and drop into a HashSet.
  • Wrap in a gradle.taskGraph.whenReady closure otherwise the premature resolution causes the configuration to be locked when they try to add a dependency closure.

Example

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'

   }
}


来源:https://stackoverflow.com/questions/25752078/multiproject-gradle-duplicate-dependencies-in-distribution-zip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!