【问题标题】:Maven compilation error try-with-resourcesMaven 编译错误 try-with-resources
【发布时间】:2018-10-20 15:16:59
【问题描述】:

我的系统中有以下配置:

Apache Maven 3.5.2 Maven 主页:/usr/share/maven Java 版本:1.8.0_162,供应商:甲骨文公司 Java 主页:/usr/lib/jvm/java-8-openjdk-amd64/jre 默认语言环境:en_US,平台编码:UTF-8 操作系统名称:“linux”,版本:“4.15.0-20-generic”,arch:“amd64”,家族:“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

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

【问题讨论】:

    标签: java maven java-8 compilation


    【解决方案1】:

    如果您想使用 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 了解更多详情。但是try-with-resources 是在 JDK 1.7(内部)版本中引入的。

    【讨论】:

      【解决方案2】:

      要回答第一部分,请在 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>
      

      【讨论】:

        【解决方案3】:

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

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

        见maven-compiler-plugin refeerence。

        【讨论】:

          猜你喜欢
          • 2014-01-06
          • 2018-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-18
          • 1970-01-01
          相关资源
          最近更新 更多