是什么原因导致Eclipse中的导入Maven项目默认使用Java 1.5而不是Java 1.6,我怎样才能确保它没有?

Chr*_*ins 209 java eclipse maven-2 build-error m2eclipse

我导入了一个Maven项目,它使用了Java 1.5,尽管我将1.6配置为我的Eclipse默认值Preferences->Java->Installed JREs.

当我改变Maven项目以使用1.6 JRE时,它仍然存在从项目使用Java 1.5时遗留的构建错误(我在前面描述了这些构建错误:我在m2eclipse中构建错误但在命令行上没有使用maven2 - 我的m2eclipse是否配置错误?)

我要删除该项目并再次尝试,但我想确保这次它从一开始就使用Java 1.6来查看是否可以消除构建问题.

如何在导入项目时确保项目使用Java 1.6?

Pas*_*ent 246

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>
Run Code Online (Sandbox Code Playgroud)

如果您的项目已导入,请更新项目配置(右键单击项目,然后单击Maven V Update Project Configuration).

  • @Raoul如果您按照我的写作**更新项目配置**,它可以在不重新导入项目的情况下工作.我做了3次测试,它只适用于我的机器. (6认同)

hog*_*ogi 31

我将它添加到项目描述下面的pom.xml中并且它有效:

<properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
</properties>
Run Code Online (Sandbox Code Playgroud)


Nic*_*las 29

我想在已提供的答案中添加一些内容.默认情况下,maven-compiler-plugin将使用Java 1.5编译您的项目,这是m2e获取其信息的地方.

这就是为什么你必须用1.5以外的东西显式声明你的项目中的maven-compiler-plugin.您的有效pom.xml意志将隐式使用maven-compiler-plugin中 的默认设置pom.xml.

  • 对我来说,这更清楚地回答"当我制作一个新的Maven项目时,为什么它是1.5?" (6认同)

pit*_*408 27

<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>
Run Code Online (Sandbox Code Playgroud)

  • 在<build> </ build>里面 (8认同)

rog*_*lad 7

这是java 1.5的根本原因:

另请注意,目前默认的源设置为1.5,默认目标设置为1.5,与您运行Maven的JDK无关.如果要更改这些默认值,则应设置源和目标.

参考: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>
Run Code Online (Sandbox Code Playgroud)

以下插件取自扩展的POM版本(Effective 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>
Run Code Online (Sandbox Code Playgroud)

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

下载插件,Apache Maven编译器插件»3.1,它在jar中可用,并在任何文件压缩工具中打开,如7-zip

穿过罐子,找到它

plugin.xml中

文件夹内的文件

Maven的编译器插件-3.1.jar\META-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>
Run Code Online (Sandbox Code Playgroud)

查看上面的代码,找出以下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>
Run Code Online (Sandbox Code Playgroud)

祝好运.


Ton*_* Vu 6

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

1)右键单击项目,然后选择Run As > Run Configurations

2)在"运行配置"窗口中,选择左侧面板上的项目构建配置.在右侧,您将看到各种选项卡:Main,JRE,Refresh,Source,...

3)点击JRE标签,你应该看到这样的东西

在此输入图像描述

4)默认情况下,将使用Work Default JRE(您在Preferences-> Java-> Installed JREs下选择的JRE默认值).如果要使用其他已安装的JRE,请勾选Alternate JRE复选框,然后从下拉列表中选择首选JRE.