我导入了一个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中新创建的项目或导入的项目使用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)。

我发现我的问题是有人提交了.project和.classpath文件,其中引用Java1.5作为默认JRE。

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
    <attributes>
        <attribute name="owner.project.facets" value="java"/>
    </attributes>
</classpathentry>

通过关闭项目,删除文件,然后作为Maven项目重新导入,我能够正确地将项目设置为使用工作区JRE或相关jdk,而无需将其恢复到1.5。因此,避免将.project和.classpath文件签入到SVN中

希望这能帮助到其他人。

项目特定设置

另一个可能出错的地方是在Eclipse中的项目特定设置中。

项目属性:单击您的项目和以下之一: Alt + Enter 菜单>项目>属性 右键单击项目>项目属性(菜单中的最后一项) 点击“Java编译器” 取消勾选“启用项目特定设置”(或手动更改所有设置)。

由于客户的需求,我们启用了它们,使我们的项目保持在1.6版本。当它需要升级到1.7时,我们遇到了困难,因为我们需要全面更改java版本:

项目的POM Eclipse工作区默认值 项目特定设置 执行虚拟机(一切都使用1.6)

我把这个添加到项目描述下面的pom.xml中,它起作用了:

<properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
</properties>
<project>

    <!-- ... -->

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>