2017-08-30 24 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>