All 15 Build Tools: Maven & Gradle questions
More in Build Tools: Maven & Gradle
Advertisement
Back to results for “Java Fundamentals: Syntax”
Java › Build Tools: Maven & Gradle

How do you configure the Maven Compiler Plugin to target a specific Java language version, and what's the difference between source, target, and release configuration?

Intermediate

Answer

The maven-compiler-plugin's <source> and <target> properties independently control the Java language level accepted by the compiler and the bytecode version produced respectively (historically requiring both to be set consistently); the newer <release> property (Java 9+) is preferred since it sets both simultaneously AND ensures the compiler checks against the correct API surface for that specific version (preventing accidental use of APIs only available in a newer JDK than the target, which source/target alone don't fully guard against).
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <release>17</release>  <!-- preferred over separate source/target -->
    </configuration>
</plugin>
Real-world example A team building with JDK 21 installed but targeting Java 17 compatibility for their deployed environment uses <release>17</release>, which correctly rejects any accidental usage of a Java 18+ only API, an error that using only <source>17</source><target>17</target> might not have caught.

Common follow-ups: What error occurs if you compile with a newer JDK using an API not available in your target release, and how does <release> specifically catch this?;What's Gradle's equivalent sourceCompatibility/targetCompatibility versus toolchain configuration?