我导入了一个Maven项目,它使用Java 1.5,即使我有1.6配置为我的Eclipse默认首选项->Java->安装的jre。

当我将Maven项目更改为使用1.6 JRE时,它仍然有从项目使用Java 1.5时遗留下来的构建错误(我在前面描述了这些构建错误:我使用m2eclipse构建错误,但在命令行上没有使用maven2—是我的m2eclipse配置错误吗?)

我将删除该项目,然后再试一次,但我想确保这次它从一开始就使用Java 1.6,看看这是否消除了构建问题。

我如何确保项目在导入时使用Java 1.6 ?


当前回答

如果有人想知道为什么Eclipse仍然将J2SE-1.5库放在Maven项目的Java构建路径上,即使Maven .compiler.release属性指定了Java版本>= 9(截至2020年10月,即Eclipse版本2020-09,包括Maven版本3.6.3):Maven默认使用Maven编译器插件的3.1版本,而release属性仅在3.6版本中引入。

因此,当使用release属性时,不要忘记在pom.xml中包含Maven编译器插件的当前版本:

<properties>
    <maven.compiler.release>15</maven.compiler.release>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </plugin>
    </plugins>
</build>

或者,直接在插件配置中指定Java版本,但可能不那么突出:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>15</release>
            </configuration>
        </plugin>
    </plugins>
</build>

这拾起了Line对已接受答案的评论,如果我早点看到它,就可以节省我另一个小时的搜索时间。

其他回答

如果您希望确保Eclipse中新创建的项目或导入的项目使用java 1.5以外的其他默认java版本,您可以在maven-compiler-plugin中更改配置。

Go to the folder .m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.1 Open maven-compiler-plugin-3.1.jar with a zip program. Go to META-INF/maven and open the plugin.xml In the following lines: <source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source> <target implementation="java.lang.String" default-value="1.5">${maven.compiler.target}</target> change the default-value to 1.6 or 1.8 or whatever you like. Save the file and make sure it is written back to the zip file.

从现在开始,所有新的Maven项目都使用您指定的java版本。

信息来自以下博客文章:https://sandocean.wordpress.com/2019/03/22/directly-generating-maven-projects-in-eclipse-with-java-version-newer-than-1-5/

m2eclipse插件不使用Eclipse默认值,m2eclipse插件从POM派生设置。因此,如果你想让Maven项目在Eclipse下导入时使用Java 1.6设置,请适当配置Maven -compiler-plugin,正如我已经建议的那样:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>2.1</version>
  <configuration>
    <source>1.6</source>
    <target>1.6</target>
  </configuration>
</plugin>

如果项目已经导入,则更新项目配置(右键单击项目,然后单击Maven V update project configuration)。

要更改JDK版本,您可以执行以下操作:

1-项目>属性 2-进入Java构建路径 3-在Libraries中选择JRE System…点击编辑 4-选择合适的版本并验证

我想对已经提供的答案补充一些东西。maven-compiler-plugin默认会使用Java 1.5编译你的项目,这是m2e获取信息的地方。

这就是为什么你必须在你的项目中显式地声明maven-compiler-plugin,而不是使用1.5版本。有效的pom.xml将隐式使用maven-compiler-plugin pom.xml中的默认设置。

如果有人想知道为什么Eclipse仍然将J2SE-1.5库放在Maven项目的Java构建路径上,即使Maven .compiler.release属性指定了Java版本>= 9(截至2020年10月,即Eclipse版本2020-09,包括Maven版本3.6.3):Maven默认使用Maven编译器插件的3.1版本,而release属性仅在3.6版本中引入。

因此,当使用release属性时,不要忘记在pom.xml中包含Maven编译器插件的当前版本:

<properties>
    <maven.compiler.release>15</maven.compiler.release>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </plugin>
    </plugins>
</build>

或者,直接在插件配置中指定Java版本,但可能不那么突出:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>15</release>
            </configuration>
        </plugin>
    </plugins>
</build>

这拾起了Line对已接受答案的评论,如果我早点看到它,就可以节省我另一个小时的搜索时间。