简体   繁体   English

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

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

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 Example by Sonatype ) is: 我的POM的相关部分(取自Sonatype Maven By Example )是:

<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使用Source 1.7?

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 attribute error. 添加这些行后,您可以成功构建您的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-dependencies descriptorRef by copying dependencies to a build dir, and then creating the jar with the included libraries. 这个pom通过将依赖项复制到构建目录,然后使用包含的库创建jar来克服jar-with-dependencies descriptorRef的一些问题。

Thanks goes out to @André Aronsen for 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进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:(185,13)java:在-source 1.5中不支持try-with-resources(使用-source 7或更高版本来启用try-with-resources) - Error:(185, 13) java: try-with-resources is not supported in -source 1.5 (use -source 7 or higher to enable try-with-resources) -source 1.5 不支持 java ee websocket 聊天室 try-with-resources - java ee websocket chatroom try-with-resources is not supported in -source 1.5 Maven编译错误尝试资源 - Maven compilation error try-with-resources IntelliJ错误 - 在-source 1.6错误中不支持java:try-with-resources。即使在项目设置中,也选择了1.7 JDK - IntelliJ error - java: try-with-resources is not supported in -source 1.6 error. Even though in project settings 1.7 JDK is selected 为什么 Maven 会生成此错误:“...在 -source 1.5 中不受支持”? - Why is Maven generating this error: “…is not supported in -source 1.5”? 使代码与Java 1.6兼容(-source 1.6中不支持try-with-resources) - make code java 1.6 compatible (try-with-resources is not supported in -source 1.6) 使用Try-with-Resources时的编译错误 - Error in compilation when using Try-with-Resources Maven try-with-resources错误中的neo4j EmbeddedNeo4j.java - neo4j EmbeddedNeo4j.java in maven try-with-resources error 继承和尝试资源 - Inheritance and Try-With-Resources 尝试资源 - The try-with-resources
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM