我导入了一个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 ?


当前回答

您的JRE可能在运行配置中定义。在Eclipse中按照以下步骤更改构建JRE。

1)右键单击项目,选择“以>运行配置”

2)在Run Configurations窗口中,选择左边面板上的项目构建配置。在右侧,您将看到各种选项卡:Main, JRE, Refresh, Source,…

3)单击JRE选项卡,您应该会看到如下内容

4)默认使用“工作默认JRE”(您在“首选项->Java->已安装JRE”中选择的默认JRE)。如果您想使用其他已安装的JRE,请勾选“备用JRE”复选框,并在下拉框中选择您首选的JRE。

其他回答

项目特定设置

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

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

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

项目的POM Eclipse工作区默认值 项目特定设置 执行虚拟机(一切都使用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/

下面是java 1.5的根本原因:

还要注意,目前默认的源设置是1.5,默认的目标设置是1.5,与运行Maven的JDK无关。如果您想更改这些默认值,您应该设置source和target。

参考:Apache Mavem编译器插件

详情如下:

平原pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
             http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

    <groupId>com.pluralsight</groupId>
    <artifactId>spring_sample</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

以下插件是从一个扩展的POM版本(有效的POM),

这可以通过命令行C:\mvn help:effective-pom得到,我只是在这里放了一个小片段,而不是整个pom。

    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <executions>
      <execution>
        <id>default-compile</id>
        <phase>compile</phase>
        <goals>
          <goal>compile</goal>
        </goals>
      </execution>
      <execution>
        <id>default-testCompile</id>
        <phase>test-compile</phase>
        <goals>
          <goal>testCompile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

即使在这里你也看不到java版本的定义在哪里,让我们挖掘更多…

下载插件,Apache Maven Compiler plugin»3.1,因为它在jar中可用,并在任何文件压缩工具(如7-zip)中打开它

遍历罐子并找出答案

plugin . xml

文件夹内的文件

maven -compiler -plugin 3 .1.jar \ maven目标-INF \ \

现在您将在文件中看到以下部分,

      <configuration>
    <basedir implementation="java.io.File" default-value="${basedir}"/>
    <buildDirectory implementation="java.io.File" default-value="${project.build.directory}"/>
    <classpathElements implementation="java.util.List" default-value="${project.testClasspathElements}"/>
    <compileSourceRoots implementation="java.util.List" default-value="${project.testCompileSourceRoots}"/>
    <compilerId implementation="java.lang.String" default-value="javac">${maven.compiler.compilerId}</compilerId>
    <compilerReuseStrategy implementation="java.lang.String" default-value="${reuseCreated}">${maven.compiler.compilerReuseStrategy}</compilerReuseStrategy>
    <compilerVersion implementation="java.lang.String">${maven.compiler.compilerVersion}</compilerVersion>
    <debug implementation="boolean" default-value="true">${maven.compiler.debug}</debug>
    <debuglevel implementation="java.lang.String">${maven.compiler.debuglevel}</debuglevel>
    <encoding implementation="java.lang.String" default-value="${project.build.sourceEncoding}">${encoding}</encoding>
    <executable implementation="java.lang.String">${maven.compiler.executable}</executable>
    <failOnError implementation="boolean" default-value="true">${maven.compiler.failOnError}</failOnError>
    <forceJavacCompilerUse implementation="boolean" default-value="false">${maven.compiler.forceJavacCompilerUse}</forceJavacCompilerUse>
    <fork implementation="boolean" default-value="false">${maven.compiler.fork}</fork>
    <generatedTestSourcesDirectory implementation="java.io.File" default-value="${project.build.directory}/generated-test-sources/test-annotations"/>
    <maxmem implementation="java.lang.String">${maven.compiler.maxmem}</maxmem>
    <meminitial implementation="java.lang.String">${maven.compiler.meminitial}</meminitial>
    <mojoExecution implementation="org.apache.maven.plugin.MojoExecution">${mojoExecution}</mojoExecution>
    <optimize implementation="boolean" default-value="false">${maven.compiler.optimize}</optimize>
    <outputDirectory implementation="java.io.File" default-value="${project.build.testOutputDirectory}"/>
    <showDeprecation implementation="boolean" default-value="false">${maven.compiler.showDeprecation}</showDeprecation>
    <showWarnings implementation="boolean" default-value="false">${maven.compiler.showWarnings}</showWarnings>
    <skip implementation="boolean">${maven.test.skip}</skip>
    <skipMultiThreadWarning implementation="boolean" default-value="false">${maven.compiler.skipMultiThreadWarning}</skipMultiThreadWarning>
    <source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source>
    <staleMillis implementation="int" default-value="0">${lastModGranularityMs}</staleMillis>
    <target implementation="java.lang.String" default-value="1.5">${maven.compiler.target}</target>
    <testSource implementation="java.lang.String">${maven.compiler.testSource}</testSource>
    <testTarget implementation="java.lang.String">${maven.compiler.testTarget}</testTarget>
    <useIncrementalCompilation implementation="boolean" default-value="true">${maven.compiler.useIncrementalCompilation}</useIncrementalCompilation>
    <verbose implementation="boolean" default-value="false">${maven.compiler.verbose}</verbose>
    <mavenSession implementation="org.apache.maven.execution.MavenSession" default-value="${session}"/>
    <session implementation="org.apache.maven.execution.MavenSession" default-value="${session}"/>
  </configuration>

看一下上面的代码,找出下面两行

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

祝你好运。

Springboot中最简单的解决方案

如果你使用Springboot,我会给你一个最简单的例子:

<properties>
  <java.version>1.8</java.version>
</properties>

然后,右键单击Eclipse项目:Maven >更新项目>从pom.xml更新项目配置

应该可以了。

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

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