我想将Maven2配置为使用sun-java6-jdk来构建JavaSE1.6模块,并使用openjdk-7来构建JavaSE1.7模块。有可能吗?
然后,Maven2应该自动选择正确的JDK,以便在一个命令中构建不同的模块。
例如,它应该是
$ mvn package而不是
$ cd module1
$ update-alternatives ... jdk6 ...
$ mvn package
...
$ cd module2
$ update-alternatives ... jdk7 ...
$ mvn package附注:这与pom.xml文件无关,这些文件已经为不同的模块设置了不同的<source>,<target>值。如果我选择使用openjdk-7,Maven2将生成1.6版的类文件,但使用的是openjdk-7而不是sun-java6-jdk。问题是如何配置Java SE配置文件。
发布于 2011-01-18 20:30:02
我们通过在编译插件的配置中明确指定javac (将JAVA_HOME_6和JAVA_HOME_7定义为环境变量)解决了这个问题:
对于Java 6模块
<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>对于Java 7模块
<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>发布于 2011-01-18 20:30:09
您可以将maven编译器插件告知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>发布于 2013-03-21 16:03:52
从@lweller的答案上获得了大量的好评,我猜这很奇怪,但由于1.7是source,而target maven仍然尝试使用java 1.5进行编译。而不只是7..。如下所示:
<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编译器插件2.5.1版。
https://stackoverflow.com/questions/4724000
复制相似问题