Can't run 'dependencies { earlib ...' on Gradle 9.1.0

Building & deplying my EAR (JAR + WAR) on ‘gradle.build’ with ‘gradle-wrapper.properties’ runs fine on ‘gradle-8.14.3’:

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"
    }
}

However when using 9.1.0:

distributionUrl=https\://services.gradle.org/distributions/gradle-9.1.0-bin.zip

It errors with:

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.

Shows error on '–scan ’ with 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.

Error starts with:

earlib "com.google.code.gson:gson:${versions.gson}" // gson: 2.13.2",

Any suggestions? TIA.

You have all the configuration that does not belong there inside the configuration action for the ViViFYdEAR task.
That means it is only executed if the ViViFYdEAR task is configured.
This - as long as task-configuration avoidance is not broken - happens as late as possible and too late to add further dependencies to the earlib configuration which was already used in dependency resolution before the task is configured (which is another sign of something maybe doing bad things as configurations should also not be resolved at configuration time).

Also, before doing a major version upgrade in Gradle you should fix all deprecation warnings, because most become errors after the upgrade. While running on 8.14.3 you should already get a deprecation warning about this behavior.

Move everything that is not configuring the ViViFYdEAR task out of the configuration action for it, like the dependencies block.

Also you should not use apply ... that is legacy and discouraged.
You should always use the plugins { ... } block to apply plugins unless you have very good reasons not to which are extremely rare.

Also, an explicit dependsOn that does not have a lifecycle task on the left-hand side is almost always a code-smell and a sign of something not being done properly, most often task outputs are not properly wired to task inputs which would bring necessary task dependencies automatically.

Btw. just if you are not aware, your logging output appears as soon as your task is configured, whether it will be executed or not or when.


I also highly recommend switching to Kotlin DSL.
By now it is the default DSL, you immediately get type-safe build scripts, actually helpful error messages if you mess up the syntax, and amazingly better IDE support when using a good IDE like IntelliJ IDEA or Android Studio.

Using 8.14.3:

This build did not contain any deprecations.

Using 9.1.0:

dependencies {
    // General utilities
    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}"

    // Apache HTTP Components
    earlib "org.apache.httpcomponents:httpclient:${versions.httpclient}"
    earlib "org.apache.httpcomponents:httpcore:${versions.httpcore}"

    // PDF processing
    earlib "org.apache.pdfbox:pdfbox:${versions.pdfbox}"

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

    // Align MongoDB driver modules via BOM
    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"
}
tasks.register('ViViFYdEAR', Ear) {
//    project.logger.lifecycle(">>>>> 6.1 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"
    project.logger.lifecycle(">>>>> 6.1 ViViFYdEAR")
}

Errors with:

FAILURE: Build failed with an exception.

* What went wrong:
java.lang.NoClassDefFoundError: org/jetbrains/kotlin/gradle/plugin/AbstractKotlinPluginWrapper
> org/jetbrains/kotlin/gradle/plugin/AbstractKotlinPluginWrapper

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Get more help at https://help.gradle.org.

BUILD FAILED in 4s

Using : id(‘org.jetbrains.kotlin.jvm’) version ‘2.2.20’

FIXED:

Deleted: .gradle & rebuilt:

Started just now, finished just now, 14s total build time
Gradle 9.1.0, Develocity plugin 4.1
Explore console log
0 failures
This build did not contain any failures.
0 build deprecations
This build did not contain any deprecations.

Thanks.

Please fix the formatting of your post, it is totally unreadable.