无法使用jdk 17配置gradle 7.4

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

我正在将我的 gradle 项目从 jdk 11 迁移到 jdk 17。 我更改了项目中的jdk版本,gradle jvm并将gradle版本从6.6更改为7.4。

我还将 gradle.properties 文件中的 org.gradle.jvmargs 从这个

-Xmx4g -XX:+UseConcMarkSweepGC -Dfile.encoding=UTF-8
更改为现在的这个
org.gradle.jvmargs=-Xmx4g -XX:+UseZGC -Dfile.encoding=UTF-8

但是当我刷新依赖项时,我收到错误 -

A problem occurred evaluating root project .
> Failed to apply plugin 'org.gradle.java'.
   > Cannot have two attributes with the same name but different types. This container already has an attribute named 'org.gradle.jvm.environment' of type 'java.lang.String' and you are trying to store another one of type 'org.gradle.api.attributes.java.TargetJvmEnvironment'

下面是 build.gradle 文件中的代码部分,我在其中收到此错误 -

subprojects {
    apply plugin: 'jacoco'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'java'

    project.allprojects {subproject ->
        subproject.configurations.all{
            try {
                attributes {
                    attribute(Attribute.of('org.gradle.jvm.environment', String), 'standard-jvm')
                }
            }
            catch(Exception e){}
        }
    }

我尝试调查但找不到解决方案

java spring gradle jvm java-17
1个回答
0
投票

这是一段非常hacky的代码,您试图抓住它,添加一个属性并在失败时捕获错误。

我建议将该属性添加到需要该属性的配置中,而不是这种容易出错的方法。 (我敢说,如果你一开始就这样做,现在就不会有这个问题了。)

如果您无法识别需要此属性的配置,您可以将有问题的代码包装在

afterEvaluate
钩子内,这应该可以解决错误。然而,这应该被视为临时解决方案,因为它是在橡皮膏上粘贴橡皮膏。

project.allprojects {subproject ->
    subproject.afterEvaluate {
        subproject.configurations.all{
            try {
                attributes {
                    attribute(Attribute.of('org.gradle.jvm.environment', String), 'standard-jvm')
                }
            }
            catch(Exception e){}
        }
    }   
}
© www.soinside.com 2019 - 2024. All rights reserved.