繁体   English   中英

Maven编译错误尝试资源

[英]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>

请参阅maven-compiler-plugin参考

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM