You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle 9.1.0配置earlib依赖报错:配置解析后无法修改

Gradle 9.1.0升级后EAR构建任务报错解决

问题背景

Gradle 8.14.3版本中,以下ViViFYdEAR任务可正常构建包含JAR与WAR的EAR包:

tasks.register('ViViFYdEAR', Ear) {
    project.logger.lifecycle(">>>>>> ViViFYdEAR")

    apply plugin: "ear"
    apply plugin: "kotlin"

    dependsOn copyViViFYdJAR
    dependsOn copyViViFYdWAR

    archiveFileName = "ViViFYd.ear"

    manifest {
        from("./src/main/resources/META-INF/MANIFEST.MF")
    }

    exclude "**/*.class"
    exclude "**/asm-Java.jar"
    exclude "**/jboss-deployment-structure.xml"

    dependencies {
        earlib "com.google.code.gson:gson:${versions.gson}"
        earlib "org.apache.commons:commons-lang3:${versions.commons}"
        earlib "org.jsoup:jsoup:${versions.jsoup}"
        earlib "net.bytebuddy:byte-buddy:${versions.bytebuddy}"
        earlib "org.jetbrains.kotlin:kotlin-stdlib:${versions.kotlin}"

        earlib "org.apache.httpcomponents:httpclient:${versions.httpclient}"
        earlib "org.apache.httpcomponents:httpcore:${versions.httpcore}"

        earlib "org.apache.pdfbox:pdfbox:${versions.pdfbox}"

        earlib "dev.morphia.morphia:morphia-core:${versions.morphia}"
        earlib "dev.morphia.morphia:morphia-kotlin:${versions.morphia}"

        earlib platform("org.mongodb:mongodb-driver-bom:${versions.mongodb}")
        earlib "org.mongodb:mongodb-driver-core"
        earlib "org.mongodb:mongodb-driver-sync"
        earlib "org.mongodb:bson"
    }
}

切换至Gradle 9.1.0(distributionUrl=https://services.gradle.org/distributions/gradle-9.1.0-bin.zip)后,执行构建报错:

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/NOTiFY/IdeaProjects/ViViFYd/build.gradle' line: 472

* What went wrong:
Could not determine the dependencies of task ':test'.
> Could not create task ':ViViFYdEAR'.
   > Cannot mutate the dependencies of configuration ':earlib' after the configuration was resolved. After a configuration has been observed, it should not be modified.

* 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 generate a Build Scan (Powered by Develocity).
> Get more help at https://help.gradle.org.

通过Develocity扫描得到的核心错误:

Could not create task ':ViViFYdEAR'.
> Cannot mutate the dependencies of configuration ':earlib' after the configuration was resolved. After a configuration has been observed, it should not be modified.

错误触发点为任务内的earlib "com.google.code.gson:gson:${versions.gson}"配置行。

原因分析

Gradle 9.x强化了配置阶段与执行阶段的边界检查,不允许在任务配置块内修改已被解析的配置(如earlib)。原代码存在两个关键问题:

  1. 在任务内部应用ear和kotlin插件,导致插件初始化时机滞后,earlib配置可能提前被解析
  2. 在任务内部的dependencies块中添加earlib依赖,此时earlib配置可能已经被项目其他逻辑观察/解析,后续修改触发报错

解决方案

调整代码结构,将插件应用、依赖配置移至项目根级别,任务仅保留自身特有的配置逻辑:

修改后的代码

  1. 项目根级别应用插件:
apply plugin: "ear"
apply plugin: "kotlin"
  1. 项目根级别配置earlib依赖:
dependencies {
    earlib "com.google.code.gson:gson:${versions.gson}"
    earlib "org.apache.commons:commons-lang3:${versions.commons}"
    earlib "org.jsoup:jsoup:${versions.jsoup}"
    earlib "net.bytebuddy:byte-buddy:${versions.bytebuddy}"
    earlib "org.jetbrains.kotlin:kotlin-stdlib:${versions.kotlin}"

    earlib "org.apache.httpcomponents:httpclient:${versions.httpclient}"
    earlib "org.apache.httpcomponents:httpcore:${versions.httpcore}"

    earlib "org.apache.pdfbox:pdfbox:${versions.pdfbox}"

    earlib "dev.morphia.morphia:morphia-core:${versions.morphia}"
    earlib "dev.morphia.morphia:morphia-kotlin:${versions.morphia}"

    earlib platform("org.mongodb:mongodb-driver-bom:${versions.mongodb}")
    earlib "org.mongodb:mongodb-driver-core"
    earlib "org.mongodb:mongodb-driver-sync"
    earlib "org.mongodb:bson"
}
  1. 简化ViViFYdEAR任务配置:
tasks.register('ViViFYdEAR', Ear) {
    project.logger.lifecycle(">>>>>> ViViFYdEAR")

    dependsOn copyViViFYdJAR
    dependsOn copyViViFYdWAR

    archiveFileName = "ViViFYd.ear"

    manifest {
        from("./src/main/resources/META-INF/MANIFEST.MF")
    }

    exclude "**/*.class"
    exclude "**/asm-Java.jar"
    exclude "**/jboss-deployment-structure.xml"
}

关键说明

  • Gradle 9.x严格遵循配置阶段只做配置,执行阶段只做执行的原则,所有插件应用、依赖配置必须在项目配置阶段完成,不能延迟到任务配置块中
  • ear插件提供的earlib配置属于项目级配置,应在项目根dependencies块中声明,而非任务内部

内容的提问来源于stack exchange,提问作者NOTiFY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 07:44:52