I am trying to compile Java 11 code using maven-compiler-plugin 2.2, and it gives the following error:
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
\projects\testoldmaven\src\main\java\Main.java:[10,14] error: cannot find symbol
\projects\testoldmaven\src\main\java\Main.java:[11,8] error: cannot find symbol
I wonder if the reason is that either that I am doing something wrong or the problem is general and Maven 2 does not support Java 11 at all. Or are there maybe any workarounds that could help me compile Java 11 code using Maven 2? I have tried to search for any reliable article that could directly state that Maven 2 does not support Java, or any minimal maven version requirement to run Java 11 code, or any maximum Java version which could be executed with maven-compler-plugin 2.x, but was not able to find anything helpful.
The code itself is just simple test with some Java 11 specific fragments:
public static void main(String[] args) {
Optional<Integer> value = Optional.empty();
final var id = 123;
var text = "This is the test of var & repeat\r\n";
System.out.println(text.repeat(2));
System.out.println(id * text.lines().count());
}
My pom.xml looks like this:
<groupId>1.0</groupId>
<artifactId>test-old-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.2</version>
<!--<version>3.6.1</version>-->
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.release>11</maven.compiler.release>
</properties>
If anyone could shed some light on my problem, that would be great. Thanks for any help in advance.
By setting <maven.compiler.source>1.8</maven.compiler.source>
you are essentially passing -source 1.8
to the javac
compiler.
That is telling the compiler that you want to explicitly limit the input source code to Java 1.8 syntax and language. As you are using language constructs not defined in Java 8 (e.g. var
), it is going to fail.
As you are specifying release
as well, it appears that the value for source
is taking precedence.