Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Maven to use different JDK for different J2SE versions?

Tags:

maven-2

I want to configure Maven2 to use sun-java6-jdk to build Java SE 1.6 modules, and use openjdk-7 to build Java SE 1.7 modules. Is it possible?

Maven2 should then auto choose the correct JDK to build different modules in one command.

For example, it should be

$ mvn package 

instead of

$ cd module1 $ update-alternatives ... jdk6 ... $ mvn package ... $ cd module2 $ update-alternatives ... jdk7 ... $ mvn package 

P.S. It's nothing about pom.xml files, which have already been setup maven-compiler-plugin with different <source>, <target> values for different modules. If I choose to use openjdk-7, Maven2 will generate version 1.6 class files, but using openjdk-7 rather then sun-java6-jdk. The question is about how to configure Java SE profiles.

like image 227
Xiè Jìléi Avatar asked Jan 18 '11 12:01

Xiè Jìléi


People also ask

How do I set Java version to Maven?

You can set the JAVA_HOME parameter just before you start maven (and change it back afterwards if need be). You could also go into your mvn (non-windows)/ mvn. bat / mvn. cmd (windows) and set your java version explicitly there.

What version of JDK does Maven use?

The Maven tool uses JDK version 11.0. 10. The default JDK is set to 13.0.

Is Maven part of JDK?

During the build of a project, Maven, without toolchains, will use the JDK to perform various steps, like compiling the Java sources, generate the Javadoc, run unit tests or sign JARs. Each of those plugins need a tool of the JDK to operate: javac, javadoc, jarsigner, etc.


Video Answer


1 Answers

we solved this problem by explicitely sepecify the javac in config of compile plugin (with JAVA_HOME_6 and JAVA_HOME_7 defined as environment variables):

and for Java 6 module

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-compiler-plugin</artifactId>     <configuration>         <source>1.6</source>         <target>1.6</target>         <showDeprecation>true</showDeprecation>         <showWarnings>true</showWarnings>         <executable>${env.JAVA_HOME_6}/bin/javac</executable>         <fork>true</fork>     </configuration> </plugin> 

and for Java 7 module

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-compiler-plugin</artifactId>     <configuration>         <source>1.7</source>         <target>1.7</target>         <showDeprecation>true</showDeprecation>         <showWarnings>true</showWarnings>         <executable>${env.JAVA_HOME_7}/bin/javac</executable>         <fork>true</fork>     </configuration> </plugin> 
like image 93
lweller Avatar answered Oct 08 '22 20:10

lweller