How to specify Java version in Spring/Spring Boot pom.xml?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In a Maven-based Spring or Spring Boot project, the Java version should be declared in pom.xml so compilation is consistent across local machines, CI, and packaging. In modern Maven builds, the cleanest approach is usually to set the intended Java version in properties and compile with the Maven compiler plugin using release.
The important idea is that build metadata and the real installed JDK must agree. Declaring a Java version in pom.xml expresses intent, but Maven still runs on the JDK that is actually installed in the environment.
The Common Spring Boot Convention
In Spring Boot projects, it is common to declare a java.version property.
This makes the intended baseline obvious and is commonly used by Boot-oriented build conventions.
But the property alone is not the whole story. The actual compiler behavior still depends on the Maven compiler plugin settings.
Use the Maven Compiler Plugin with release
A modern Maven setup usually prefers release over separate source and target values.
Or explicitly inside the plugin:
Using release is usually clearer because it ties compilation to the intended Java platform level more directly than just source-syntax and bytecode-target settings.
A Practical Spring Boot Example
The same overall pattern works even if the exact Boot version or Java baseline is different in your project.
Why release Is Usually Better Than Only source and target
Older examples often show:
That can work, but release is generally the clearer modern setting because it expresses "compile against this Java platform level" in one place.
If you are maintaining an older build, you may still see source and target. For new or cleaned-up builds, release is usually the better default.
The Installed JDK Still Matters
A correct pom.xml does not install or switch Java for you. Maven will still run using the JDK available in your shell, CI agent, or toolchain configuration.
So in practice, all of these must line up:
- the Java version declared in
pom.xml - the JDK Maven is actually using
- the Java version supported by your Spring or Spring Boot line
If they do not agree, the XML may look correct while the build still fails.
Verify the Effective Environment
When something seems inconsistent, check what Maven is really using.
Those commands are often more informative than staring at the pom.xml, because they show the actual JDK behind the build.
Toolchains Are the Next Step for Strict Environments
If you need stricter control across machines, Maven Toolchains can help ensure the build uses the intended JDK instead of whatever happens to be on the shell path. That is especially useful in larger teams and CI environments.
But even without toolchains, the first step is still the same: declare the version clearly and keep the environment aligned with it.
Common Pitfalls
The biggest mistake is setting <java.version> and assuming that alone controls compilation even when the compiler plugin is not aligned. Another is copying old source and target examples without considering whether release is the cleaner modern choice. Developers also often declare one Java version in pom.xml while CI is actually running Maven on a different JDK. Finally, Spring or Spring Boot compatibility still matters, so the selected Java version must match the framework line you are actually using.
Summary
- Declare the intended Java version in
pom.xmlproperties. - Use the Maven compiler plugin with
releasefor the actual compilation target. - Keep the declared Java version and the installed JDK aligned.
- '
releaseis usually preferable to onlysourceandtarget.' - Verify the real build environment with
mvn -versionwhen behavior seems inconsistent.

