75

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?

136
  • 1,083
  • 1
  • 7
  • 14
  • If you're on linux or OSX, try: `find . -name .classpath -type f` in the root folder of your project – tim_yates Apr 26 '21 at 10:57
  • Just checked this. I don't have any `.classpath` in my project – 136 Apr 26 '21 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: https://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. – Daniel Alder Jan 10 '23 at 10:06
  • @DanielAlder, thanks for your comment. It helped me a lot. I had exactly this problem. – Mr. BoFrost Jan 12 '23 at 08:19

11 Answers11

71

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))
    }
}
João Carlos
  • 1,405
  • 1
  • 15
  • 21
33
jar {
   duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}
Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
21

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

wprogLK
  • 488
  • 5
  • 11
13

Please add this

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

In the build.gradle file then solved it.

Morteza
  • 141
  • 1
  • 6
  • 1
    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 ....._ – Steve Jun 20 '22 at 09:49
  • tasks.withType doesn't work for me, this Copy does – Mia Nov 25 '22 at 09:13
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 }
korn
  • 71
  • 1
  • 4
  • `INHERIT` didn't work for me but `EXCLUDE` did. – Brian White Feb 13 '23 at 03:27
  • `DuplicatesStrategy.INHERIT` is already the default in gradle 7 - https://docs.gradle.org/7.5.1/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:duplicatesStrategy, so setting it to the same value again might not make any difference – devatherock Apr 07 '23 at 16:24
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.

yatsvic
  • 61
  • 2
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.

Kirill
  • 6,762
  • 4
  • 51
  • 81
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

faraz naeem
  • 91
  • 1
  • 5
0
tasks.getByName<Jar>("jvmJar") {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
Dani Pralea
  • 4,545
  • 2
  • 31
  • 49
0

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.

miracle_the_V
  • 1,006
  • 1
  • 14
  • 31
0

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