Specifying Java version in maven - differences between properties and compiler plugin
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Specifying the Java version in a Maven project is essential for ensuring that the compiled code is compatible with the target Java runtime environment. Maven provides two primary methods for setting the Java version: through properties in the pom.xml file or by configuring the Maven Compiler Plugin directly. Each method has its nuances and implications for the build process.
Specifying Java Version Using Properties
The simplest way to specify the Java version in a Maven project is by setting properties in the pom.xml file. These properties are maven.compiler.source and maven.compiler.target. The source property specifies the version of Java code that can be used, effectively setting the minimum required JDK version for your project code. Conversely, the target property sets the target version of the Java class files, ensuring compatibility with the specified Java runtime environment. Here is how these properties can be set:
This configuration tells Maven to compile the project code as compatible with Java 8. While this technique is less flexible than configuring the compiler plugin directly, it is simpler and sufficient for most projects.
Specifying Java Version Using the Compiler Plugin
A more flexible approach involves configuring the Maven Compiler Plugin directly in the pom.xml. This method allows for more granular control over the compilation process, including specifying a particular JDK for compilation and additional supported compiler options. Here is a typical configuration:
With this setup, you can not only control the Java version but also include other options such as compilerArgs, where you can pass additional arguments to the compiler.
Comparison of Both Methods
Here’s a comparison between using properties and the compiler plugin for Java version specification:
| Feature | Properties | Compiler Plugin |
| Ease of Use | Simple to set up | Requires more configuration |
| Flexibility | Less control over options | More control and options |
| Maintenance | Easy to maintain | Slightly more complex |
| Common Use Case | Small to medium-sized projects | Large or complex projects |
| Override Possibility | Low (Depends on parent POM) | High (Specific to module) |
Additional Considerations
Selecting Java Version:
When choosing which Java version to specify, consider the version supported by your runtime environment and any third-party dependencies you are using. Specifying a lower version than necessary can restrict the usage of newer language features, while a higher version requires ensuring that all environments where the code runs support this version.
Modular Java Applications:
For modular Java applications (Java 9 and later), you will also need to configure the maven.compiler.release property or the release parameter in the Maven Compiler Plugin. This ensures that your code only uses APIs available in the specified Java release.
Legacy Systems:
For projects that need to support older Java versions (e.g., Java 6 or 7), check compatible plugin and dependencies versions that support older configurations.
Using the appropriate Java version specification method in Maven is crucial for the successful building and deployment of your Java applications. Each method has its merits, and the choice largely depends on project size, complexity, and specific requirements.

