Specify JDK for Maven to use Specify JDK for Maven to use java java

Specify JDK for Maven to use


So bottom line is, is there a way to specify a jdk for a single invocation of maven?

Temporarily change the value of your JAVA_HOME environment variable.


Seems that maven now gives a solution here : Compiling Sources Using A Different JDK

Let's say your JAVA_HOME points to JDK7 (which will run maven processes)

Your pom.xml could be :

<build>    <plugins>        <!-- we want JDK 1.6 source and binary compatiblility -->        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-compiler-plugin</artifactId>            <configuration>                <source>1.6</source>                <target>1.6</target>            </configuration>        </plugin>        <!-- ... -->        <!-- we want sources to be processed by a specific 1.6 javac -->        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-compiler-plugin</artifactId>            <version>3.1</version>            <configuration>              <verbose>true</verbose>              <fork>true</fork>              <executable>${JAVA_1_6_HOME}/bin/javac</executable>              <compilerVersion>1.3</compilerVersion>            </configuration>        </plugin>    </plugins></build>

If your developpers just add (and customize) the following lines in their settings.xml, your pom will be platform independant :

<settings>  [...]  <profiles>    [...]    <profile>      <id>compiler</id>        <properties>          <JAVA_1_4_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_4_HOME>          <JAVA_1_6_HOME>C:\Program Files\Java\j2sdk1.6.0_18</JAVA_1_6_HOME>        </properties>    </profile>  </profiles>  [...]  <activeProfiles>    <activeProfile>compiler</activeProfile>  </activeProfiles></settings>


compile:compile has a user property that allows you to specify a path to the javac.

Note that this user property only works when fork is true which is false by default.

$ mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable=/path/to/the/javac compile

You might have to double quote the value if it contains spaces.

> mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable="C:\...\javac" compile

See also Maven custom properties precedence.