Skip to content

mixedJavaRelease compatibility with toolchain #187

@carl-mastrangelo

Description

@carl-mastrangelo

In Gradle 6.8, a new clause called toolchain was introduced that decoupled the JDK used for Gradle itself from the JDK used to compile and run the project's code. One of the limitations of this new clause is that the sourceRelease and targetRelease versions can no longer be set on the (sub)project itself, since it's forbidden by the toolchain. For example this fails:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(11)
    }

    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

To work around this, the source and target can be set on the JavaCompile tasks directly. For example this works:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(11)
    }
}

tasks.withType(JavaCompile) {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

I am trying to use the Java modularity plugin to compile some of my subprojects with lower target compatibilities, but using a more modern toolchain. I would like to use the mixedJavaRelease feature to compile the subproject with JDK 8, but the module-info.java with JDK 9. This works when not using the tool chain, because the source and target compatibility can be set on the factored out Gradle task. However, when using the toolchain, javamodularity tries to set source and target compatibility on the task itself, which Gradle disallows. For example this fails again:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(11)
    }
}

tasks.withType(JavaCompile) {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

modularity.mixedJavaRelease 8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions