Question

I have both JDK 1.6 and 1.7 installed on my system (Linux, under the /opt directory). I've added the bin directory for JDK 1.6 in my path, so that's the version of Java used by default.

I'm working on a project that requires JDK 1.7 and some that require 1.6. Previously I had set JDK 1.7 settings within Eclipse, but I wanted to convert this project to Maven so that everyone could use their preferred editor.

Is it possible to specify the 1.7 location on a Maven installation/configuration (and not in the POM file) such that it uses 1.6 by default and 1.7 when specifying the project requires it in the POM? As far as I am aware, everyone working on a project should have the same contents in their POM files, so I am reluctant to set the location of the Java 7 directory here (as it'll be different on everyone's machines).

$ mvn --version
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 13:51:28+0000)
Maven home: /opt/apache-maven-3.0.5
Java version: 1.6.0_45, vendor: Sun Microsystems Inc.
Java home: /opt/jdk1.6.0_45/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "linux", version: "3.8.0-27-generic", arch: "amd64", family: "unix"

Adding the following:

<build>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
            <configuration>
                <outputDirectory>${project.build.outputDirectory}/resources</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

results in:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project golemlite: Fatal error compiling: invalid target release: 1.7 -> [Help 1]
Was it helpful?

Solution

Take a look here. You can set a property in your settings.xml and use it in the pom. Be aware that everybody would have to define that property.

As for your example:

<build>
<plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <fork>true</fork>
        <executable>${JAVA_1_7_HOME}/bin/javac</executable>
    </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
        <configuration>
            <outputDirectory>${project.build.outputDirectory}/resources</outputDirectory>
        </configuration>
    </plugin>
</plugins>
</build>

and in the settings:

<settings>
  [...]
  <profiles>
    [...]
    <profile>
      <id>compiler</id>
        <properties>
          <JAVA_1_7_HOME>/path/to/jdk7</JAVA_1_7_HOME>
        </properties>
    </profile>
  </profiles>
  [...]
  <activeProfiles>
    <activeProfile>compiler</activeProfile>
  </activeProfiles>
</settings>

OTHER TIPS

You can place a file named mvn.sh near your pom.xml in which you set JAVA_HOME to anything you want. Then, build project like ./mvn clean install. And be sure not to check it to VCS.

Maven uses whatever version is in your JAVA_HOME system variable. So I like to add to my .bash_profile functions to swap between them just in my current shell, so you could leave Java 6 by default but allow yourself to trivially switch to Java 7 or even 8 when you need to build a newer project.

Gist to Change Java Versions on Demand

If your terminal is not bash then you may need to tweak this solution.

Personally I prefer this to setting it in your settings.xml even though it is slightly more work as it allows you to use the same solution across all build tools and even just for running jars built targeting other versions of Java.

So a solution for you might be adding something like this to your .bash_profile (clearly tweaking the paths appropriately to where ever the Java versions are installed on your machine):

function java6
{
  JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0_65-b14-462.jdk/Contents/Home
  export JAVA_HOME
}

function java7
{
  JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home
  export JAVA_HOME
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top