java Maven 给出错误:-source 1.5 不支持 try-with-resources

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17300008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:38:33  来源:igfitidea点击:

Maven giving error: try-with-resources is not supported in -source 1.5

javamaven

提问by Robert H

I am getting errors when trying to create a jar with Maven 3.0.5 using IntelliJ 12.1.4 and Java 7. I am able to run the project via the IDE with no problems, but when I try to package it I get the following errors.

尝试使用 IntelliJ 12.1.4 和 Java 7 创建带有 Maven 3.0.5 的 jar 时出现错误。我可以通过 IDE 运行该项目,没有任何问题,但是当我尝试对其进行打包时,出现以下错误.

The relevant section of my POM (taken from Maven By Exampleby Sonatype) is:

(摘自我的POM的相关部分Maven By Example通过Sonatype)是:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>jar-with-dependencies</descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

and the errors are:

错误是:

[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

How can I get maven to use source 1.7?

如何让 maven 使用源 1.7?

回答by Robert H

To answer the first part, add the following lines to the POM to set language level

要回答第一部分,请将以下几行添加到 POM 以设置语言级别

 <properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

After adding those lines you can successfully build your jar, although when you run it your jar will give a no main manifest attributeerror.

添加这些行后,您可以成功构建您的 jar,但当您运行它时,您的 jar 会no main manifest attribute出错。

This can either be fixed by running like java -cp app.jar com.somepackage.SomeClass

这可以通过运行来修复 java -cp app.jar com.somepackage.SomeClass

or to correct this and make an executable jar, make your pom look like

或者要纠正这个问题并制作一个可执行的 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>

This pom overcomes some issues with the jar-with-dependenciesdescriptorRef by copying dependencies to a build dir, and then creating the jar with the included libraries.

这个 pomjar-with-dependencies通过将依赖项复制到构建目录,然后使用包含的库创建 jar,克服了descriptorRef 的一些问题。

Thanks goes out to @André Aronsenfor his pom solution.

感谢@André Aronsen的 pom 解决方案。

Regarding the no main manifest error, there are a lot of posts about this issue, some solutions work, some don't. This solution works for me, so I've included it in this post for completion.

关于无主要清单错误,有很多关于这个问题的帖子,有些解决方案有效,有些则无效。这个解决方案对我有用,所以我把它包含在这篇文章中以供完成。

Tested with Java 7, Maven 3.0.5 and JetBrains IntelliJ IDEA 12.1.4 Ultimate.

使用 Java 7、Maven 3.0.5 和 JetBrains IntelliJ IDEA 12.1.4 Ultimate 进行测试。