maven編譯報錯 -source 1.5 中不支持 lambda 表達式


在用maven編譯項目是由於項目中用了jdk 1.8, 編譯是報錯  -source 1.5 中不支持 lambda 表達式,Google找到這篇解決方案,記錄一下:

 

編譯時報如下錯誤:

[ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] AAA\.jenkins\workspace\BBB\CCC.java:[73,46] 錯誤: -source 1.5 中不支持 diamond 運算符 [ERROR] (請使用 -source 7 或更高版本以啟用 diamond 運算符) [ERROR] AAA\.jenkins\workspace\BBB\DDD.java:[38,33] 錯誤: -source 1.5 中不支持 lambda 表達式 [ERROR] (請使用 -source 8 或更高版本以啟用 lambda 表達式)

奇怪的是我的 Jenkins 構建機器上只安裝了 JDK 8,為什么還會說不支持 diamond 和 lambda 呢?在 Google 大神的指引下,在 Maven Compiler 插件介紹 里面找到了答案:Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with.

原來 Maven Compiler 插件默認會加 -source 1.5 及 -target 1.5 參數來編譯(估計是為了兼容一些比較老的 Linux 服務器操作系統,它們通常只有 JDK 5),而我們的代碼里使用了 JDK 7/8 的語法。解決辦法在這里

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

http://blog.csdn.net/kai161/article/details/50379418

Setting the -source and -target of the Java Compiler

Sometimes when you may need to compile a certain project to a different version than what you are currently using. The javac can accept such command using -source and -target. The Compiler Plugin can also be configured to provide these options during compilation.

For example, if you want to use the Java 8 language features (-source 1.8) and also want the compiled classes to be compatible with JVM 1.8 (-target 1.8), you can either add the two following properties, which are the default property names for the plugin parameters:

<project>
  [...]
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  [...]
</project>

or configure the plugin directly:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler's boot classpath to match the target JRE or use the Animal Sniffer Maven Plugin to verify your code doesn't use unintended APIs. In the same way, setting the sourceoption does not guarantee that your code actually compiles on a JDK with the specified version. To compile your code with a specific JDK version, different than the one used to launch Maven, refer to the Compile Using A Different JDK example.

http://maven.apache.org/components/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM