Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure Maven to use JDK6 by default, but JDK7 as needed

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]
like image 802
ataulm Avatar asked Aug 08 '13 12:08

ataulm


1 Answers

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>
like image 50
André Stannek Avatar answered Oct 11 '22 11:10

André Stannek