Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set the default jdk for maven-compiler-plugin

Tags:

java

maven

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?

like image 777
Brett Avatar asked Oct 24 '13 16:10

Brett


People also ask

How do I change my default Maven compiler?

Create a pom-only ( <packaging>pom</packaging> ) project that has the compiler settings (and any other default settings) you want. You give treat it like any other project (release it; deploy it to your Maven repo, etc.). It doesn't help much if all you want to set is compiler settings.

Which JDK does Maven use?

The Maven tool uses JDK version 11.0. 10. The default JDK is set to 13.0.


1 Answers

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.

like image 158
Robert H Avatar answered Sep 20 '22 19:09

Robert H