Home > Net >  How to specify java version when I use mvn package command without change environment path
How to specify java version when I use mvn package command without change environment path

Time:12-21

My environment java version is jdk8,Now I want use Maven command mvn package to compile some project.How to specify java version(like java11) to compile it whitout change environment path,because java version is a dynamic arguments in my process

CodePudding user response:

Compiling Sources Using A Different JDK

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
              <verbose>true</verbose>
              <fork>true</fork>
              <executable><!-- path-to-javac --></executable>
              <compilerVersion>11</compilerVersion>
            </configuration>
          </plugin>
        </plugins>
        [...]
      </build>
      [...]
    </project>

Well the side effect is we have to edit the pom.xml file.

  • We may keep different version of pom.xml file, like pom-java11.xml, pom-java17.xml
  • mvn -f <other pom file> for your task, like mvn -f pom-java11.xml ...

CodePudding user response:

In pom.xml you can define the version of JDK

Like

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>2020.0.1</spring-cloud.version>
</properties>
  • Related