mvn安装报错eclipse

mvn install error eclipse

在执行 maven clean install 然后 运行 mvn install in eclipse 得到这个错误,不确定它指的是什么:

[ERROR] /Users/user1/Desktop/proj1/src/main/java/com/proj/proj1/dao/testDAO.java:[18,21] try-with-resources is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable try-with-resources)

try-with-resources 是 Java 7 特异性。

因此,当您使用 Maven 时,您必须在 pom.xml 中配置 Maven 编译器插件以至少使用 Java 7.

如果您使用例如 JDK 8,您可以在 pom.xml 的 build 元素中添加此属性:

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

sourcetarget 不一定具有相同的值,但在大多数情况下,它更好。

在您的 POM.xml 中添加一个属性标签,以确保您将使用 Java 8(try-with-resources 要求)。检查您是否安装了 Java 1.8 sdk,并且您的 IDE 配置是否正确。

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>