compilation failure for spring boot application with java 9 and maven

前端 未结 3 1030
醉话见心
醉话见心 2020-12-18 11:24

I am trying to build spring-boot application that uses java-9 and would be deployed to heroku. As a build tool I use maven

相关标签:
3条回答
  • 2020-12-18 12:05

    Rightly pointed out by @Robert you need to update your maven-compiler-plugin. To add to it, also as stated in the Maven/Jigsaw+9 over the maven plugins. The minimum compatible version of maven-compiler-plugin with current jdk-9+181 is 3.6.2 which can be used as :

    <!--Minimum compatible version required-->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.2</version>
        <configuration>
            <jdkToolchain>
                <version>9</version>
            </jdkToolchain>
        </configuration>
    </plugin>
    

    And to quote from one of the examples of maven-compiler-plugin with module-info.java here:

    For projects that want to be compatible with older versions of Java (i.e 1.8 or below), but also want to provide a module-info.java for Java 9 projects must be aware that they need to call javac twice: the module-info.java must be compiled with release=9, while the rest of the sources must be compiled with a lower version of source/target.

    which can be achieved using (JAVA_HOME set as 1.8.x) :

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.2</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <configuration>
              <!-- compile everything to ensure module-info contains right entries -->
              <!-- required when JAVA_HOME is JDK 8 or below -->
              <jdkToolchain>
                <version>9</version>
              </jdkToolchain>
              <release>9</release>
            </configuration>
          </execution>
          <execution>
            <id>base-compile</id>
            <goals>
              <goal>compile</goal>
            </goals>
            <!-- recompile everything for target VM except the module-info.java -->
            <configuration>
              <excludes>
                <exclude>module-info.java</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
        <!-- defaults for compile and testCompile -->
        <configuration>
          <!-- jdkToolchain required when JAVA_HOME is JDK 9 or above -->
          <jdkToolchain>
            <version>[1.5,9)</version>
          </jdkToolchain>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
    </plugin>
    

    Few things to note there though are:

    ... you will need at least Maven 3.3.1 to specify a custom jdkToolchain in your plugin configuration


    Or you can also configure the JAVA_HOME as /jdk9/Contents/Home/bin and use the following configuration:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.2</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <configuration>
              <!-- compile everything to ensure module-info contains right entries -->
              <release>9</release>
            </configuration>
          </execution>
          <execution>
            <id>base-compile</id>
            <goals>
              <goal>compile</goal>
            </goals>
            <!-- recompile everything for target VM except the module-info.java -->
            <configuration>
              <excludes>
                <exclude>module-info.java</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
        <!-- defaults for compile and testCompile -->
        <configuration>
          <!-- Only required when JAVA_HOME isn't at least Java 9 and when haven't configured the maven-toolchains-plugin -->
          <jdkToolchain>
            <version>9</version>
          </jdkToolchain>
          <release>6</release>
        </configuration>
    </plugin>
    
    0 讨论(0)
  • 2020-12-18 12:14

    maven-compiler-plugin 3.6.0 is based on the first signature of module-info.class, which has changed a couple of times. It is not compatible with current Java 9 signature. You should use 3.6.2 when using most recent versions of JDK 9.

    0 讨论(0)
  • 2020-12-18 12:23

    Looks like your toolchains.xml does not include an entry for Java 9. Try this:

    <toolchain>
        <type>jdk</type>
        <provides>
            <version>1.9</version>
            <vendor>oracle</vendor>
        </provides>
        <configuration>
            <jdkHome>/path/to/jdk/9</jdkHome>
        </configuration>
    </toolchain>
    
    0 讨论(0)
提交回复
热议问题