Reputation: 6020
I have just installed maven on an new ubuntu system, which includes the maven-compiler-plugin. I have a java project that was previously building fine, defaulting to a javac source and target of 5 (jdk 1.5). However, the project is now trying to compile using jdk1.3 on the new system. Is there an easy way to configure the system to use >=jdk5 ?
Here's some of the configuration details of the system:
$ java -version
java version "1.6.0_45"
$ dpkg -s maven
Package: maven
Status: install ok installed
Priority: optional
Section: java
Installed-Size: 1489
Maintainer: Ubuntu Developers <[email protected]>
Architecture: all
Version: 3.0.4-2
$ dpkg -s libmaven-compiler-plugin-java
Package: libmaven-compiler-plugin-java
Status: install ok installed
Priority: optional
Section: java
Installed-Size: 75
Maintainer: Ubuntu Developers <[email protected]>
Architecture: all
Source: maven-compiler-plugin
Version: 2.0.2-6
I've checked the maven-compiler-plugin-2.0.2.pom file, and plexus-compiler-javac.originalVersion and others are set to 1.5.3.
I know I can set this on a per-project basis by including a source/target tag in a plugin context, but I'd like to configure maven-compiler to default to jdk5 or higher without having to do this across a large number of projects.
how can i do this?
Upvotes: 15
Views: 24976
Reputation: 1655
Based on Chinto's answer I got it running on my windows machine with this:
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>openjdk</vendor>
</provides>
<configuration>
<jdkHome>C:\path\to\openJDK1.8.0</jdkHome>
</configuration>
</toolchain>
</toolchains>
Upvotes: 0
Reputation: 1201
I used following settings to set maven default java compiler version.
first, modify maven settings.xml:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
second, in eclipse preferences, make the java home point to jdk home
Upvotes: 1
Reputation: 1516
I tried the maven-compiler-plugin approach and it proved cumbersome as there are plugins like maven-surefire-plugin and maven-cobertura-plugin which still fail due to incompatibility issues.
The better approach was to use maven-toolchain-plugin.
Step 1 Create /.m2/toolchains.xml
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.7</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.6</version>
<vendor>apple</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/1.6.0_65-b14-462.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<!-- other toolchains -->
<!--
<toolchain>
<type>netbeans</type>
<provides>
<version>5.5</version>
</provides>
<configuration>
<installDir>/path/to/netbeans/5.5</installDir>
</configuration>
</toolchain>
-->
Step 2 Add maven-toolchain-plugin to plugins section in your project pom.xml.
*If using maven 3, ensure this goes into pluginManagement as well *
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>1.7</version>
<vendor>sun</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
Voila all your other plugins pick up the right JDK. Hope it helps. I spent almost half day on this exact issue today.
Upvotes: 8
Reputation: 11730
In your pom specify the following to set the compiler to JDK5:
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
</properties>
i.e.
<project>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
...
</project>
I specify mine prior to the dependencies, although so long as its part of the project element you should be able to place it anywhere inside the pom.
I ran into a similar issue with Maven previously, this fixed it for me. Essentially what this does is set the -source
and -target
flags to the value specified and passes it to the compiler. Newer plugins default to 1.5.
In order to use the default approach without specifying the properties, you will need to be running a later version of Maven.
I suppose you could also set up a template via your IDE to include this in all new pom files. Of course the actual implementation would depend upon your IDE...
See The apache maven compiler plugin documentation as well as Setting the source and compiler examples for more details.
Upvotes: 19
Reputation: 97359
The simplest solution to such things is using an up-to-date version of Maven (3.1.1) and in particular create a parent pom.xml file for all your projects where you define the configuration and version of your maven-compiler-plugin via pluginManagement or better all of your plugins.
Upvotes: 0
Reputation: 3516
Suggestion: Use the latest maven compiler plugin.
In order to change the defaults, you should set source and target.
<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>
</configuration>
</plugin>
</plugins>
More Info: maven-compiler-plugin
Upvotes: 0
Reputation: 3354
Default value for source and target was 1.3 in older versions of maven-compiler plugin (like 2.0.2-6). Use at least a 3.0 version of the Maven compiler plugin to get this back to the original behaviour, or just configure that plugin to get source and target to appropriate values.
Upvotes: 1