Skip to content
BETAGuardrails are in beta. The library, the configuration format and the CLI command can still change.

Guardrails / java

Java version is declared

java/version-declared@v1

The build declares the Java release it compiles for, and that release is no older than the configured floor.

Idjava/version-declared
Versionv1
Categoryjava
Default severitywarning
Interpreterpython3
Timeout30 seconds
Violations tolerated0
Collectsjava

Collectors

This guardrail gathers nothing itself. It depends on the collectors below, which the CLI runs once per build before any check, and reads what they found out of GUARDRAIL_FACTS. A collector that collects nothing skips this guardrail rather than failing it.

CollectorGathersInputs it is given
javaThe Java version the Gradle or Maven build declares, how it declares it, and whether that mechanism makes the build reproducible, for the project and for every Gradle project it includes.projectDir

The inputs above are this guardrail's own, passed straight through. Configuring one in buildnote.json changes what is collected, and two guardrails configured the same way share the one collection.

Configuration

json
{
  "guardrails": {
      "failOn": "error",
      "comment": true,
      "checks": [
          {
              "use": "java/version-declared@v1",
              "severity": "warning",
              "with": {
                  "projectDir": ".",
                  "minVersion": "17"
              },
              "exemptions": []
          }
      ]
  }
}

Inputs

InputDescriptionDefaultEnvironment variable
projectDirDirectory holding the project, relative to the directory the CLI runs in..GUARDRAIL_INPUT_PROJECTDIR
minVersionOldest Java release accepted, as a release number such as 17. A declared release below this is a violation.17GUARDRAIL_INPUT_MINVERSION

How to fix

Declare the release where the build already declares itself:

kotlin
java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}

Maven says the same thing with <maven.compiler.release>21</maven.compiler.release>. With nothing declared the build compiles for whatever release the JDK on the runner defaults to, so a call added in a newer release compiles on the machine that introduced it and breaks on the agent carrying an older JDK.

More in java

  • java/toolchain-declared. The build chooses the JDK it compiles with through a Java toolchain, rather than inheriting whichever JDK started it.
  • java/version-reproducible. The Java release is declared by a mechanism that provisions the JDK, rather than one that asks whichever JDK is running to target an older release.

All 3 java guardrails

Buildnote Limited
Registered in England and Wales, Reg: 16140412