Duplicate files in jar


i am trying to build an IDEA plugin (with gradle 2.2).

To run, debug or package the plugin from IDEA I need to write classes and resources into the same directory. Otherwise IDEA will ignore the resources.

My problem now is that classes and resource are duplicated in the final jar file. I tried to handle this by creating my own ‘jar’ task that does only include my target directory. Unfortunately it does not work as expected.

Interesting is that the file are not duplicated if I just run ‘gradle clean jar’. If I run ‘gradle clean build’ all files are duplicated in the jar.

Is there any explanation why this does happen? How can I solve it?

Another thing I do not understand is that running ‘gradle clean jar’ prints the debug stuff from ‘eachFile’ and ‘doLast’. Running ‘gradle clean build’ does not. Any idea what is happening here?

Here is my code:

sourceSets {

main {

output.classesDir = “$buildDir/plugin”

output.resourcesDir = “$buildDir/plugin”

} }

task jar (type: Jar, overwrite: true, dependsOn: ‘classes’) {

from “$buildDir/plugin”

eachFile {

println it


doLast {

println “#### JAR ####”

} }

Overwriting a task isn’t safe, and shouldn’t be used. The duplicates arise because the ‘jar’ task is configured to include both classes and resources, which now point to the same directory. To solve this, you can set ‘jar.duplicatesStrategy = DuplicatesStrategy.EXCLUDE’ (instead of the whole task redeclaration).

As for the debug output problem, not overwriting the task may solve that as well. Another potential cause is that the task is considered up-to-date. That shouldn’t happen on a clean build though, unless the task isn’t configured correctly, or there is nothing to include in the Jar.

1 Like

Thanks Peter, I will try the duplicate strategy, without overwriting.

Regarding classes/resources, does that mean that ‘from’ in the jar task does not overwrite the sources for jar? Does it just add another source or is it not used at all here? Or could that be another unwanted effect of overwriting?

Is it save to use ‘tasks.remove()’ and add a task withe the same name or would that be the same as overwrite=true?

Hello, Try DuplicateFilesDeleter for your problem…