How do you specify the Java compiler version in a pom.xml file?
Asked Answered
S

5

262

I wrote some Maven code in Netbeans that has approximately more than 2000 lines. When I compile it on Netbeans, everything is fine, but if I want to run it on command line, I will get these errors:

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
        ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
        HashSet<Double> resid_List = new HashSet<Double>(Arrays.asList(resid_val));

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
        List<Integer> ind_ovlpList = new ArrayList<Integer>(Arrays.asList(ind_ovlp));

generics are not supported in -source 1.3
(use -source 5 or higher to enable generics)
public class ColumnComparator implements Comparator<double[]> {

annotations are not supported in -source 1.3
(use -source 5 or higher to enable annotations)
@Override

I tried to use Java 1.3.1, compiler errors, but I got more errors. I found from other posts that I should modify pom.xml, but I do not know how. Here is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany</groupId>
  <artifactId>mavenmain</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>mavenmain</name>
    <url>http://maven.apache.org</url>

   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

   <dependencies>
     <dependency>
       <groupId>junit</groupId>
        <artifactId>junit</artifactId>
       <version>3.8.1</version>
       <scope>test</scope>
     </dependency>
     <dependency>
        <groupId>gov.nist.math</groupId>
        <artifactId>jama</artifactId>
        <version>1.0.2</version>
     </dependency>
   </dependencies>
 </project>

It would be great if you can help me!

Steffy answered 23/5, 2013 at 20:42 Comment(5)
Time to upgrade java version.Billowy
Generics are supported from Java 5. Impossible to make them work before that Java version.Culbert
The default for OpenJDK javac is 1.3, where for Oracle JDK it is 1.5Jota
Both of which predate me being a programmer!Sym
Related: Specifying java version in maven - differences between properties and compiler pluginCythera
R
312
<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>(whatever version is current)</version>
        <configuration>
          <!-- or whatever version you use -->
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

See the config page for the maven compiler plugin:

http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

Oh, and: don't use Java 1.3.x, current versions are Java 11 or 17.

Ripsaw answered 23/5, 2013 at 20:44 Comment(12)
You also might want to add a check on the JDK version you are using to run Maven with the Maven Enforcer Plugin: maven.apache.org/enforcer/maven-enforcer-plugin I find this very convenient as I have to support different projects that are on different JDK versions. It avoids that you compile with Java 8 on a Java 7 project for example.Coucher
The plugin version is missing. It wouldn't throw an error but it's strongly recommended, to set the version there. The current version is 3.3Mishamishaan
@LukasWerner you're right of course. but if I add a version I have to edit the question every few months. went for a compromise :-)Ripsaw
PLEASE clarify... What goes into this tag <version>(whatever version is current)</version>? Is it something like 1.7?Anaemic
@Elijah no, that would be the java language version. I am talking about the plugin version. you can find that through this link: mvnrepository.com/artifact/org.apache.maven.plugins/…Ripsaw
To add to this, the plugin version seems to be really important! I thought I could just leave it out and maven would try to find the latest version, but it doesn't and I kept getting the error about the source being 1.3 (using maven 2.2.1).Serrato
@Serrato a) you should no longer be using Maven 2.x, it's end-of-life. b) with no explicit versions, maven falls back to the versions defined in the implicit global super pom.Ripsaw
@SeanPatrickFloyd I would like to stop using Maven 2, but unfortunately, that's what I have to live with until our project can be moved to Maven 3. Not that moving to 3 is difficult, and is on our tech debt list, but unfortunately, I don't control the priorities. :( Edit to add: though maybe this would be a good time to revisit!Serrato
Thank you! To get it working I also had to enable "Use plugin registry" in Settings->Build, Execution, Deployment->Maven.Credits
why is <compilerVersion>1.7</compilerVersion> not working as with source and target tags?Racemose
Update: See Answer by davidxxx for the modern replacement of source and target with release in Maven 3.6 and later.Cythera
https://mcmap.net/q/102002/-how-do-you-specify-the-java-compiler-version-in-a-pom-xml-file is preferred way, for compilers starting from JDK9 specifying maven.compiler.release property as well is recommendedGild
S
379

maven-compiler-plugin it's already present in plugins hierarchy dependency in pom.xml. Check in Effective POM.

For short you can use properties like this:

<properties>
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
</properties>

I'm using Maven 3.2.5.

Surgeon answered 28/7, 2015 at 11:54 Comment(6)
Which way is "best"? This one is less verbose compared to the selected answer, but it seems sort of hidden. Even the Maven site documentation shows using the plugin.Sydel
@Sydel I guess this is a personal choice. If I need to configure something else besides the compiler and source version I prefer to do that in plugins section.Surgeon
Good point with the "configure something else". If I want to use something like google/error-prone, I would have to use the plugins section way.Sydel
This is now the first method on the Maven documentation page that @Sydel gave. Much nicer, and you don't have to pin the version of the compiler plugin.Commandeer
This worked for me after I recreated the project in IntelliJ. I had to reopen the project in IntelliJ by opening the project's pom file as a new project which I am assuming recreated the project.Gearbox
Update: See Answer by davidxxx for the modern replacement of source and target with release in Maven 3.6 and later.Cythera
R
312
<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>(whatever version is current)</version>
        <configuration>
          <!-- or whatever version you use -->
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

See the config page for the maven compiler plugin:

http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

Oh, and: don't use Java 1.3.x, current versions are Java 11 or 17.

Ripsaw answered 23/5, 2013 at 20:44 Comment(12)
You also might want to add a check on the JDK version you are using to run Maven with the Maven Enforcer Plugin: maven.apache.org/enforcer/maven-enforcer-plugin I find this very convenient as I have to support different projects that are on different JDK versions. It avoids that you compile with Java 8 on a Java 7 project for example.Coucher
The plugin version is missing. It wouldn't throw an error but it's strongly recommended, to set the version there. The current version is 3.3Mishamishaan
@LukasWerner you're right of course. but if I add a version I have to edit the question every few months. went for a compromise :-)Ripsaw
PLEASE clarify... What goes into this tag <version>(whatever version is current)</version>? Is it something like 1.7?Anaemic
@Elijah no, that would be the java language version. I am talking about the plugin version. you can find that through this link: mvnrepository.com/artifact/org.apache.maven.plugins/…Ripsaw
To add to this, the plugin version seems to be really important! I thought I could just leave it out and maven would try to find the latest version, but it doesn't and I kept getting the error about the source being 1.3 (using maven 2.2.1).Serrato
@Serrato a) you should no longer be using Maven 2.x, it's end-of-life. b) with no explicit versions, maven falls back to the versions defined in the implicit global super pom.Ripsaw
@SeanPatrickFloyd I would like to stop using Maven 2, but unfortunately, that's what I have to live with until our project can be moved to Maven 3. Not that moving to 3 is difficult, and is on our tech debt list, but unfortunately, I don't control the priorities. :( Edit to add: though maybe this would be a good time to revisit!Serrato
Thank you! To get it working I also had to enable "Use plugin registry" in Settings->Build, Execution, Deployment->Maven.Credits
why is <compilerVersion>1.7</compilerVersion> not working as with source and target tags?Racemose
Update: See Answer by davidxxx for the modern replacement of source and target with release in Maven 3.6 and later.Cythera
https://mcmap.net/q/102002/-how-do-you-specify-the-java-compiler-version-in-a-pom-xml-file is preferred way, for compilers starting from JDK9 specifying maven.compiler.release property as well is recommendedGild
H
22

Generally you don't want to value only the source version (javac -source 1.8 for example) but you want to value both the source and the target version (javac -source 1.8 -target 1.8 for example).
Note that from Java 9, you have a way to convey both information and in a more robust way for cross-compilation compatibility (javac -release 9).
Maven that wraps the javac command provides multiple ways to convey all these JVM standard options.

How to specify the JDK version?

Using maven-compiler-plugin or maven.compiler.source/maven.compiler.target properties to specify the source and the target are equivalent.

<plugins>
    <plugin>    
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>

and

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

are equivalent according to the Maven documentation of the compiler plugin since the <source> and the <target> elements in the compiler configuration use the properties maven.compiler.source and maven.compiler.target if they are defined.

source

The -source argument for the Java compiler.
Default value is: 1.6.
User property is: maven.compiler.source.

target

The -target argument for the Java compiler.
Default value is: 1.6.
User property is: maven.compiler.target.

About the default values for source and target, note that since the 3.8.0 of the maven compiler, the default values have changed from 1.5 to 1.6.

<release> tag — new way to specify Java version in maven-compiler-plugin 3.6

You can use the release argument :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>9</release>
    </configuration>
</plugin>

You could also declare just the user property maven.compiler.release:

<properties>
    <maven.compiler.release>9</maven.compiler.release>
</properties>

But at this time the last one will not be enough as the maven-compiler-plugin default version you use doesn't rely on a recent enough version.

The Maven release argument conveys release to the Java compiler to access the JVM standard option newly added to Java 9, JEP 247: Compile for Older Platform Versions.

Compiles against the public, supported and documented API for a specific VM version.

This way provides a standard way to specify the same version for the source, the target and the bootstrap JVM options.
Note that specifying the bootstrap is a good practice for cross compilations and it will not hurt if you don't make cross compilations either.

Which is the best way to specify the JDK version?

Java 8 and below

Neither maven.compiler.source/maven.compiler.target properties or using the maven-compiler-plugin is better. It changes nothing in the facts since finally the two ways rely on the same properties and the same mechanism : the maven core compiler plugin.

Well, if you don't need to specify other properties or behavior than Java versions in the compiler plugin, using this way makes more sense as this is more concise:

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Java 9 and later

The release argument (third point) is a way to strongly consider if you want to use the same version for the source and the target.

Homogenize answered 30/8, 2018 at 19:11 Comment(0)
G
11

I faced same issue in eclipse neon simple maven java project

But I add below details inside pom.xml file

   <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

After right click on project > maven > update project (checked force update)

Its resolve me to display error on project

Hope it's will helpful

Thansk

Goldenrod answered 3/5, 2017 at 7:19 Comment(1)
This worked for me when I was trying to build the SqlServerSample app for the Microsoft JDBC Driver for SQL Server.Plataea
H
1
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <fork>true</fork>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin> 
Howl answered 23/4, 2019 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.