[英]Eclipse Maven and Java 8 problems
我從簡單的Web應用程序遷移到了Maven Web應用程序,在Eclipse Neon上遇到了一個令人沮喪的問題:在pom.xml中添加了使用Java 8或7的規范后,它不起作用。
為了驗證它是否有效,我編寫了一個簡單的類,在其中使用try(expression)聲明。
在maven中使用Java 8(我已經安裝了它並且可以在常規Web應用程序中使用),我該怎么辦?
pom.xml的代碼是這樣的:
<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.webdata</groupId>
<artifactId>WebParser</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>WebParser</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.23</version>
</dependency>
</dependencies>
</project>
首先,您的JAVA_HOME
必須指向JDK 1.8。
否則,您必須以這種方式在pom.xml
的編譯器配置中將JDK 1.8指定為源和目標:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerVersion>1.8</compilerVersion>
<fork>true</fork>
<executable>D:\jdk1.8\bin\javac</executable>
</configuration>
</plugin>
然后,在eclipse中,您必須檢入Preferences:
Java->Installed JREs
了1.8 JRE / JDK。 如果要將1.8用於Eclipse中任何新創建的項目,可以將以下內容設置為默認值:
Java->Installed JREs
:選擇1.8。 Java->Compiler
:選擇JDK編譯器符合級別為1.8。 如果默認首選項不使用1.8編譯合規性和JDK / JRE,或者項目是從此Eclipse外部創建的,並且首選項設置為1.8編譯合規性和JDK / JRE,則應檢查並調整Eclipse項目的properties
為此,請進入項目的屬性,然后進入Java Build Path
並在Libraries
選項卡中。 您必須檢查JRE System Library
使用Java 1.8。 如果不是這種情況,請將其替換為1.8版本。
完成后,始終在項目的屬性中,轉到Java Compiler
並檢查您是否使用了符合Java 1.8的JDK編譯器。
它應該工作。
這很有趣 :))
我搜索了3-4個小時,嘗試了幾種方法,直到問到這里,我才找到解決方案:)
右鍵單擊項目->屬性-> Java編譯器->取消選中“從執行中使用編譯...”,然后選擇“ 1.8”
這對我有用:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
如這里提到的-> maven-compiler-plugin
不要忘記更新Maven項目-> Alt + F5。
在pom.xml中,定義此maven.compiler.source
屬性以告訴Maven
使用Java 8
編譯項目。
Maven屬性Java 8
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
編譯器插件-可選,直接配置插件。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.