Gradle“条目.classpath是重复的,但尚未设置重复处理策略”

问题描述 投票:0回答:12

我正在尝试构建一个 gradle 项目,但是,当我尝试

$ gradle build
时,我得到以下输出:

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

做了

Get-ChildItem -Path ./ -Filter .classpath -Recurse -Force
之后我得出的结论是 我的项目中甚至没有一个名为
.classpath
的文件。我该怎么办?

java gradle build compilation
12个回答
72
投票

与@korn的答案类似,我使用

EXCLUDE
策略解决了我的问题;

tasks.withType<Jar>() {

    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    manifest {
        attributes["Main-Class"] = "MainKt"
    }

    configurations["compileClasspath"].forEach { file: File ->
        from(zipTree(file.absoluteFile))
    }
}

39
投票
jar {
   duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}

24
投票

我在 IntelliJ 中执行一些测试时遇到了同样的问题。对我有帮助的只是运行一个简单的

gradle clean


12
投票

请添加这个

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

build.gradle
文件中然后解决它。


7
投票

我在使用 kotlin 和 gradle 7 构建时遇到了同样的问题。将以下代码添加到您的

build.gradle.kts
即可解决问题。

tasks.withType<Jar> { duplicatesStrategy = DuplicatesStrategy.INHERIT }

6
投票

如果您使用 Kotlin DSL 和 Gradle 7.0,可能是由于该错误 KT-46165 应该在 1.5.0 版本中修复。


4
投票

不知道您的情况,您甚至找不到

.classpath
文件(据我所知,该文件通常是使用我不使用的 Eclipse IDE 创建的)

但是我在将 Spring Boot 应用程序升级到 Gradle 7.x 时遇到了同样的错误。 我的构建脚本有额外的资源处理任务来支持

@..@
风格的占位符(就像 Spring Boot Maven 构建那样,因为现在我支持项目中的两个构建系统,并且我需要它们表现相同):

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()])
                }
        }
    }
}

我在 Gradle 7 中遇到了同样的错误:

条目 my-app.properties 重复,但未设置重复处理策略。详情请参阅 https://docs.gradle.org/7.1/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy

而且,确实有一个重复的。 Gradle 首先将未处理的文件复制到

build/resources/main
,然后尝试执行我的自定义
processResources
并将文件再次复制到同一位置。

解决方案是将

duplicatesStrategy = 'include'
添加到
with copySpec {}
块中。看起来之前 Gradle 默默地覆盖了重复的内容,所以没有问题。


2
投票

在我的例子中,有一个通过依赖暴露的

com.sun.xml.ws:jaxws-rt
,而我的项目使用
org.glassfish.jaxb:jaxb-runtime

两者都会在类路径中生成具有相同名称 (

jaxb-core.jar
) 的 jar。

从依赖中删除

com.sun.xml.ws:jaxws-rt
有帮助。 幸运的是,依赖性在我的控制之中。


0
投票

检查您的应用程序级别构建等级,看看您是否没有在下面的行中输入两次字体名称

project.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 ]
如果您有任何重复的姓名条目对我有用,请删除


0
投票
tasks.getByName<Jar>("jvmJar") {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

0
投票

我在构建弹性搜索存储库时遇到了同样的错误。删除 build-tools-internal 文件夹中的构建文件以某种方式对我有用。


0
投票

转到 build.gradle 文件并添加以下内容

tasks.named("jar") {
  duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  exclude("classpath.index")
}
© www.soinside.com 2019 - 2024. All rights reserved.