Skip to content

brunoessmann/check-java-version-gradle-plugin

Repository files navigation

Check Java Version Gradle Plugin

Build Status Gradle Plugin Portal Kotlinlang Slack Chat ktlint

https://plugins.gradle.org/plugin/ch.essmann.gradle.check-java-version

The check-java-version Gradle plugin is a settings plugin that ensures that the Java version used to execute your Gradle build is in a certain range.

Ever wondered why your build fails only to find an inconspicuous unsupported class file version buried in pages of build output? Often it is not even your own project, but a third party library that has a dependency on a minimum Java version.

Sounds familiar? Then this simple plugin might be for you. The sole purpose of this plugin is to abort the build with a big friendly and easy to spot message if you are not using the intended Java version:

         )))      *****************************        )))
        (((      *     INVALID JAVA VERSION    *      (((
      +-----+    *  BUILD REQUIRES JAVA >= 17  *    +-----+
     [|     |    *     ACTIVE VERSION IS 8     *    |     |]
      `-----'     *****************************     `-----'

FAILURE: Build failed with an exception.

* What went wrong:
Build requires Java 17, active version is 8

That's all that this plugin does, nothing more, nothing less.

At first glance this is not very useful, but whenever a new team member cannot build a project because of a legacy Java version it might come in handy. It doesn't do anything you wouldn't find out anyway, but it makes it more obvious.

So, if this plugin at some point saves you a couple of minutes, then the inclusion in your build has already been worthwhile.

Usage

To use this plugin simply include it in the plugins block of your settings and configure the Java version you need in the checkJavaVersion block.

Please note, this is a settings plugin and not a project plugin, therefore the declaration is done in the settings file.

settings.gradle.kts (Kotlin)

plugins {
    id("ch.essmann.gradle.check-java-version") version "1"
}

checkJavaVersion {
	minimumJavaVersion.set(JavaLanguageVersion.of(8)) // optional
	maximumJavaVersion.set(JavaLanguageVersion.of(11)) // optional
}

settings.gradle (Groovy)

plugins {
    id 'ch.essmann.gradle.check-java-version' version '1'
}

checkJavaVersion {
	minimumJavaVersion = JavaLanguageVersion.of('8') // optional
	maximumJavaVersion = JavaLanguageVersion.of('11') // optional
}

Configuration

Configuration of the plugin is done in the checkJavaVersion block in the settings file (yes, this block is also in the settings file).

You can specify either minimumJavaVersion or maximumJavaVersion as needed. Both of optional, but if you omit both the plugin obviously won't do anything because there is no default value for either of them.

minimumJavaVersion <= activeJavaVersion <= maximumJavaVersion

To pin the project to a specific major Java version use the same version for minimumJavaVersion or maximumJavaVersion.

The Java version has to be specified as Java Toolchain support language version, e.g. JavaLanguageVersion.of(11).

Note: Only the major language version number is supported. You can't check for minor versions using this plugin.

Java Toolchain Support

Please note that this plugin does not have any relation to the Java toolchain support version that you can define for your project.

This plugin merely makes sure that your Gradle instance is running the Java version that you would expect it to run with.

Also, unless you really want to make sure that a specific Java version is used I would suggest to only use minimumJavaVersion and then set the Java toolchain support language version as desired. Higher Java versions are in most circumstances perfectly capable of compiling compatible versions for lower Java versions.

References

About

Gradle settings plugin that ensures that the Java version used to execute Gradle is in a certain range

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages