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
mvn compile
mvn -o compile # offline
mvn compile -Dmaven.compiler.release=17 # target Java 17 bytecodeCommon 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 / property | Effect |
|---|---|
| -Dmaven.compiler.release=N | Compile against Java N API and bytecode |
| -Dmaven.compiler.source / .target | Legacy source/target levels |
| -o | Offline; use only the local repository |
| -Dmaven.compiler.failOnWarning=true | Fail the build on compiler warnings |