Configure Maven to use different JDK for different J2SE versions?

坚强是说给别人听的谎言 提交于 2019-12-17 15:24:48

问题


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.


回答1:


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>



回答2:


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>



回答3:


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.



来源:https://stackoverflow.com/questions/4724000/configure-maven-to-use-different-jdk-for-different-j2se-versions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!