【问题标题】:How to set default Maven's Java in Eclipse?如何在 Eclipse 中设置默认的 Maven 的 Java?
【发布时间】:2013-09-30 14:20:04
【问题描述】:

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

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

这些默认设置从何而来?

如何改成1.6或1.7?

【问题讨论】:

    标签: java eclipse maven m2e maven-archetype


    【解决方案1】:

    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>
    

    【讨论】:

    • maven-compiler-plugin 目前版本为 3.1。
    • 我的错误 ;) ... 已修复!
    • Maven 新手在这里:我已经把它放在我的 pom.xml 文件中,并在我右键单击项目时从 maven 菜单中选择了“更新项目”,但没有任何改变。为了让 eclipse 更新它用于我的项目的 java 版本,我还需要做些什么吗?
    【解决方案2】:

    您应该在 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>
    

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

    【讨论】:

    • 只需要配置部分。
    • Maven->更新项目对我来说是必要的。
    【解决方案3】:

    您必须使用以下插件手动更新 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>
    

    参考:

    1. Eclipse JRE System Library [J2SE-1.5]

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

    【讨论】:

    • 只需要配置maven-compiler-plugin。除此之外,它将在 Eclipse 中设置启动器,但不会设置 maven 使用的编译器,或者更准确地说,使用已安装 jdk 的兼容性。
    猜你喜欢
    • 2018-02-21
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多