[英]Maven compilation error try-with-resources
我的系統中有以下配置:
Apache Maven 3.5.2 Maven主目錄:/ usr / share / maven Java版本:1.8.0_162,供應商:Oracle Corporation Java主目錄:/ usr / lib / jvm / java-8-openjdk-amd64 / jre默認語言環境:en_US,平台編碼:UTF-8操作系統名稱:“ linux”,版本:“ 4.15.0-20-generic”,拱門:“ amd64”,家族:“ unix”
當我編譯(使用Maven)具有嘗試資源的項目時,出現以下錯誤:
/path/driver.java:[29,13] try-with-resources is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable try-with-resources)
我嘗試添加標志-source 7,但這不是解決問題的方法,因為它給了我另一個錯誤:
[ERROR] Error executing Maven.
[ERROR] The specified user settings file does not exist: /path/ource
我在互聯網上搜索了第一個錯誤,但沒有找到任何東西
如果要使用Java 8語言功能(-source 1.8),並且還希望編譯的類與JVM 1.8(-target 1.8)兼容,則可以添加以下兩個屬性,它們是Java 8語言的默認屬性名稱。插件參數:
<project>
[...]
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
[...]
</project>
或直接配置插件:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
請查看本文以獲取更多詳細信息。 但是try-with-resources
在JDK 1.7(內部)版本中引入了try-with-resources
。
要回答第一部分,請將以下行添加到POM以設置語言級別
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
添加這些行后,您可以成功構建jar,盡管運行時jar不會給出主要的清單屬性錯誤。
這可以通過像java -cp app.jar
com.somepackage.SomeClass
一樣運行java -cp app.jar
com.somepackage.SomeClass
或更正此錯誤並制作一個可執行jar,使您的pom看起來像
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>fully.qualified.main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
@Ravindra已經建議了一種選擇。 另一種選擇是添加屬性。
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.