简体   繁体   English

是什么导致 Eclipse 中导入的 Maven 项目默认使用 Java 1.5 而不是 Java 1.6,我如何确保它不会?

[英]What causes imported Maven project in Eclipse to use Java 1.5 instead of Java 1.6 by default and how can I ensure it doesn't?

I imported a Maven project and it used Java 1.5 even though I have 1.6 configured as my Eclipse default Preferences->Java->Installed JREs .我导入了一个 Maven 项目并且它使用了 Java 1.5,即使我将 1.6 配置为我的 Eclipse 默认Preferences->Java->Installed JREs

When I changed the Maven project to use the 1.6 JRE it still had the build errors left over from when the project was using Java 1.5 (I described these build errors earlier in: I have build errors with m2eclipse but not with maven2 on the command line - is my m2eclipse misconfigured? )当我将 Maven 项目更改为使用 1.6 JRE 时,它仍然存在项目使用 Java 1.5 时遗留的构建错误(我在前面描述了这些构建错误: 我在命令行上使用 m2eclipse 但没有使用 maven2 构建错误- 我的 m2eclipse 是否配置错误?

I'm going to delete the project and try again but I want to make sure this time that it uses Java 1.6 from the start to see if this eliminates the build problems.我将删除该项目并重试,但这次我想确保它从一开始就使用 Java 1.6,看看这是否消除了构建问题。

How do I make sure the project uses Java 1.6 when I import it?导入时如何确保项目使用 Java 1.6?

The m2eclipse plugin doesn't use Eclipse defaults, the m2eclipse plugin derives the settings from the POM. m2eclipse 插件不使用 Eclipse 默认值,m2eclipse 插件从 POM 派生设置。 So if you want a Maven project to be configured to use Java 1.6 settings when imported under Eclipse, configure the maven-compiler-plugin appropriately, as I already suggested:因此,如果您希望在 Eclipse 下导入时将 Maven 项目配置为使用 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>

If your project is already imported, update the project configuration ( right-click on the project then Maven V Update Project Configuration ).如果您的项目已导入,请更新项目配置(右键单击该项目,然后单击Maven V Update Project Configuration )。

I added this to my pom.xml below the project description and it worked:我将此添加到项目描述下方的 pom.xml 中,并且它起作用了:

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

I wanted to add something to the answer already provided.我想在已经提供的答案中添加一些内容。 maven-compiler-plugin by default will compile your project using Java 1.5 which is where m2e get's its information. maven-compiler-plugin默认将使用 Java 1.5 编译您的项目,这是 m2e 获取其信息的地方。

That's why you have to explicitly declare the maven-compiler-plugin in your project with something other then 1.5.这就是为什么您必须使用 1.5 以外的其他内容在项目中显式声明maven-compiler-plugin Your effective pom.xml will implicitly use the default set in the maven-compiler-plugin pom.xml .您的有效pom.xml将隐式使用maven-compiler-plugin pom.xml的默认设置。

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

Your JRE was probably defined in run configuration.您的 JRE 可能是在运行配置中定义的。 Follow these steps in Eclipse to change the build JRE.在 Eclipse 中按照以下步骤更改构建 JRE。

1) Right click on the project and select Run As > Run Configurations 1)右键单击项目并选择运行方式>运行配置

2) From Run Configurations window, select your project build configuration on the left panel. 2) 从 Run Configurations 窗口,在左侧面板上选择您的项目构建配置。 On the right, you will see various tabs: Main, JRE, Refresh, Source,...在右侧,您将看到各种选项卡:Main、JRE、Refresh、Source、...

3) Click on JRE tab, you should see something like this 3)点击JRE选项卡,你应该看到这样的

在此处输入图片说明

4) By default, Work Default JRE (The JRE you select as default under Preferences->Java->Installed JREs ) will be used. 4) 默认情况下,将使用 Work Default JRE(您在Preferences->Java->Installed JREs下选择为默认的JRE )。 If you want to use another installed JRE, tick the Alternate JRE checkbox and select your preferred JRE from the dropdown.如果您想使用另一个已安装的 JRE,请勾选备用 JRE复选框并从下拉列表中选择您喜欢的 JRE。

Here is the root cause of java 1.5:这是java 1.5的根本原因:

Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with.另请注意,目前默认源设置为 1.5,默认目标设置为 1.5,这与您运行 Maven 所使用的 JDK 无关。 If you want to change these defaults, you should set source and target.如果要更改这些默认值,则应设置源和目标。

Reference : Apache Mavem Compiler Plugin参考: Apache Maven 编译器插件

Following are the details:以下是详细信息:

Plain pom.xml普通的 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>

Following plugin is taken from an expanded POM version(Effective POM),以下插件取自扩展的 POM 版本(Effective POM),

This can be get by this command from the command line C:\\mvn help:effective-pom I just put here a small snippet instead of an entire 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>

Even here you don't see where is the java version defined, lets dig more...即使在这里你也看不到 java 版本定义在哪里,让我们挖掘更多......

Download the plugin, Apache Maven Compiler Plugin » 3.1 as its available in jar and open it in any file compression tool like 7-zip下载插件, Apache Maven Compiler Plugin » 3.1,因为它在 jar 中可用,并在任何文件压缩工具(如7-zip)中打开它

Traverse the jar and findout遍历罐子并找出

plugin.xml插件文件

file inside folder文件夹内的文件

maven-compiler-plugin-3.1.jar\\META-INF\\maven\\ maven-compiler-plugin-3.1.jar\\META-INF\\maven\\

Now you will see the following section in the file,现在您将在文件中看到以下部分,

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

Look at the above code and find out the following 2 lines查看上面的代码,找出以下2行

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

Good luck.祝你好运。

Simplest solution in Springboot Springboot 中最简单的解决方案

I'll give you the simplest one if you use Springboot:如果你使用Springboot,我会给你最简单的:

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

Then, right click on your Eclipse project: Maven > Update project > Update project configuration from pom.xml然后,右键单击您的 Eclipse 项目:Maven > Update project > Update project configuration from pom.xml

That should do.那应该做。

One more possible reason if you are using Tycho and Maven to build bundles, that you have wrong execution environment ( Bundle-RequiredExecutionEnvironment ) in the manifest file ( manifest.mf ) defined.如果您使用TychoMaven来构建包,另一个可能的原因是您在定义的清单文件 ( manifest.mf ) 中有错误的执行环境 ( Bundle-RequiredExecutionEnvironment )。 For example:例如:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Engine Plug-in
Bundle-SymbolicName: com.foo.bar
Bundle-Version: 4.6.5.qualifier
Bundle-Activator: com.foo.bar.Activator
Bundle-Vendor: Foobar Technologies Ltd.
Require-Bundle: org.eclipse.core.runtime,
 org.jdom;bundle-version="1.0.0",
 org.apache.commons.codec;bundle-version="1.3.0",
 bcprov-ext;bundle-version="1.47.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.5
Export-Package: ...
...
Import-Package: ...
...

In my case everything else was ok.就我而言,其他一切都很好。 The compiler plugins (normal maven and tycho as well) were set correctly, still m2 generated old compliance level because of the manifest.编译器插件(正常的 maven 和 tycho 也是如此)设置正确,由于清单,m2 仍然生成旧的合规性级别。 I thought I share the experience.我以为我分享了经验。

Project specific settings项目特定设置

One more place where this can go wrong is in the project specific settings, in Eclipse.这可能会出错的另一个地方是在 Eclipse 中的项目特定设置中。

  1. project properties: click your project and one of the following:项目属性:单击您的项目和以下选项之一:

    • Alt + Enter Alt + Enter
    • Menu > Project > Properties菜单 > 项目 > 属性
    • right click your project > project properties (last item in the menu)右键单击您的项目 > 项目属性(菜单中的最后一项)
  2. click on "Java Compiler"点击“Java编译器”

  3. Uncheck "Enable project specific settings" (or change them all by hand).取消选中“启用项目特定设置”(或手动更改它们)。

Because of client requirements we had them enabled to keep our projects in 1.6.由于客户的要求,我们启用了它们以将我们的项目保持在 1.6 中。 When it was needed to upgrade to 1.7, we had a hard time because we needed to change the java version all over the place:当需要升级到 1.7 时,我们遇到了困难,因为我们需要到处更改 java 版本:

  • project POM项目POM
  • Eclipse Workspace default Eclipse 工作区默认
  • project specific settings项目特定设置
  • executing virtual machine (1.6 was used for everything)执行虚拟机(1.6 用于一切)

In case anyone's wondering why Eclipse still puts a J2SE-1.5 library on the Java Build Path in a Maven project even if a Java version >= 9 is specified by the maven.compiler.release property (as of October 2020, that is Eclipse version 2020-09 including Maven version 3.6.3): Maven by default uses version 3.1 of the Maven compiler plugin, while the release property has been introduced only in version 3.6.如果有人想知道为什么 Eclipse 仍然将 J2SE-1.5 库放在 Maven 项目的 Java 构建路径上,即使maven.compiler.release属性指定了 Java 版本 >= 9(截至 2020 年 10 月,即 Eclipse 版本) 2020-09 包括 Maven 3.6.3 版):Maven 默认使用 3.1 版的 Maven 编译器插件,而release 属性仅在 3.6 版中引入。

So don't forget to include a current version of the Maven compiler plugin in your pom.xml when using the release property:因此,在使用 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>

Or alternatively but possibly less prominent, specify the Java version directly in the plugin configuration:或者,但可能不那么突出,直接在插件配置中指定 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>

This picks up Line's comment on the accepted answer which, had I seen it earlier, would have saved me another hour of searching.这拾取了Line对已接受答案评论,如果我早点看到它,本可以再节省一个小时的搜索时间。

I found that my issue was someone committed the file .project and .classpath that had references to Java1.5 as the default JRE.我发现我的问题是有人提交了引用 Java1.5 作为默认 JRE 的文件 .project 和 .classpath。

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

By closing the project, removing the files, and then re-importing as a Maven project, I was able to properly set the project to use workspace JRE or the relevant jdk without it reverting back to 1.5 .通过关闭项目,删除文件,然后作为 Maven 项目重新导入,我能够正确设置项目以使用工作区 JRE 或相关的 jdk 而不会恢复到 1.5 。 Thus, avoid checking into your SVN the .project and .classpath files因此,避免检查 SVN 的 .project 和 .classpath 文件

Hope this helps others.希望这对其他人有帮助。

If you want to make sure that newly created projects or imported projects in Eclipse use another default java version than Java 1.5, you can change the configuration in the maven-compiler-plugin.如果要确保 Eclipse 中新创建的项目或导入的项目使用其他默认的 Java 版本而不是 Java 1.5,则可以更改 maven-compiler-plugin 中的配置。

  • Go to the folder .m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.1转到文件夹 .m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.1
  • Open maven-compiler-plugin-3.1.jar with a zip program.使用 zip 程序打开 maven-compiler-plugin-3.1.jar。
  • Go to META-INF/maven and open the plugin.xml转到 META-INF/maven 并打开 plugin.xml
  • In the following lines:在以下几行中:
    <source implementation="java.lang.String" default-value="1.5">${maven.compiler.source}</source> <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> <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.将默认值更改为 1.6 或 1.8 或您喜欢的任何值。

  • Save the file and make sure it is written back to the zip file.保存文件并确保将其写回 zip 文件。

From now on all new Maven projects use the java version you specified.从现在开始,所有新的 Maven 项目都使用您指定的 java 版本。

Information is from the following blog post: https://sandocean.wordpress.com/2019/03/22/directly-generating-maven-projects-in-eclipse-with-java-version-newer-than-1-5/信息来自以下博文: https : //sandocean.wordpress.com/2019/03/22/directly-generating-maven-projects-in-eclipse-with-java-version-newer-than-1-5/

To change JDK's version, you can do:要更改 JDK 的版本,您可以执行以下操作:

1- Project > Properties 1- 项目 > 属性
2- Go to Java Build Path 2- 转到 Java 构建路径
3- In Libraries, select JRE System ... and click on Edit 3- 在库中,选择 JRE System ...,然后单击编辑
4- Choose your appropriate version and validate 4- 选择合适的版本并验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM