Set default jdk for maven-compiler-plugin

I just installed maven on the new ubuntu system, which includes the maven-compiler plugin. I have a java project that previously built fine, the default was javac source and target 5 (jdk 1.5). However, the project is currently trying to compile using jdk1.3 on the new system. Is there an easy way to configure the system to use> = jdk5?

Here are some system configuration details:

$ 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 <ubuntu-devel-discuss@lists.ubuntu.com> 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 <ubuntu-devel-discuss@lists.ubuntu.com> Architecture: all Source: maven-compiler-plugin Version: 2.0.2-6 

I checked the maven-compiler-plugin-2.0.2.pom and plexus-compiler-javac.originalVersion file, and others - 1.5.3.

I know that I can set this for each project by including the source / target tag in the context of the plugin, but I would like to configure maven-compiler by default on jdk5 or higher without doing this through a large number of projects.

How can i do this?

+8
java maven
source share
6 answers

The default value for the source and target was 1.3 in older versions of the maven-compiler plugin (e.g. 2.0.2-6). Use at least the version of the Maven plugin for version 3.0 to return it to its original behavior or simply configure this plugin to get the source and target values ​​for the corresponding values.

+1
source share

In your memory, specify the following to install the compiler in 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 point mine to dependencies, although as long as you can place part of the project element anywhere inside pom.

I ran into a similar problem with Maven earlier, this fixed it for me. In essence, this means that the -source and -source flags match the specified value and pass it to the compiler. New plugins default to 1.5.

To use the default approach without specifying properties, you will need to run a later version of Maven.

I suppose you can also customize the template through your IDE to include it in all new pom files. Of course, the actual implementation will depend on your IDE ...

See the documentation for the apache maven compiler plugins , as well as Configuring sources and compiler examples for more details.

+17
source share

I tried the maven-compiler-plugin approach and it turned out to be cumbersome as there are plugins like maven-surefire-plugin and maven-cobertura-plugin which still do not work due to incompatibility issues.

The best approach was to use the 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 the maven-toolchain-plugin to the plugins in your pom.xml project.

* If you are using maven 3, make sure this also applies to pluginManagement *

  <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 correct JDK . Hope it helps. Today I spent almost half a day on this exact issue.

+2
source share

I used the following settings to install the maven java compiler version by default.

first, change 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 the eclipse settings make the java home point in jdk home

+1
source share

Suggestion: Use the latest maven compiler plugin.

To change the default values, you must set the 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> 

Additional info: maven-compiler-plugin

0
source share

The simplest solution to such issues is to use the updated version of Maven (3.1.1) and, in particular, create the parent pom.xml file for all your projects, where you determine the configuration and version of your maven compiler-plugin through pluginManagement or better all your plugins.

0
source share

All Articles