maven更新后,Java版本自动更改为java 1.5

asd*_*lkj 106 java eclipse maven

我使用eclipse作为IDE.当我右键单击项目然后单击maven更新我的java版本更改为1.5.这是我到目前为止所做的,我按照这里列出的所有步骤

http://qussay.com/2013/09/13/solving-dynamic-web-module-3-0-requires-java-1-6-or-newer-in-maven-projects/

  1. 我将"Java构建路径"更改为"工作区默认jre 1.8.0_25"
  2. 然后将"java compiler"更改为1.8
  3. 然后改变了"project facets"> java> 1.8
  4. 将pom.xml java版本更改为1.8
    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.1.3.v20140225</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugin</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
Run Code Online (Sandbox Code Playgroud)

毕竟当我点击"Maven update"时,我的java版本会自动更改为1.5.同样在上述步骤中,前两步的版本也会自动更改为1.5.我怎样才能解决这个问题?

Jor*_*pos 195

打开pom.xml文件并在其上添加以下行:

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

1.8您当前的JDK/JRE的java版本在哪里.另一种方法是<build>使用maven-compile-pluginas 添加a

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version> <!-- or whatever current version -->
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

编辑

如果您正在寻找一种方法使其适用于Java 9+以上版本,请在这里查看@JDelorean的 答案,并且不要忘记给他一个upvote :)

  • 我想我已经做了第二种方式,我也发布了代码.看不到您在该代码中更改的内容 (4认同)
  • 这工作得很好谢谢,我有类似的问题OP (2认同)
  • @JorgeCampos - +1给你的答案.它工作正常.我也有同样的问题 (2认同)

小智 19

安装Java 9时遇到同样的问题.我的项目默认为J2SE-1.5 Execution Environment.奇怪的是,Java 9合规性级别不像以前的版本那样引用,即"1.8",而是"9".所以我必须相应地提供我的属性和Maven编译器插件配置:

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

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>9</source>
        <target>9</target>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

这似乎解决了这个问题.


Dav*_*ora 7

此问题的根本原因是,如果Eclipse 从pom生成/更新.c​​lasspath文件时,由于某种原因 Eclipse无法解析该属性的有效值maven.compiler.source,它将默认默认为using org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5

正如@ jorge-campos的专家回答,设置该属性有多种方法。

但是,豪尔赫的答案似乎对我没有用。这是我的设置:

<properties>
    <javaVersion>1.8</javaVersion>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
Run Code Online (Sandbox Code Playgroud)

...

究竟。${java.version}从来没有打算解决的(完全不同的)财产javaVersion和Eclipse忽略的财产,并使用默认值。

这使我回到了开头提到的“ 出于任何原因 ”部分;开发人员的愚蠢可能是这些原因之一。


小智 5

将此行添加到您的 pom.xml 中,然后右键单击您的 JRE 系统库 -> 属性 -> 将正确的执行环境设置为 Java 1.8 或您想要设置的版本。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version> <!-- or whatever current version -->
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin> 
Run Code Online (Sandbox Code Playgroud)