Skip to content
Latchkey

mvn compile: Usage, Options & Common CI Errors

Compile main sources - the phase where most JDK-version mismatches surface.

compile is the Maven lifecycle phase that compiles the project’s main source code into target/classes using the maven-compiler-plugin. It is where source/target/release version mismatches and missing-symbol errors first appear in CI.

What it does

compile runs the compiler plugin against src/main/java, writing class files to target/classes. It does not compile or run tests (that is test-compile / test). The bytecode level is controlled by maven.compiler.release (preferred) or maven.compiler.source/target.

Common usage

Terminal
mvn compile
mvn -o compile                          # offline
mvn compile -Dmaven.compiler.release=17 # target Java 17 bytecode

Common error in CI (and the fix)

Symptom: "invalid target release: 21" or "Fatal error compiling: error: release version 21 not supported". Cause: the JDK running Maven is older than the requested release. Fix: provision the matching JDK in CI (e.g. actions/setup-java with java-version: 21) so the running JDK is at least the requested release, or lower maven.compiler.release to a version the installed JDK supports.

Options

Flag / propertyEffect
-Dmaven.compiler.release=NCompile against Java N API and bytecode
-Dmaven.compiler.source / .targetLegacy source/target levels
-oOffline; use only the local repository
-Dmaven.compiler.failOnWarning=trueFail the build on compiler warnings

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →