Multiproject gradle duplicate dependencies in dist

2019-05-06 15:42发布

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

2条回答
Deceive 欺骗
2楼-- · 2019-05-06 16:03
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.

查看更多
干净又极端
3楼-- · 2019-05-06 16:08

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'

   }
}
查看更多
登录 后发表回答