如何在 Eclipse 中设置默认的 Maven 的 Java?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19096608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:04:52  来源:igfitidea点击:

How to set default Maven's Java in Eclipse?

javaeclipsemavenm2emaven-archetype

提问by Suzan Cioc

If I create new Mavenproject in Eclipseand base it on quickstart archetype, it appears with J2SE-1.5in Java Build Pathwindow and 1.5 in Java Compiler / JDK Compliancewindow.

如果我在其中创建新Maven项目Eclipse并基于快速入门原型,它会J2SE-1.5Java Build Path窗口中显示,在窗口中显示 1.5 Java Compiler / JDK Compliance

So, I usually have to change this to other Java manually.

所以,我通常必须手动将其更改为其他 Java。

Where are these default setting come from?

这些默认设置从何而来?

How to change to 1.6 or 1.7?

如何更改为 1.6 或 1.7?

回答by Nicolas

The m2eclipse plugin uses the settings from the POM. So you need to add this to your POM:

m2eclipse 插件使用来自 POM 的设置。所以你需要把它添加到你的 POM 中:

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

回答by Gurminder Singh

You will have to manually update the pom.xmlwith the following plugin because 1.5 is the default.

您必须使用以下插件手动更新pom.xml,因为 1.5 是默认值。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
    <classpathContainers>
       <classpathContainer>
org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6
       </classpathContainer>
    </classpathContainers>
</configuration>
</plugin>

Refrences:

参考资料:

  1. Eclipse JRE System Library [J2SE-1.5]

  2. Eclipse + Maven: force Execution Environment "JavaSE-1.6" instead of fixed JDK

  1. Eclipse JRE 系统库 [J2SE-1.5]

  2. Eclipse + Maven:强制执行环境“JavaSE-1.6”而不是固定的 JDK

回答by justadeveloper

You should add plugin in your pom.xml like below :

您应该在 pom.xml 中添加插件,如下所示:

 <build>
    <pluginManagement>
      <plugins>
         <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>your version</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
         <configuration>
            <source>1.7</source>
            <target>1.7</target>
         </configuration>
      </plugin>
      </plugins>
    </pluginManagement>
  </build>

And then you can see your project marked with error.In this case, Right click your project directory->Maven->Update Project option will work

然后你可以看到你的项目标有错误。在这种情况下,右键单击你的项目目录->Maven->更新项目选项将起作用