简体   繁体   中英

Configuring Maven with two versions of Java

I need two versions of Java ie. Java5 and Java6 on the same machine, as some of my projects support Java6 and some supports Java5. So I made it by putting the path of Java6 in JAVA_HOME and Java5 in JAVA5_HOME. I added both the JAVA_HOMEs in the PATH variable.

But when I do java -version, then by default I am getting the following result

java version "1.6.0" Java(TM) SE Runtime Environment (build 1.6.0-b105) Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

Is it possible to get Java5 also along with Java6??

Also I would like to know that, is it possible to configure maven2 with two versions of Java (Java1.6 & Java1.5) and use the required version for building the project in the run time?

One way to achieve this is documented at the maven-compiler-plugin site .

Personally I would first try to use the source and target parameters of the compile mojo ( documented here ), as that would allow you to target both 1.5 and 1.6 without switching to another Java compiler.

So I made it by putting the path of Java6 in JAVA_HOME and Java5 in JAVA5_HOME. I added both the JAVA_HOMEs in the PATH variable.

Not really a good idea (and one of them will be first on the path anyway). You should add JAVA_HOME only and make it point to the JDK you want to use (and switch on demand).

Also I would like to know that, is it possible to configure maven2 with two versions of Java (Java1.6 & Java1.5) and use the required version for building the project in the run time?

No. Maven uses JAVA_HOME, you can only use one JDK at a time.

If you want to build the same project with 2 different JDKs, your best option would be to use a CI engine like Hudson and to setup a build matrix.

I just recently, after seven long years with Maven, learned about toolchains.xml. Maven has it even documented and supports it from 2.0.9 toolchains documentation

So I added a toolchain.xml file to my ~/.m2/ folder with following content:

<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
 <!-- JDK toolchains -->
 <toolchain>
   <type>jdk</type>
   <provides>
     <version>1.8</version>
     <vendor>sun</vendor>
   </provides>
   <configuration>
     <jdkHome>/opt/java8</jdkHome>
   </configuration>
 </toolchain>
 <toolchain>
   <type>jdk</type>
   <provides>
     <version>1.7</version>
     <vendor>sun</vendor>
   </provides>
   <configuration>
     <jdkHome>/opt/java7</jdkHome>
   </configuration>
 </toolchain>
</toolchains>

It allows you to define what different JDKs Maven can use to build the project irrespective of the JDK Maven runs with. Sort of like when you define JDK on project level in IDE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM