mangu
mangu

Reputation: 41

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?

Upvotes: 2

Views: 13604

Answers (3)

sajeeth
sajeeth

Reputation: 77

set fist of all JAVA_HOME then execute the mvn clean install

export JAVA_HOME="C:/Program Files/Java/jdk-11.0.15"

Then excute maven commands

mvn clean install

Note: export command will not work on CMD use gitbash for this command.

Upvotes: 1

M A
M A

Reputation: 72844

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.

Upvotes: 3

Vishwa Ratna
Vishwa Ratna

Reputation: 6390

You can explicitly change the java version on the go for the particular project so that it gets compliled with the version you want.

Use : JAVA_HOME=/path/to/jdk1.8/ mvn build

Beside above you can explicitly set the version in your pom.

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

Upvotes: 3

Related Questions