Gradle doing weird things. Duplicating files on .jar

I don’t understand what is doing Gradle:

In a “jar” task of a multi java project I have a single “from”, if I do this it works:

'jar {

from project.sourceSets.main.output

… }’

but if I add “.classesDir” I get every class duplicated in my .jar:

'jar {

from project.sourceSets.main.output.classesDir

… }’

ok, classesDir is a File instance pointing to an existing directory where classes exists only once, what’s wrong?

next thing, if instead of the first snippet) I do:

'jar {

from(project.sourceSets.main.output) {} … }’

then again everything duplicated…

Can somebody explain me this please?

Thanks!

It’s working as expected:

//below is redundant because the java plugin does it automatically
jar {
     from project.sourceSets.main.output
 }
  //below does not make much sense because java plugin already puts the output in the jar
//adding a new 'from' folder causes the duplicates
jar {
     from project.sourceSets.main.output.classesDir
 }
  //below means exactly the same as the 1st
jar {
     from(project.sourceSets.main.output) {}
 }

Hope that helps. If in doubts, please read the chapter about the java plugin in the user guide :slight_smile:

Hello,

In fact, the jar plugin has a bug: if ‘sourceSets.output.main.{classes,resources}Dir’ are the same, it will happily create a jar will all files added twice, without noticing the fact that they are in fact the same path.

I noticed the problem very recently while working on a pull request to the gradle-fatjar-plugin. And I think gradle should do the same here: when collecting files, it should take each file’s canonical path and add it to a ‘Set’.

Gradle 1.7 adds the ability to control how duplicates are dealt with when creating archives.

Trying to unique the files as you suggest won’t quite work. While they are the same source file, there’s nothing stopping one of the tasks (compile or processResources) transforming them in some way.

It might be better to merge identical entries when merging, which will be another kind of duplicates strategy.