Entry .classpath duplicate but no duplicate handling strategy has been set

Total
0
Shares

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

  1. EXCLUDE – Keep the previous file. Ignore subsequent duplicates.
  2. FAIL – Throw exception DuplicateFileCopyingException
  3. INCLUDE – Do not prevent duplicate creation
  4. INHERIT – Use parent strategy or INCLUDE
  5. WARN – Log a warning message but use INCLUDE 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()])
                }
        }
    }
}