Java Project Guardrails
Enforce Java-specific project standards including build tool wrappers, Java version requirements, Maven/Gradle version minimums, and test scope.
java to your lunar-config.yml:uses: github://earthly/lunar-lib/policies/java@v1.0.5
Included Guardrails
This policy includes 7 guardrails that enforce standards for your devex build and ci.
build-tool-wrapper-exists
Ensures a build tool wrapper exists (mvnw for Maven projects, gradlew for Gradle projects). Wrappers ensure reproducible builds without requiring a pre-installed build tool.
min-java-version
Ensures the Java version configured in the build file (pom.xml or build.gradle) meets the minimum. Compares full versions, not just the major; the update a feature-release target can't show comes from Java recorded in CI.
min-java-version-cicd
Ensures every Java version recorded for java/javac commands in CI meets the minimum. Compares full versions and doesn't need a build file.
min-java-version-base-image
Ensures the Java base image of each Dockerfile's runtime (final) stage meets the minimum. Floating, major-only or unrecognized tags skip as unknown.
min-maven-version
Ensures the Maven version used in CI/CD commands meets the minimum requirement. Helps maintain build tool security and compatibility.
min-gradle-version
Ensures the Gradle version used in CI/CD commands meets the minimum requirement. Helps maintain build tool security and compatibility.
tests-all-modules
Ensures tests run across all modules rather than a subset. Prevents accidentally skipping tests in multi-module projects.
How Guardrails Fit into Lunar
Lunar guardrails define your engineering standards as code. They evaluate data collected by integrations and produce pass/fail checks with actionable feedback.
Policies support gradual enforcement—from silent scoring to blocking PRs or deployments—letting you roll out standards at your own pace without disrupting existing workflows.
Learn How Lunar Works →Required Integrations
This policy evaluates data gathered by one or more of the following integration(s).
Make sure to enable them in your lunar-config.yml.
Configuration
Configure this policy in your lunar-config.yml.
Inputs
| Input | Required | Default | Description |
|---|---|---|---|
min_java_version
|
Optional |
17
|
Minimum Java version, as a feature release ("17"), a specific update ("21.0.13.1") or one minimum per feature release ("21.0.13.1, 25.0.5.1, 27.0.1.1") |
min_java_version_cicd
|
Optional |
17
|
Minimum Java version for java/javac commands in CI, in the same forms as min_java_version |
min_java_version_base_image
|
Optional |
17
|
Minimum Java version for runtime base images in Dockerfiles, in the same forms as min_java_version |
java_base_image_families
|
Required | — | Java base images to check, one per line as a repository pattern and a version scheme (temurin, version, chainguard or distro); a `defaults` line adds the built-in list. Empty uses the built-in list |
min_maven_version
|
Optional |
3.9.0
|
Minimum required Maven version (e.g., "3.9.0") |
min_gradle_version
|
Optional |
8.0.0
|
Minimum required Gradle version (e.g., "8.0.0") |
require_java_version_cicd
|
Optional |
false
|
Set to "true" to fail min-java-version-cicd, instead of skipping, when CI recorded no Java version for a Java project. Needs the java collector's project and cicd sub-collectors |
Documentation
View on GitHubJava Project Guardrails
Enforce Java-specific project standards including build tool wrappers, Java version requirements, Maven/Gradle version minimums, and test scope.
Overview
This policy validates Java projects against best practices for build reproducibility and project structure. It ensures projects use build tool wrappers (mvnw/gradlew), target a minimum Java version, use recent build tool versions in CI, and run tests across all modules.
Policies
This plugin provides the following policies (use include to select a subset):
| Policy | Description | Failure Meaning |
|---|---|---|
build-tool-wrapper-exists |
Validates mvnw/gradlew exists | Missing build wrapper for reproducibility |
min-java-version |
Ensures minimum Java version in build config | Java version below the minimum or not comparable to it, or CI ran Java below a minimum's update |
min-java-version-cicd |
Ensures every Java version recorded in CI meets a minimum | CI ran a JDK below the minimum or one whose version wasn't recorded, or (opt-in) recorded no Java for a Java project |
min-java-version-base-image |
Ensures the Java base image of each Dockerfile's runtime stage meets a minimum | A runtime stage builds on a Java image whose tag names a version below the minimum |
min-maven-version |
Ensures minimum Maven version in CI/CD | CI Maven version too old |
min-gradle-version |
Ensures minimum Gradle version in CI/CD | CI Gradle version too old |
tests-all-modules |
Ensures tests run all modules | Tests may miss modules |
Version comparison
min-java-version and min-java-version-cicd compare full versions, so a minimum can be a feature release (17), a specific update (21.0.13.1) or one minimum per feature release. 21.0.5 fails a 21.0.13.1 minimum and passes a 21 one.
| Version | Compared as | Notes |
|---|---|---|
21, 21.0.5, 21.0.13.1, 17.0.21.0.3 |
Element by element | Missing trailing elements count as zero, so 21 equals 21.0.0 |
21.0.5+11, 21.0.5+11-LTS |
21.0.5 |
Build numbers and vendor suffixes are ignored |
27-ea, 27-ea+12 |
Just before 27 |
A pre-release sorts before its release and after every earlier version |
1.8.0_202, 1.8.0_202-b08, 8u202 |
8.0.202 |
Java 8 and earlier; 1.8 is 8 |
Against a minimum with an update, such as 21.0.13.1:
- A
21build target can't show which Java 21 update is in use, somin-java-versiontakes the update from the Java versions thejavacollector recorded in CI. It passes when every recorded version meets the minimum and fails when one doesn't, including a command whose version wasn't recorded. If CI recorded no java or javac commands, for example because the Lunar CI/CD Tracer isn't installed, it skips. A17target fails without CI, and a25target passes. - A version recorded in CI is complete, so
21there is the 21 GA release and21.0.13is below21.0.13.1. - Only build files and CI are checked.
min-java-version-cicdsees the JDKs CI ran, not the JDK in a deployed image or on a host. - The build target comes from the top-level build file. The
javacollector reads<java.version>,<maven.compiler.source>or<maven.compiler.release>frompom.xml, thensourceCompatibility,JavaVersion.VERSION_*or a toolchain'sJavaLanguageVersion.of(21)/jvmToolchain(21)frombuild.gradleorbuild.gradle.kts, first match in that order. A version set elsewhere, such as in a convention plugin, skipsmin-java-version;min-java-version-cicdstill checks its CI JDKs.
Anything else, such as 17.x, latest or an unresolved ${java.version}, fails as unrecognized, and so does a minimum in an unrecognized format.
Minimum per feature release
A single minimum is one floor for every feature release: 21.0.13.1 passes any 25 or 27 build, including a vulnerable 25.0.5. To hold each release to its own fixed update, list one minimum per feature release, separated by commas, spaces or newlines:
with:
min_java_version: "21.0.13.1, 25.0.5.1, 27.0.1.1"
min_java_version_cicd: "21.0.13.1, 25.0.5.1, 27.0.1.1"
| Version | Result |
|---|---|
21.0.13.1, 25.0.6, 27.0.1.1 |
Passes: meets its release's minimum |
21.0.13, 25.0.5 |
Fails: below its release's minimum |
11.0.29, 17.0.21.1 |
Fails: older than every listed release |
23.0.2, 26.0.1 |
Fails: no minimum is listed for its release, and a newer release is listed |
28, 28.0.1 |
Passes: newer than every listed release, as with a single minimum |
A bare release such as 23 accepts every update of it, so 21.0.13.1, 22, 23, 24, 25.0.5.1 requires Java 21 or newer and a fixed update only on 21 and 25.
A 21 build target takes its update from CI as described above, and every Java version recorded in CI is held to the minimum for its own release.
The check sees only the version java -version reports, not the vendor. Corretto, Zulu and Microsoft builds of the same update all report 21.0.13.1, and build numbers such as +2 are ignored. IBM Semeru reports its 21.0.13 builds as 21.0.13.0.x, which sorts below 21.0.13.1. To accept a vendor's own fix version, split the components that use it into a separate policy entry with on: and list that version there.
Requiring a Java version from CI
By default min-java-version-cicd skips when CI recorded no java or javac commands, so a project whose CI isn't traced never fails it. With require_java_version_cicd: "true", a Java project fails instead once collection has finished. Until then the check is pending.
- A Java project has a
pom.xml,build.gradleorbuild.gradle.ktsat the component root, which thejavacollector'sprojectsub-collector records as.lang.java.project_exists. Detection goes by build file, not source language, so a Kotlin-only Gradle build counts: Gradle runs on a JDK, and traced CI records it. - Build files only in subdirectories aren't detected, for example
services/*/pom.xmlwith no rootpom.xml, or a Gradle build with onlysettings.gradleat the root. Those components skip when nothing is recorded, as they do without the input. - Recorded Java is checked the same way with or without the input, and with or without a build file.
- It needs the
javacollector'sprojectandcicdsub-collectors, and the Lunar CI/CD Tracer on the runners that build the component. Withoutproject, no project is detected and the check skips; withoutcicdor the tracer, every detected project fails. Useon:to scope the policy to components whose CI is traced, or to leave out projects such as Kotlin-only builds.
collectors:
- uses: github://earthly/lunar-lib/collectors/java@v1.0.0
on: ["domain:your-domain"] # replace with your own domain or tags
include: [project, cicd]
policies:
- uses: github://earthly/lunar-lib/policies/java@v1.0.0
on: ["domain:your-domain"]
enforcement: report-pr
include: [min-java-version-cicd]
with:
min_java_version_cicd: "21.0.13.1, 25.0.5.1"
require_java_version_cicd: "true"
Runtime base images
min-java-version-base-image reads the image each Dockerfile's final stage builds on, following FROM <stage> references, from the docker collector. Builder stages such as FROM maven:3.9-eclipse-temurin-21 AS build are not the runtime and aren't checked; the JDK that builds the code is min-java-version-cicd's job.
The image is checked only when it matches a Java image family: a repository pattern plus the scheme that reads the Java version from the image.
temurinreads the tag, with Temurin's build number:eclipse-temurin:21.0.5_11-jreis 21.0.5+11.versionreads a tag that starts with the version:amazoncorretto:21.0.5-al2023is 21.0.5.chainguardreads anopenjdk-<version>tag:cgr.dev/acme/jre:openjdk-21.0.5is 21.0.5.distroreads only the major in the repository name, since the tag is an image release:gcr.io/distroless/java21-debian12is 21.
The built-in list (defaults) is */eclipse-temurin (temurin), */amazoncorretto, */openjdk and */azul/zulu-openjdk* (version), cgr.dev/*/jre and cgr.dev/*/jdk (chainguard), and */distroless/java* and */ubi*/openjdk-* (distro). A pattern without a registry is a Docker Hub repository, * matches across / so */eclipse-temurin also covers mirrors that keep the name, and the first match wins. List your own images and renamed mirrors before defaults:
with:
min_java_version_base_image: "21.0.5"
java_base_image_families: |
123456789012.dkr.ecr.*.amazonaws.com/platform/java-runtime version
defaults
What a result means:
- Fail: the image names a Java version below the minimum, either a pinned update (
21.0.3_9-jre) or an older release (17-jreagainst21.0.13.1). - Pass: the version the Dockerfile names meets the minimum. That is what the Dockerfile asks for, not proof of what the built image contains.
- Skip as unknown: a floating or major-only tag (
21-jre,latest), a distro image (java21), a digest with no tag, or an ARG with no default. None of these shows an update, so they never pass a minimum that needs one. A release below or above every listed minimum is still decided. - Skip: no Dockerfile's runtime stage builds on a listed family, for example Java installed with
apt-getontodebian.
It doesn't see what ships. ARG defaults are read as written, so a --build-arg override is missed; a CI build that uses --target picks another stage; a floating tag resolves when the image is built; and distro and Chainguard packages carry vendor backports their tags don't show. Nor does a vulnerability scan of the released image fill the gap: .container_scan records findings, not the Java version, so a runtime with no matched CVE leaves no trace.
Required Data
This policy reads from the following Component JSON paths:
| Path | Type | Provided By |
|---|---|---|
.lang.java |
object | java collector |
.lang.java.project_exists |
boolean | java collector |
.lang.java.version |
string | java collector |
.lang.java.build_systems |
array | java collector |
.lang.java.mvnw_exists |
boolean | java collector |
.lang.java.gradlew_exists |
boolean | java collector |
.lang.java.cicd.cmds |
array | java collector |
.lang.java.maven.cicd.cmds |
array | java collector |
.lang.java.gradle.cicd.cmds |
array | java collector |
.lang.java.tests.scope |
string | java collector |
.containers.definitions |
array | docker collector |
Installation
Add to your lunar-config.yml:
policies:
- uses: github://earthly/lunar-lib/policies/java@v1.0.0
on: ["domain:your-domain"] # replace with your own domain or tags
enforcement: report-pr
include:
- build-tool-wrapper-exists
- min-java-version
- min-java-version-cicd
- min-java-version-base-image
- min-maven-version
- min-gradle-version
- tests-all-modules
with:
min_java_version: "17"
min_java_version_cicd: "17"
min_java_version_base_image: "17"
min_maven_version: "3.9.0"
min_gradle_version: "8.0.0"
Examples
Passing Example
{
"lang": {
"java": {
"version": "21",
"cicd": {
"cmds": [{ "cmd": "java -jar app.jar", "version": "21.0.5" }]
},
"build_systems": ["maven"],
"mvnw_exists": true,
"maven": {
"cicd": {
"cmds": [{ "cmd": "mvn clean install", "version": "3.9.6" }]
}
},
"tests": { "scope": "all" }
}
}
}
Failing Example
{
"lang": {
"java": {
"version": "11",
"cicd": {
"cmds": [{ "cmd": "java -jar app.jar", "version": "11.0.25" }]
},
"build_systems": ["maven"],
"mvnw_exists": false
}
}
}
Failure messages:
"Missing build tool wrapper(s): mvnw (Maven wrapper)""Java version 11 is below minimum 17. Update your build config to target Java 17 or newer.""CI ran Java that does not meet minimum 17""Java 11.0.25 is below minimum 17 (command 'java -jar app.jar')"
Remediation
build-tool-wrapper-exists
- Maven: Run
mvn wrapper:wrapperto generate the Maven wrapper (mvnw) - Gradle: Run
gradle wrapperto generate the Gradle wrapper (gradlew)
min-java-version
- Maven: Update
<java.version>or<maven.compiler.source>in pom.xml - Gradle: Update
sourceCompatibilityor the toolchain version in build.gradle - Minimum with an update, such as
21.0.13.1: keep the build file on the feature release and run CI on a JDK at or above the minimum, with the Lunar CI/CD Tracer installed so thejavacollector records it - Unrecognized version: Use a plain version such as
17,21.0.5or1.8(see Version comparison)
min-java-version-cicd
- Upgrade the JDK your CI runs, e.g.
java-versioninactions/setup-javaor the build image's base, to the minimum or newer - No Java version recorded: the command's
java -versionorjavac -versionprinted no version line, often because the JVM failed to start - No Java version was recorded in CI for this Java project (with
require_java_version_cicd): run its CI build on runners with the Lunar CI/CD Tracer and enable thejavacollector'scicdsub-collector; see Requiring a Java version from CI - No listed minimum: the JDK is on a feature release the minimum skips, such as 23 between 21 and 25; move to a listed release
min-java-version-base-image
- Build the runtime stage on an image whose tag names an update at or above the minimum, such as
eclipse-temurin:21.0.5_11-jre, ideally pinned by digest too - Unknown: replace a floating or major-only tag with a versioned one, or list your own base image as a family
- No listed minimum: move the runtime to a listed feature release
min-maven-version / min-gradle-version
- Update your CI/CD pipeline or build tool wrapper to use a newer version
tests-all-modules
- Remove
-pl/--projectsflags from Maven test commands - Remove
--testsfilters from Gradle test commands
Open Source
This policy is open source and available on GitHub. Contribute improvements, report issues, or fork it for your own use.
Common Use Cases
Explore how individual guardrails work with specific integrations.
Ready to Automate Your Standards?
See how Lunar can turn your AGENTS.md, engineering wiki, compliance docs, or postmortem action items into automated guardrails with our 300+ built-in guardrails.