Gradle "Entry .classpath is a duplicate but no duplicate handling strategy has been set"
Asked Answered
E

13

89

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?

Effable answered 26/4, 2021 at 10:52 Comment(4)
If you're on linux or OSX, try: find . -name .classpath -type f in the root folder of your projectGraiae
Just checked this. I don't have any .classpath in my projectEffable
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.Conceive
@DanielAlder, thanks for your comment. It helped me a lot. I had exactly this problem.Aerophyte
C
55
jar {
   duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}
Callisto answered 13/12, 2021 at 18:46 Comment(3)
For people using Spring Boot plugin: replace jar with bootJar.Northumbria
Similar for my case with java gradle plugin: replace jar with sourcesJarPerla
this one is not workingPutdown
O
80

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))
    }
}
Ontina answered 25/8, 2021 at 12:13 Comment(3)
how can conflicts be MERGED?Chiropractor
Added duplicatesStrategy = DuplicatesStrategy.EXCLUDE and it works! Magic!Bandog
this one is not workingPutdown
C
55
jar {
   duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}
Callisto answered 13/12, 2021 at 18:46 Comment(3)
For people using Spring Boot plugin: replace jar with bootJar.Northumbria
Similar for my case with java gradle plugin: replace jar with sourcesJarPerla
this one is not workingPutdown
A
27

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

Astrometry answered 5/1, 2023 at 9:3 Comment(1)
This one solved my case. Clean slate start sometimes did the trick.Arthrospore
H
15

Please add this

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

In the build.gradle file then solved it.

Hamforrd answered 20/6, 2022 at 9:44 Comment(3)
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 .....Killick
tasks.withType<Jar> doesn't work for me, this Copy doesCorset
This one worked for me. It seems to depend on which task experiences a conflict.Pusher
H
7

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 }
Hydroid answered 11/6, 2021 at 8:40 Comment(3)
INHERIT didn't work for me but EXCLUDE did.Catatonia
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 differenceMcgarry
this one is not workingPutdown
M
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.

Marcasite answered 30/4, 2021 at 14:12 Comment(0)
A
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.

Appendectomy answered 1/7, 2021 at 11:29 Comment(0)
A
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.

Armond answered 24/5, 2023 at 9:59 Comment(0)
C
2

Go to build.gradle file and add the following

tasks.named("jar") {
  duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  exclude("classpath.index")
}
Casual answered 16/11, 2023 at 2:41 Comment(0)
H
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

Horntail answered 6/11, 2022 at 18:33 Comment(0)
S
0
tasks.getByName<Jar>("jvmJar") {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
Swordtail answered 13/5, 2023 at 10:0 Comment(0)
D
0

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

Dulse answered 19/6, 2023 at 6:29 Comment(0)
J
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.

Jolo answered 6/3 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.