Gradle Runtime Compatibilities
You can open this sample inside an IDE using the IntelliJ native importer, Eclipse Buildship. |
This sample shows how to use the plugin development toolbox to query Gradle runtime compatibility information to configure non-plugin projects. The project is a simple Java library targeting, in this example, Gradle runtime 4.9 with unit tests.
pluginManagement {
repositories {
gradlePluginPortal()
maven { url = uri('https://repo.nokee.dev/release') }
}
}
plugins {
id 'dev.gradleplugins.gradle-plugin-development' version '1.2'
}
rootProject.name = 'gradle-plugin-development-runtime-compatibilities'
plugins {
id 'java-library'
id 'groovy-base'
}
import static dev.gradleplugins.GradleRuntimeCompatibility.*
java {
targetCompatibility = minimumJavaVersionFor('4.9') (1)
sourceCompatibility = minimumJavaVersionFor('4.9') (1)
}
repositories {
gradlePluginDevelopment() (2)
mavenCentral() (3)
}
dependencies {
compileOnly gradleApi('4.9') (4)
testImplementation gradleApi('4.9') (5)
testImplementation platform('org.spockframework:spock-bom:1.2-groovy-2.4')
testImplementation 'org.spockframework:spock-core'
}
tasks.register('gradleCompatibility') {
def gradleVersion = '4.9'
doLast {
println "=== Gradle ${gradleVersion} Compatibility Information ==="
println "Minimum Java version: ${minimumJavaVersionFor(gradleVersion)}"
println "Groovy version: ${groovyVersionOf(gradleVersion)}"
println "Kotlin version: ${kotlinVersionOf(gradleVersion).orElse('N/A')}"
}
}
pluginManagement {
repositories {
gradlePluginPortal()
maven { url = uri("https://repo.nokee.dev/release") }
}
}
plugins {
id("dev.gradleplugins.gradle-plugin-development") version("1.2")
}
rootProject.name = "gradle-plugin-development-runtime-compatibilities"
plugins {
id("java-library")
id("groovy-base")
}
import dev.gradleplugins.GradleRuntimeCompatibility.*
java {
targetCompatibility = minimumJavaVersionFor("4.9") (1)
sourceCompatibility = minimumJavaVersionFor("4.9") (1)
}
repositories {
gradlePluginDevelopment() (2)
mavenCentral() (3)
}
dependencies {
compileOnly(gradleApi("4.9")) (4)
testImplementation(gradleApi("4.9")) (5)
testImplementation(platform("org.spockframework:spock-bom:1.2-groovy-2.4"))
testImplementation("org.spockframework:spock-core")
}
tasks.register("gradleCompatibility") {
val gradleVersion = "4.9"
doLast {
println("=== Gradle ${gradleVersion} Compatibility Information ===")
println("Minimum Java version: ${minimumJavaVersionFor(gradleVersion)}")
println("Groovy version: ${groovyVersionOf(gradleVersion)}")
println("Kotlin version: ${kotlinVersionOf(gradleVersion).orElse("N/A")}")
}
}
1 | Query the minimum Java version for the specified Gradle version. |
2 | Configure a Maven repository containing the redistributed Gradle API artifacts with the sources. |
3 | Configure Maven central repository to resolve groovy-all and kotlin-stdlib runtime dependencies of the Gradle API. |
4 | Add compile only dependency to the specified Gradle API. |
5 | Add compile and runtime dependency to the specified Gradle API. |
To query the information about Gradle 4.9:
$ ./gradlew gradleCompatibility > Task :gradleCompatibility === Gradle 4.9 Compatibility Information === Minimum Java version: 1.7 Groovy version: 2.4.12 Kotlin version: 1.2.41 BUILD SUCCESSFUL 1 actionable task: 1 executed
To build and test the library:
$ ./gradlew test BUILD SUCCESSFUL 3 actionable tasks: 3 executed
The plugin builds and executes the test as expected. The important point here is the compile vs runtime dependencies:
$ ./gradlew dependencies ... compileClasspath - Compile classpath for source set 'main'. \--- dev.gradleplugins:gradle-api:4.9 \--- org.codehaus.groovy:groovy:2.4.12 ... testCompileClasspath - Compile classpath for source set 'test'. +--- dev.gradleplugins:gradle-api:4.9 | \--- org.codehaus.groovy:groovy:2.4.12 +--- org.spockframework:spock-bom:1.2-groovy-2.4 | \--- org.spockframework:spock-core:1.2-groovy-2.4 (c) \--- org.spockframework:spock-core -> 1.2-groovy-2.4 +--- org.codehaus.groovy:groovy-all:2.4.15 \--- junit:junit:4.12 \--- org.hamcrest:hamcrest-core:1.3 ... testRuntimeClasspath - Runtime classpath of source set 'test'. +--- dev.gradleplugins:gradle-api:4.9 | +--- org.codehaus.groovy:groovy-all:2.4.12 -> 2.4.15 | \--- org.jetbrains.kotlin:kotlin-stdlib:1.2.41 | \--- org.jetbrains:annotations:13.0 +--- org.spockframework:spock-bom:1.2-groovy-2.4 | \--- org.spockframework:spock-core:1.2-groovy-2.4 (c) \--- org.spockframework:spock-core -> 1.2-groovy-2.4 +--- org.codehaus.groovy:groovy-all:2.4.15 \--- junit:junit:4.12 \--- org.hamcrest:hamcrest-core:1.3 ...
We can see the compile dependencies only pull the org.codehaus.groovy:groovy
dependencies as required by the Gradle API.
As for the runtime dependencies, the Gradle API will pull in org.codehaus.groovy:groovy-all
and org.jetbrains.kotlin:kotlin-stdlib
dependencies to allow every feature offered by the Gradle API to work as expected.
Most notably, ProjectBuilder
requires both Groovy and Kotlin dependencies to be present on the runtime classpath.
The outgoing variants shows the plugin configured the JVM version for Gradle 4.9 API properly:
$ ./gradlew outgoingVariants ... -------------------------------------------------- Variant apiElements -------------------------------------------------- Description = API elements for main. Capabilities - :gradle-plugin-development-runtime-compatibilities:unspecified (default capability) Attributes - org.gradle.category = library - org.gradle.dependency.bundling = external - org.gradle.jvm.version = 7 - org.gradle.libraryelements = jar - org.gradle.usage = java-api ...
For more information, see Gradle Plugin Development reference chapters and user manual chapter.