Search code examples
javamavenversionpom.xmljava-11

Setting the Java Version in Maven with system Java version as 11.0


I am currently using jdk version as 11 in my local machine, Set using JAVA_HOME.

I need to run a maven project with java_version as 9. for doing the same took reference from https://www.baeldung.com/maven-java-version

here is my pom.xml file

<properties>
    <java.version>8</java.version>
    <maven.compiler.plugin.version>3.8.0</maven.compiler.plugin.version>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <release>8</release>
            </configuration>
        </plugin>
    </plugins>
</build>

Then i ran the command "mvn clean install" and the jar file generated is having java-version as 11 in its manifest file. here is the snippet of Manifest file of the gemnerated jar

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.5.0
Built-By: test
Build-Jdk: 11.0.2

Why it is still picking java version as 11, why not 8?


Solution

  • Setting java.version in the POM <properties> section does not change the Java version used to compile with the maven-compiler-plugin. It is instead used by Spring Boot (see Specifying java version in maven - differences between properties and compiler plugin for a related discussion).

    Your build is still using the JDK 11 installed on your machine (this is reflected by the manifest attribute Build-Jdk: 11.0.2), however, your code is actually compiled with source and target of Java 8. JDK 11 knows how to compile against earlier versions like JDK 8, so your build is working fine as expected.