Gradle throws Entry .classpath is a duplicate but no duplicate handling strategy has been set error when it finds duplicate class files but there is no way defined to process those duplicates. In general when there are duplicate files, you have options to either overwrite the previous one or neglect the new one. In any case, you will need to let Gradle know what to do.
There are 5 types of duplicate strategies –
-
EXCLUDE
– Keep the previous file. Ignore subsequent duplicates. -
FAIL
– Throw exceptionDuplicateFileCopyingException
-
INCLUDE
– Do not prevent duplicate creation -
INHERIT
– Use parent strategy orINCLUDE
-
WARN
– Log a warning message but useINCLUDE
strategy.
Solutions
1. Using strategy in tasks.withType
You can add a strategy to tasks.withType(){}
block in build.gradle
file like this –
tasks.withType<Jar>() { duplicatesStrategy = DuplicatesStrategy.EXCLUDE ... }
2. For Spring Boot app add to processResources
processResources { with copySpec { from 'src/main/resources' duplicatesStrategy = DuplicatesStrategy.EXCLUDE 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()]) } } } }