配置Maven为不同的J2SE版本使用不同的JDK?

Xiè*_*léi 34 maven-2

我想配置Maven2使用sun-java6-jdk构建Java SE 1.6模块,并使用openjdk-7构建Java SE 1.7模块.可能吗?

然后Maven2应该自动选择正确的JDK来在一个命令中构建不同的模块.

例如,它应该是

$ mvn package
Run Code Online (Sandbox Code Playgroud)

代替

$ cd module1
$ update-alternatives ... jdk6 ...
$ mvn package
...
$ cd module2
$ update-alternatives ... jdk7 ...
$ mvn package
Run Code Online (Sandbox Code Playgroud)

PS这是一无所知的pom.xml文件,这些文件已经设置maven-compiler-plugin不同<source>,<target>价值观不同的模块.如果我选择使用openjdk-7,Maven2将生成1.6版本的类文件,但是使用openjdk-7而不是sun-java6-jdk.问题是如何配置Java SE配置文件.

lwe*_*ler 83

我们通过明确地在编译插件的配置中指定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>
Run Code Online (Sandbox Code Playgroud)

并为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>
Run Code Online (Sandbox Code Playgroud)

  • Colin,我认为你的问题是你错过了行<fork> true </ fork>原因是maven正在使用你的JAVA_HOME env变量运行而没有这个编译器会留在JAVA_HOME版本显然忽略你的被覆盖选项. (3认同)
  • 我已经在2年前回答了这个问题,并且由于今天我的回答仍然被接受,所以它对于当今的某些人仍然有用。非常感谢。但老实说,我们现在使用Maven的更强大功能来解决此问题:Maven工具链。这个答案很好地说明了这一点:http://stackoverflow.com/a/12498238/201498 (2认同)

Sea*_*oyd 5

您可以告诉maven-compiler-plugin 使用不同的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>
Run Code Online (Sandbox Code Playgroud)