35

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.

Gab是好人
  • 1,976
  • 1
  • 25
  • 39
Lenik
  • 13,946
  • 17
  • 75
  • 103
  • 6
    FYI: there is no such thing as J2SE 1.6. Starting from 1.6, it's called Java SE (Just as J2EE is now Java EE). [Reference](http://weblogs.java.net/blog/kgh/archive/2005/06/goodbye_j2se_he_1.html) – Sean Patrick Floyd Jan 18 '11 at 12:36
  • I like Maven Profiles as it is explained here [enter link description here](https://stackoverflow.com/questions/35209975/maven-compiling-with-different-jdk-versions) – wromma Aug 24 '22 at 08:36

3 Answers3

83

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>
lweller
  • 11,077
  • 3
  • 34
  • 38
  • Thanks, you answered incredible fast, I'm just gonna clarify my question one more step, but you've already give the answer. :) – Lenik Jan 18 '11 at 12:37
  • 1
    Note: For me it seems that if JAVA_HOME is set it uses it no matter what and the section doesn't apply. I had to remove it and actually use JAVA_HOME_6 and JAVA_HOME_7 for it to work – Collin Peters Apr 13 '12 at 04:48
  • What happens if my source and target say 1.6, however my JAVA_HOME points to 1.7? – Kevin Meredith Sep 13 '12 at 19:57
  • 3
    Colin, I think your problem was that you will have missed out the line true The reason is than maven is running using your JAVA_HOME env variable & without this the compiler will stay with the JAVA_HOME version apparently ignoring your overridden option. – Bill Comer Jan 09 '13 at 11:12
  • When I use this, I'm getting compilation errors based on the web subproject not being able to find classes from the core subproject – Marc Feb 20 '13 at 10:36
  • http://stackoverflow.com/questions/2585220/how-to-configure-a-subproject-dependency-in-maven-without-deploying-jars resolved this – Marc Feb 20 '13 at 11:07
  • 2
    I've answered this question 2 years ago and as my answer is still upvoted today, it still seams to be useful for some people nowadays. Thanks a lot. But to be honest we now use a more powerful feature of maven to solve this problem: maven toolchains. This answer explains it very well: http://stackoverflow.com/a/12498238/201498 – lweller Jan 15 '14 at 12:41
  • @KevinMeredith, it doesn't matter. source and target are just options for the javac. the target is still 1.6 if JAVA_HOME points to 1.7, see the documentation about [javac](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html) at Oracle – Gab是好人 Sep 23 '15 at 19:21
5

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>
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
1

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.

yair
  • 8,945
  • 4
  • 31
  • 50
  • 2
    7 is a synonym for 1.7. Take a look at https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html for `-source` and `-target` parameters – freedev Dec 01 '14 at 09:17