2017-08-30 19 views
3

我正在尝试构建使用java-9并将部署到herokuspring-boot应用程序。作为构建工具,我使用maven使用java 9和maven编写的弹簧引导应用程序编译失败

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

pom.xml看起来像这样

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.lapots.breed.platform.cloud</groupId> 
    <artifactId>java-cloud-sample</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>java-cloud-sample</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.6.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.9</java.version> 
     <maven.compiler.source>1.9</maven.compiler.source> 
     <maven.compiler.target>1.9</maven.compiler.target> 
     <maven.compiler.release>9</maven.compiler.release> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-devtools</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
       <executions> 
        <execution> 
         <goals> 
          <goal>build-info</goal> 
         </goals> 
         <configuration> 
          <additionalProperties> 
           <encoding.source>${project.build.sourceEncoding}</encoding.source> 
           <encoding.reporting>${project.reporting.outputEncoding}</encoding.reporting> 
           <java.source>${maven.compiler.source}</java.source> 
           <java.target>${maven.compiler.target}</java.target> 
          </additionalProperties> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>3.6.0</version> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-toolchains-plugin</artifactId> 
       <version>1.1</version> 
       <configuration> 
        <toolchains> 
         <jdk> 
          <version>1.9</version> 
          <vendor>oracle</vendor> 
         </jdk> 
        </toolchains> 
       </configuration> 
       <executions> 
        <execution> 
         <goals> 
          <goal>toolchain</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 


</project> 

当我尝试使用mvn clean package项目中,我得到的错误

[ERROR] COMPILATION ERROR : 
[INFO] ------------------------------------------------------------- 
[ERROR] javac: invalid flag: -Xmodule:null 
Usage: javac <options> <source files> 
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 <options> <source files> 
[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 repository

回答

8

Maven的编译器插件3.6.0是基于module-info.class先签字,这已经改变了几次。它与当前的Java 9签名不兼容。在使用最新版本的JDK 9时,您应该使用3.6.2。

+0

哇,可以有这样的问题...我想我应该阅读有关Java 9更多 – lapots

+3

这是使用ea版本时的问题。这不是官方的,但用户想要试验它。因此,不是所有的maven-compiler-plugin 3.6.x都能与Java 9一起工作。我将很快做出3.7.0的标记,以标示第一个正式的Java 9支持版本。与目前相比,没有太大的区别:) –

+0

但是它似乎问题本身'testCompile'不是'raw'编译任务 – lapots

1

看起来你toolchains.xml不包括Java的9条目试试这个:

<toolchain> 
    <type>jdk</type> 
    <provides> 
     <version>1.9</version> 
     <vendor>oracle</vendor> 
    </provides> 
    <configuration> 
     <jdkHome>/path/to/jdk/9</jdkHome> 
    </configuration> 
</toolchain> 
+0

但包括。我在'.m2'文件夹中使用'github'库中的'toolchains.xml'。它似乎在'test'汇编上失败 – lapots

+1

这不应该是原因,它必须是用于上述情况的编译插件。 – nullpointer

2

正确地指出@Robert,您需要更新您的maven-compiler-plugin。要添加到它,也正如Maven/Jigsaw+9所述通过maven插件。的maven-compiler-plugin当前jdk-9+181最低兼容版本是3.6.2它可以作为:

<!--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> 

而且从examples of maven-compiler-plugin with module-info.java这里的一个引用:

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

可使用实现(JAVA_HOME设置为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> 

几件事情要注意,虽然是:

...你将至少需要的Maven 3.3。1指定自定义 jdkToolchain在你的插件配置


或者你也可以配置JAVA_HOME为/ jdk9 /内容/首页/ bin并使用以下配置:

<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>