![](/img/trans.png)
[英]Error:(185, 13) java: try-with-resources is not supported in -source 1.5 (use -source 7 or higher to enable try-with-resources)
[英]Maven giving error: try-with-resources is not supported in -source 1.5
我嘗試使用IntelliJ 12.1.4和Java 7創建一個使用Maven 3.0.5的jar時出錯。我能夠通過IDE運行項目沒有問題,但是當我嘗試打包它時,我得到以下錯誤。
我的POM的相關部分(取自Sonatype
Maven By Example
)是:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>jar-with-dependencies</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
而錯誤是:
[ERROR] ...[33,55] error: diamond operator is not supported in -source 1.5
[ERROR] ...[207,7] error: try-with-resources is not supported in -source 1.5
[ERROR] ...[73,52] error: diamond operator is not supported in -source 1.5
[ERROR] ...[129,40] error: multi-catch statement is not supported in -source 1.5
[ERROR] ...[44,6] error: try-with-resources is not supported in -source 1.5
[ERROR] ...[28,39] error: diamond operator is not supported in -source 1.5
[ERROR] ...[31,7] error: try-with-resources is not supported in -source 1.5
[ERROR] ...[38,6] error: try-with-resources is not supported in -source 1.5
[ERROR] ...[34,41] error: diamond operator is not supported in -source 1.5
[ERROR] ...[77,43] error: diamond operator is not supported in -source 1.5
[ERROR] ...[84,6] error: try-with-resources is not supported in -source 1.5
[ERROR] ...[281,38] error: diamond operator is not supported in -source 1.5
[ERROR] ...[13,55] error: diamond operator is not supported in -source 1.5
[ERROR] ...[155,7] error: try-with-resources is not supported in -source 1.5
如何讓maven使用Source 1.7?
要回答第一部分,請將以下行添加到POM以設置語言級別
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
添加這些行后,您可以成功構建您的jar,但是當您運行它時,您的jar將給出一個no main manifest attribute
錯誤。
這可以通過像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>
這個pom通過將依賴項復制到構建目錄,然后使用包含的庫創建jar來克服jar-with-dependencies
descriptorRef的一些問題。
謝謝@AndréAronsen的pom解決方案。
關於沒有主要的明顯錯誤,關於這個問題有很多帖子,有些解決方案有效,有些則沒有。 這個解決方案對我有用,所以我把它包含在這篇文章中以便完成。
使用Java 7,Maven 3.0.5和JetBrains IntelliJ IDEA 12.1.4 Ultimate進行測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.