使用java 9和maven进行Spring启动应用程序的编译失败

我正在尝试构建使用java-9 spring-boot应用程序,并将其部署到heroku 。 作为构建工具,我使用maven

我使用initializr生成了spring boot 1.5应用程序。 我添加了heroku特定文件,并为maven-compiler-plugin添加了toolchains.xml.m2存储库。

我的pom.xml看起来像这样

   4.0.0 com.lapots.breed.platform.cloud java-cloud-sample 0.0.1-SNAPSHOT jar java-cloud-sample Demo project for Spring Boot  org.springframework.boot spring-boot-starter-parent 1.5.6.RELEASE     UTF-8 UTF-8 1.9 1.9 1.9 9    org.springframework.boot spring-boot-starter-actuator   org.springframework.boot spring-boot-starter-web   org.springframework.boot spring-boot-devtools runtime   org.postgresql postgresql runtime   org.springframework.boot spring-boot-starter-test test      org.springframework.boot spring-boot-maven-plugin    build-info    ${project.build.sourceEncoding} ${project.reporting.outputEncoding} ${maven.compiler.source} ${maven.compiler.target}       org.apache.maven.plugins maven-compiler-plugin 3.6.0   org.apache.maven.plugins maven-toolchains-plugin 1.1    1.9 oracle       toolchain        

当我尝试使用mvn clean package编译项目时,我得到了错误

 [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] javac: invalid flag: -Xmodule:null Usage: javac   use --help for a list of possible options [INFO] 1 error [INFO] ------------------------------------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.031 s [INFO] Finished at: 2017-08-30T21:08:25+03:00 [INFO] Final Memory: 20M/309M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.0:testCompile (default-testCompile) on project java-cloud-sample: Compilation failure [ERROR] javac: invalid flag: -Xmodule:null [ERROR] Usage: javac   [ERROR] use --help for a list of possible options [ERROR] [ERROR] [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException 

module-info看起来像这样

 module com.lapots.breed.platform.cloud.javacloudsample { requires spring.boot; } 

问题是什么? 项目可在此处获得github存储库

maven-compiler-plugin 3.6.0基于module-info.class的第一个签名,该签名已经改变了几次。 它与当前的Java 9签名不兼容。 使用最新版本的JDK 9时应使用3.6.2。

@Robert正确指出你需要更新你的maven-compiler-plugin 。 添加到它,也如Maven / Jigsaw + 9中maven插件所述。 maven-compiler-plugin与当前jdk-9 + 181的最小兼容版本是3.6.2 ,可用作:

   org.apache.maven.plugins maven-compiler-plugin 3.6.2   9    

并引用一个带有module-info.java的maven-compiler-plugin示例 :

对于想要与旧版Java(即1.8或更低版本)兼容的项目,但也希望为Java 9项目提供module-info.java,必须注意他们需要调用两次javac :module-info。必须使用release = 9编译java,而其他源必须使用较低版本的源/目标进行编译。

可以使用(JAVA_HOME设置为1.8.x)来实现:

  org.apache.maven.plugins maven-compiler-plugin 3.6.2   default-compile     9  9    base-compile  compile     module-info.java         [1.5,9)  1.5 1.5   

虽然有几点需要注意:

…至少需要Maven 3.3.1才能在插件配置中指定自定义jdkToolchain


或者您也可以将JAVA_HOME配置为/ jdk9 / Contents / Home / bin并使用以下配置:

  org.apache.maven.plugins maven-compiler-plugin 3.6.2   default-compile   9    base-compile  compile     module-info.java         9  6   

看起来您的toolchains.xml不包含Java 9的条目。试试这个:

  jdk  1.9 oracle   /path/to/jdk/9