I’m using Maven 3.2.3. I have a multi-module project and currently have my Maven compiler plugin set as such
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-proc:none</compilerArgument>
<fork>true</fork>
<!-- <debug>true</debug> <debuglevel>none</debuglevel> -->
</configuration>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
What I am wondering is how can I set the compiler version based on the $JAVA_HOME variable in my system, meaning, if $JAVA_HOME points at a Java 6 installation, my Maven compiler will use “1.6” as its source and target. Similarly, if my $JAVA_HOME points at a Java 7 installation, source and target would be “1.7” and so on. If there is no $JAVA_HOME set in the environment, I’d like the compiler to default to 1.6.
Thanks for advice and examples regarding how to set this up.
As Jarrod pointed, solution is usage of profiles. Using example provided from maven docs.
Profiles can be automatically triggered based on the detected state of the build environment. These triggers are specified via an section in the profile itself. Currently, this detection is limited to prefix-matching of the JDK version, the presence of a system property or the value of a system property. Here are some examples.
The following configuration will trigger the profile when the JDK's version starts with "1.4" (eg. "1.4.0_08", "1.4.2_07", "1.4"):
See example below merged with your example and maven doc example.
<profiles>
<profile>
<activation>
<jdk>1.6</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-proc:none</compilerArgument>
<fork>true</fork>
<!-- <debug>true</debug> <debuglevel>none</debuglevel> -->
</configuration>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>