简体   繁体   English

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

[英]Java version automatically change to java 1.5 after maven update

I am using eclipse as IDE.我正在使用 Eclipse 作为 IDE。 When I right click on the project and then click maven update my java version change to 1.5.当我右键单击该项目,然后单击 Maven 更新我的 java 版本更改为 1.5。 Here is what I did so far, I followed all the steps listed here这是我到目前为止所做的,我遵循了这里列出的所有步骤

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

  1. I changed "Java build path" to "workspace default jre 1.8.0_25"我将“Java 构建路径”更改为“工作区默认 jre 1.8.0_25”
  2. Then changed "java compiler" to 1.8然后将“java编译器”更改为1.8
  3. Then changed "project facets">java>1.8然后更改“项目方面”> java> 1.8
  4. Changed pom.xml java version to 1.8将 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>

After all this when I click on "Maven update" my java version change to 1.5 automatically.毕竟,当我单击“Maven 更新”时,我的 java 版本会自动更改为 1.5。 Also in above steps, first two step's version also change to 1.5 automatically.同样在上述步骤中,前两步的版本也会自动更改为 1.5。 How can I fix this?我怎样才能解决这个问题?

Open your pom.xml file and add the following lines on it:打开您的pom.xml文件并在其上添加以下行:

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

Where 1.8 is the Java version of your current JDK/JRE.其中1.8是当前 JDK/JRE 的 Java 版本。 Another way of doing this is adding a <build> with the maven-compile-plugin as:另一种方法是使用maven-compile-plugin添加一个<build>

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

If you are looking for a way to make it work with Java versions 9+ please take a look at @JDelorean's answer .如果您正在寻找一种使其适用于 Java 9+ 版本的方法,请查看@JDelorean 的答案

Had the same issue when I installed Java 9. My project would default to J2SE-1.5 Execution Environment.当我安装 Java 9 时遇到了同样的问题。我的项目将默认为 J2SE-1.5 执行环境。 Strangely, Java 9 compliance level is not referenced like previous versions, ie "1.8", but as "9".奇怪的是,Java 9 的合规级别并没有像以前的版本(即“1.8”)那样被引用,而是被称为“9”。 So I had to provide my properties and Maven compiler plugin config accordingly:所以我必须相应地提供我的属性和 Maven 编译器插件配置:

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

and

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

This seems to have solved the problem.这似乎解决了这个问题。 Works for versions 9 and above.适用于版本 9 及更高版本。

The root-cause of this issue is that if for any reason Eclipse's cannot resolve a valid value for the maven.compiler.source property when generating/updating the .classpath file from the pom, it will simply default to using org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5 .此问题的根本原因是,如果 Eclipse 在从 pom 生成/更新 .classpath 文件时由于任何原因无法解析maven.compiler.source属性的有效值,它将默认使用org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5

As expertly answered by @jorge-campos , there are multiple ways to set that property.正如@jorge-campos专业回答的那样,有多种方法可以设置该属性。

However, Jorge's answer didn't appear to work for me.但是,豪尔赫的回答似乎对我不起作用。 Here were my settings:这是我的设置:

<properties>
    <javaVersion>1.8</javaVersion>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

... ...

Exactly.确切地。 ${java.version} is never going to resolve to the (completely different) property javaVersion and Eclipse ignored the property and used the default. ${java.version}永远不会解析为(完全不同的)属性javaVersion并且 Eclipse 忽略了该属性并使用了默认值。

Which brings me back to the " for any reason " part I opened with;这让我回到了我打开的“出于任何原因”部分; developer stupidity can be one of those reasons.开发人员的愚蠢可能是这些原因之一。

Add this lines to your pom.xml, then right click your JRE System Library -> Properties -> Set your correct execution environment to Java 1.8 or version you want to set.将此行添加到您的 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> 

I encounter similar issue on one of my team mate machine.我在我的一台队友机器上遇到了类似的问题。 He was using old version of Eclipse, I believe it he was using Keppler.他使用的是旧版本的 Eclipse,我相信他使用的是 Keppler。 Project after being updated change JRE version to 1.5.更新后的项目将 JRE 版本更改为 1.5。

Simple updating Eclipse to latest version solve this problem.简单地将 Eclipse 更新到最新版本即可解决此问题。

In my case (old JBoss Developer Studio), the issue was the JRE environments did not include 1.8 (only 1.7).就我而言(旧的 JBoss Developer Studio),问题是 JRE 环境不包括 1.8(只有 1.7)。 When I switched the maven-compiler-plugin version to 1.7 and did maven update project, it updated the Eclipse JRE system library to 1.7.当我将maven-compiler-plugin版本切换到 1.7 并进行 maven 更新项目时,它会将 Eclipse JRE 系统库更新到 1.7。 在此处输入图像描述

So the solution is to either get a newer IDE version that includes a built-in JRE environment that is 1.8 or later, or try to install it manually (see https://stackoverflow.com/a/35204314 )因此,解决方案是要么获取包含 1.8 或更高版本的内置 JRE 环境的更新 IDE 版本,要么尝试手动安装它(请参阅https://stackoverflow.com/a/35204314

I had this problem.我有这个问题。 In my case the <properties> tag & nested tags Jorge Campos mentions above were in the wrong place.在我的例子中,上面提到的 <properties> 标签和嵌套标签 Jorge Campos 放错了地方。 If I put them between the <hostversion> and <dependencies> tags in the pom.xml file, then this behaviour stopped.如果我将它们放在 pom.xml 文件中的 <hostversion> 和 <dependencies> 标记之间,那么这种行为就会停止。

That can be picked up in Eclipse if validation of these files is switched on.如果打开了对这些文件的验证,则可以在 Eclipse 中获取。

I am using Java 11. This is how the complete pom.xml file looks like after adding <properties> and <plugin>我正在使用 Java 11。这是添加<properties><plugin>后完整 pom.xml 文件的样子

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.akshay</groupId>
  <artifactId>1000SpringSecurityEg</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>1000SpringSecurityEg Maven Webapp</name>
  <url>http://maven.apache.org</url>
    
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
    <properties>
        <javaVersion>11</javaVersion>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
  
  <build>
    <finalName>1000SpringSecurityEg</finalName>
    
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
        
  </build>
</project>

The above code worked for me.上面的代码对我有用。 Hope it works for you as well.希望它也适用于你。

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

I added these lines in my "pom.xml" file and it worked.我在我的“pom.xml”文件中添加了这些行并且它起作用了。

I allow myself to update that subject with Java 11.我允许自己使用 Java 11 更新该主题。

I have installed OpenJDK11 on my computer, and I wanted to use it in an app.我已经在我的计算机上安装了 OpenJDK11,我想在应用程序中使用它。

I had trouble because Eclipse would always change my JRE to JavaSE-1.5 when I updated my project with Maven.我遇到了麻烦,因为当我使用 Maven 更新我的项目时,Eclipse 总是将我的 JRE 更改为 JavaSE-1.5。

I had set everything as you said, but I was always directly selecting in my Java Build Path "java-11-openjdk.x86_64" as one of my Alternante JRE.我已经按照您所说的设置了所有内容,但我总是直接在我的 Java 构建路径“java-11-openjdk.x86_64”中选择作为我的备用 JRE 之一。 I fixed my problem by selecting in "Execution environment" JavaSE-10 (but you have to double click on it and then choose as a compatible JRE your OpenJDK11 version) as shown on the picture.我通过在“执行环境”JavaSE-10 中选择来解决我的问题(但您必须双击它,然后选择 OpenJDK11 版本作为兼容的 JRE),如图所示。 Execution environment setup执行环境设置

The project will use Java 11 thanks to that (picture) but you have to write 10 for the java-version in the pom.xml and also set java 10 on the Project Facets.由于该(图片),该项目将使用 Java 11,但您必须在 pom.xml 中为 java-version 编写 10,并在 Project Facets 上设置 java 10。

I've resolved the issue installing the eclipse update "JAVA 12" from the market.我已经解决了从市场上安装 Eclipse 更新“JAVA 12”的问题。 It makes my eclipse pass from Kepler to Luna.它使我的日食从开普勒传递到月球。

After that, i have been able to set 1.8 as standard JDK, fixing the "maven update" problem.之后,我能够将 1.8 设置为标准 JDK,修复“maven 更新”问题。

I experienced with JRE 15.0.1 one must ONLY specify the compiler plugin like我体验过 JRE 15.0.1 必须只指定编译器插件,如

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

If I also provide the properties like如果我还提供类似的属性

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

this will again reset to JRE 1.5 on Maven / Update Project !!!这将在Maven / Update Project上再次重置为 JRE 1.5 !!!

Check in pom.xml under properties if there is any tag with this maven.enforcer.plugin.version .如果有任何带有此maven.enforcer.plugin.version的标签,请在属性下检查pom.xml Delete it and replace that with the below code删除它并用下面的代码替换它

<javaVersion>1.8</javaVersion>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>

under properties tag.在属性标签下。

And under build, replace the plugins with the below code:在构建下,用以下代码替换插件:

 <build>
    <finalName>1000SpringSecurityEg</finalName>
    
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
        
  </build>

This solution resolved my issue.这个解决方案解决了我的问题。

我将 Eclipse 从 kepler 更改为 neon,然后使用 Maven -> Update Project 更新了我的项目。

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

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