Maven编译错误尝试使用资源

时间:2018-05-10 11:01:59

标签: java maven java-8 compilation

我的系统中有以下配置:

Apache Maven 3.5.2 Maven主页:/ usr / share / maven Java版本:1.8.0_162,供应商:Oracle Corporation Java home:/ usr / lib / jvm / java-8-openjdk-amd64 / jre 默认语言环境:en_US,平台编码:UTF-8 操作系统名称:“linux”,版本:“4.15.0-20-generic”,arch:“amd64”,family:“unix”

当我编译(使用maven)我的项目有Try-with-Resources我得到以下错误:

/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

我在互联网上搜索第一个错误,但我没有找到任何内容

3 个答案:

答案 0 :(得分:3)

如果要使用Java 8语言功能(-source 1.8)并且还希望编译的类与JVM 1.8(-target 1.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>

有关详细信息,请查看此article。但是在JDK 1.7(内部)版本中引入了try-with-resources

答案 1 :(得分:1)

要回答第一部分,请将以下行添加到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

这样运行来修复

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

答案 2 :(得分:1)

@Ravindra已经提出了一个选项。 另一种选择是添加属性。

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

请参阅maven-compiler-plugin refeerence

相关问题