96

I'm trying to build a gradle project but, when I try $ gradle build I get the following output:

Starting a Gradle Daemon (subsequent builds will be faster) > Task :jar FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':jar'. > Entry .classpath is a duplicate but no duplicate handling strategy has been set. Please refer to https://docs.gradle.org/7.0/dsl/org.gradle.api.file.CopySpec.html#org.gradle.api.file.CopySpec:duplicatesStrategy for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 11s 4 actionable tasks: 2 executed, 2 up-to-date 

After doing Get-ChildItem -Path ./ -Filter .classpath -Recurse -Force I concluded that I don't even have a single file named .classpath in my project. What do I do?

4
  • If you're on linux or OSX, try: find . -name .classpath -type f in the root folder of your project Commented Apr 26, 2021 at 10:57
  • Just checked this. I don't have any .classpath in my project Commented Apr 26, 2021 at 11:19
  • 2
    Also note that if the error is about an Entry classpath.index instead of .classpath (google shows this question as first answer then), it's a problem caused by intellij. see: github.com/gradle/gradle/issues/17236. The "entry" is actually a file which exists with the same name in different folders, this is not very clear from the error message. Commented Jan 10, 2023 at 10:06
  • @DanielAlder, thanks for your comment. It helped me a lot. I had exactly this problem. Commented Jan 12, 2023 at 8:19

14 Answers 14

87

Similar to @korn answer, I solved mine using the EXCLUDE Strategy;

tasks.withType<Jar>() { duplicatesStrategy = DuplicatesStrategy.EXCLUDE manifest { attributes["Main-Class"] = "MainKt" } configurations["compileClasspath"].forEach { file: File -> from(zipTree(file.absoluteFile)) } } 
Sign up to request clarification or add additional context in comments.

3 Comments

how can conflicts be MERGED?
Added duplicatesStrategy = DuplicatesStrategy.EXCLUDE and it works! Magic!
this one is not working
64
jar { duplicatesStrategy(DuplicatesStrategy.EXCLUDE) } 

3 Comments

For people using Spring Boot plugin: replace jar with bootJar.
Similar for my case with java gradle plugin: replace jar with sourcesJar
this one is not working
29

I was having the same issues while executing some tests in IntelliJ. What helped me was just run a simple gradle clean.

1 Comment

This one solved my case. Clean slate start sometimes did the trick.
17

Please add this

tasks.withType(Copy).all { duplicatesStrategy 'exclude' }

In the build.gradle file then solved it.

3 Comments

Please avoid to start an answer with I have the same problem because you risk to have it flagged as Not An Answer. Instead use a more affirmative text like To solve the problem you should .....
tasks.withType<Jar> doesn't work for me, this Copy does
This one worked for me. It seems to depend on which task experiences a conflict.
8

I faced same issue while building with kotlin and gradle 7. Resolve the issue adding the below code to your build.gradle.kts.

tasks.withType<Jar> { duplicatesStrategy = DuplicatesStrategy.INHERIT } 

3 Comments

INHERIT didn't work for me but EXCLUDE did.
DuplicatesStrategy.INHERIT is already the default in gradle 7 - docs.gradle.org/7.5.1/dsl/…, so setting it to the same value again might not make any difference
this one is not working
6

If you use Kotlin DSL and Gradle 7.0 it may be due to that bug KT-46165 It should be fixed in version 1.5.0.

Comments

4

Do not know about your case with .classpath file which you can not even find (as I know this file is usually created with Eclipse IDE which I do not use)

But I faced the same error upgrading Spring Boot app to Gradle 7.x. My build script had additional resources processing task to support @..@-style placeholders (like Spring Boot Maven build does, cause for now I support both build systems in the project and I need them to behave equal):

processResources { with copySpec { from 'src/main/resources' include 'my-app*.yml' include 'my-app*.yaml' include 'my-app*.properties' project.properties.findAll().each { prop -> if (prop.value != null) { filter(ReplaceTokens, tokens: [(prop.key): prop.value.toString()]) } } } } 

I got the same error with Gradle 7:

Entry my-app.properties is a duplicate but no duplicate handling strategy has been set. Please refer to https://docs.gradle.org/7.1/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy for details.

And, indeed, there is a duplicate. Gradle first copies unprocessed file to build/resources/main and later tries to execute my custom processResources and copy files again to the same place.

The solution was adding duplicatesStrategy = 'include' to with copySpec {} block. Looks like previously Gradle silently overwrote the duplicate so there was no problem.

Comments

3

In my case, there was a com.sun.xml.ws:jaxws-rt exposed through dependency, while my project uses org.glassfish.jaxb:jaxb-runtime

Both resulted in jar with the same name (jaxb-core.jar) in classpath.

Removing com.sun.xml.ws:jaxws-rt from dependency helped. Fortunately, depencency was in my control.

Comments

2

Go to build.gradle file and add the following

tasks.named("jar") { duplicatesStrategy = DuplicatesStrategy.EXCLUDE exclude("classpath.index") } 

Comments

0

check your app level build gradle and look if you did not enter font name twice in folloing lineproject.ext.vectoricons = [ iconFontNames: [ 'FontAwesome.ttf','FontAwesome5_Brands.ttf','FontAwesome5_Regular.ttf','FontAwesome5_Solid.ttf',"Foundation.ttf","Ionicons.ttf","MaterialIcons.ttf","MaterialCommunityIcons.ttf","AntDesing.ttf","Entypo.ttf","EvilIcons.ttf","Feather.ttf","FontAwesome5.ttf","SimpleLineIcons.ttf","Octicons.ttf"] // Name of the font files you want to copy ] remove if you have any duplicate name entry worked for me

Comments

0
tasks.getByName<Jar>("jvmJar") { duplicatesStrategy = DuplicatesStrategy.EXCLUDE } 

Comments

0

I was having same error, while building elastic search repository. Deleting the build files in build-tools-internal folder worked for me somehow.

Comments

0

In my case even after adding exclude did not work.

I havd to manually delete the META-INF file from src folder and to rebuild from terminal to solve the issue.

Comments

0

Check the build directory if classpath.index exists. if it does. delete it and rebuild the project.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.