2

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.

Helen Zoykina
  • 31
  • 1
  • 1
  • 4
  • 1
    I do not think this is a duplicate question, as in the article https://stackoverflow.com/questions/49398894/unable-to-compile-simple-java-10-java-11-project-with-maven Maven 3 is used, whereas this question is exactly about Maven 2 – Helen Zoykina Nov 09 '18 at 12:02

1 Answers1

3

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.

  • Thank you for the answer. The reason why I have put rows which refer to Java 8, is that without them I am not even able to compile Java 8 code, and get an error: `[INFO] Compilation failure error: Source option 1.3 is no longer supported. Use 6 or later. error: Target option 1.1 is no longer supported. Use 1.6 or later.` – Helen Zoykina Nov 08 '18 at 14:38
  • What happens if you change source/target to 11? And are you sure that the version of javac being invoked by maven is from the JDK 11? (E.g. does $JAVA_HOME point to JDK 11, and you haven't explicitly overridden that in a maven settings files) – dan.m was user2321368 Nov 08 '18 at 14:50
  • 2
    Thanks for your answer again, the code is compiling with some modified maven properties: ` 1111`. I'll mark your answer as accepted as it helped to find the way to resolve the issue. – Helen Zoykina Nov 08 '18 at 14:54