首页 文章

Maven编译错误尝试使用资源

提问于
浏览
1

我的系统中有以下配置:

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,platform编码: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 回答

  • 1

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

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

    maven-compiler-plugin refeerence .

  • 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 . 但是 try-with-resources 是在JDK 1.7(内部)版本中引入的 .

  • 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>
    

相关问题