Specify JDK for Maven to use

2020-01-25 04:16发布

I am trying to build a Hudson plugin I've modified and it requires jdk1.6. This is fine, but I don't see how I can tell maven where the different jdk is. I've found few mentions on the internet but they don't seem to apply to me. Some suggest adding some config to .m2/settings.xml but I don't have a settings.xml. Plus, I don't want to use 1.6 for all maven builds.

One kink is I am using mvn in cygwin, if that matters at all. It appears I should be able to make the specification in the project pom file, but the existing pom is pretty bare.

So bottom line is, is there a way to specify a jdk for a single invocation of maven?

11条回答
对你真心纯属浪费
2楼-- · 2020-01-25 04:28

Maven uses variable $JAVACMD as the final java command, set it to where the java executable is will switch maven to different JDK.

查看更多
乱世女痞
3楼-- · 2020-01-25 04:28

For Java 9 :

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>9</source>
                <target>9</target>
            </configuration>
        </plugin>
    </plugins>
</build>
查看更多
看我几分像从前
4楼-- · 2020-01-25 04:32

As u said "Plus, I don't want to use 1.6 for all maven builds."....So better I will say modify your pom file and specify which jdk version to use.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.9</source>
                <target>1.9</target>
            </configuration>
        </plugin>
    </plugins>
</build>

It will ensure that your particular project uses that version of jdk.

查看更多
时光不老,我们不散
5楼-- · 2020-01-25 04:32

I say you setup JAVA_HOME like Pascal is saying: In cygwin if you use bash as your shell should be "export JAVA_HOME=/cygdrive/c/pathtothejdk" And it never harms to also export the java bin dir to the PATH with "export PATH=${JAVA_HOME}/bin:${PATH}"

and also add maven-enforce-plugin to make sure the right jdk is used. This is a good practice for your pom.

<build>
 <plugins>
   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <executions>
        <execution>
          <id>enforce-versions</id>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireJavaVersion>
                <version>1.6</version>
              </requireJavaVersion>
            </rules>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

http://maven.apache.org/plugins/maven-enforcer-plugin/usage.html

查看更多
男人必须洒脱
6楼-- · 2020-01-25 04:35

I had build problem with maven within Eclipse on Windows 7.

Though I observed mvn build was running just fine from command line.

mvn -T 5 -B -e -X -U -P test clean install -Dmaven.surefire.debug  --settings ..\..\infra-scripts\maven-conf\settings.xml   > output.log

Eclipse was considering as default JVM a JRE installation instead of JDK so it was failing on compilation.

I added to eclipse.ini following line:

-vm
C:\Program Files (x86)\Java\jdk1.8.0_25\bin

Also when starting from eclipse I used in "Goals" section following list:

-T 5 -B -e -X -U -P test clean install -Dmaven.surefire.debug  --settings ..\..\infra-scripts\maven-conf\settings.xml

Compilation error got solved.

查看更多
▲ chillily
7楼-- · 2020-01-25 04:40

Yet another alternative to manage multiple jdk versions is jEnv

After installation, you can simply change java version "locally" i.e. for a specific project directory by:

jenv local 1.6

This will also make mvn use that version locally, when you enable the mvn plugin:

jenv enable-plugin maven
查看更多
登录 后发表回答