Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 326
136 Avatar asked Apr 26 '21 10:04

136


6 Answers

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))
    }
}
like image 113
João Carlos Avatar answered Oct 17 '22 18:10

João Carlos


jar { duplicatesStrategy(DuplicatesStrategy.EXCLUDE) .....

like image 12
Marina Mikilchenko Avatar answered Oct 17 '22 16:10

Marina Mikilchenko


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 }
like image 6
korn Avatar answered Oct 17 '22 18:10

korn


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.

like image 5
yatsvic Avatar answered Oct 17 '22 17:10

yatsvic


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.

like image 3
Kirill Avatar answered Oct 17 '22 17:10

Kirill


Please add this

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

In the build.gradle file then solved it.

like image 2
Morteza Avatar answered Oct 17 '22 17:10

Morteza