Tom Joe
Tom Joe

Reputation: 127

Maven compiler settings for Java 10 and above

I set compiler version in the maven pom.xml file like this:

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

Which version do I set for Java 10 and above ? Looks lije 10 or 1.10 won't work. Have the tags changed for Java 10 and above ?

public static void main(String[] args)
{
   try (var in = new Scanner(System.in))
   {
      System.out.print("Enter n: ");
      int n = in.nextInt();
      factorial(n);
   }
}

The compiler complains that it cannot resolve symbol var, despite setting version to 10 or 1.10.

Upvotes: 0

Views: 89

Answers (2)

MrsNickalo
MrsNickalo

Reputation: 217

Here is a helpful link: http://tutorials.jenkov.com/maven/java-compiler.html

For Java 8 or earlier, use:

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

For Java 9 or later, use:

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

Upvotes: 2

davidxxx
davidxxx

Reputation: 131326

The official javac documentation provides the information :
https://docs.oracle.com/javase/10/tools/javac.htm#JSWOR627

The value is 10 but you could also simply value the release javac flag from the maven maven.compiler.release property :

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

--release release

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

Supported release targets are 6, 7, 8, 9,and10.

Upvotes: 0

Related Questions