Configure Maven to use different JDK for different J2SE versions?
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 d开发者_如何学Goifferent <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.
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>
You can tell the maven-compiler-plugin to Compile Sources Using A Different JDK
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<executable><!-- path-to-javac --></executable>
</configuration>
</plugin>
From the numerous upvotes on @lweller's answer I guess it's wierd, but with 1.7
as source
and target
maven still tried to compile using java 1.5
. Rather only with 7
... Like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source> <!-- see here, says only 7, not 1.7 -->
<target>7</target> <!-- here as well -->
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<executable>${env.JAVA_HOME_7}/bin/javac</executable>
<fork>true</fork>
</configuration>
</plugin>
maven-compiler-plugin version 2.5.1.
精彩评论