如何在 IntelliJ 中更改 Maven 的 Java 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25888023/
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-11 01:24:25  来源:igfitidea点击:

How to change Java version for Maven in IntelliJ?

javamavenintellij-idea

提问by tearvisus

I'm new to both Maven and IntelliJ IDEA.

我是 Maven 和 IntelliJ IDEA 的新手。

I have a Maven project written in Java 8. Whenever I try to build it (Maven Projects window -> Lifecycle -> compile -> Run Maven Build) I get a series of compilation errors:

我有一个用 Java 8 编写的 Maven 项目。每当我尝试构建它时(Maven 项目窗口 -> 生命周期 -> 编译 -> 运行 Maven 构建),我都会收到一系列编译错误:

[ERROR] path/to/file.java:[26,52] lambda expressions are not supported in -source 1.5
(use -source 8 or higher to enable lambda expressions)

Where am I supposed to change the value of the -source parameter? I tried adding it as an additional parameter in Settings -> Compiler - > Java Compiler, but I got the same results.

我应该在哪里更改 -source 参数的值?我尝试在 Settings -> Compiler -> Java Compiler 中将其添加为附加参数,但得到了相同的结果。

The project's and module's language levels are both set to 8.0.

项目和模块的语言级别都设置为 8.0。

I'm using Maven 3.2.3 and IntelliJ IDEA Community Edition 13.1.2.

我正在使用 Maven 3.2.3 和 IntelliJ IDEA 社区版 13.1.2。

采纳答案by hamid

Or easier, add this to your pom's propertiessection:

或者更简单,将其添加到您的 pomproperties部分:

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

回答by Rahul

Change the source as shown below in pom.xml

在 pom.xml 中更改如下所示的源

<build>
        <finalName>MQService</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

回答by Rudy Vissers

Summary:

概括:

  • 'maven-compiler-plugin' ALWAYS work!It is what I suggest you to use.
  • 'maven-compiler-plugin'总是有效的!这是我建议你使用的。

To change the language level, make usage of

要更改语言级别,请使用

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.4</source>
                <target>1.4</target>
            </configuration>
        </plugin>
    </plugins>
</build>

The properties do not alwayschange the language level of Intellij!

In the code below, 1.4 was configured in the pom using maven-compiler-plugin
(the jdk of Intellij is 1.8) and the language level of the project was changed accordingly to 1.4:

属性并不总是改变 Intellij 的语言级别!

下面的代码中,使用maven-compiler-plugin在pom中配置了1.4
(Intellij的jdk是1.8),项目的语言级别也相应的改成了1.4:

enter image description here

在此处输入图片说明

It was double-checked!It is an example. Most of the time you will not downgrade the version of the JDK to 1.4!

这是双重检查!这是一个例子。大多数时候你不会将 JDK 的版本降级到 1.4!

Of course if you use properties, let's say you put in the pom 1.8, then if you use a 1.8 JDK in Intellij(the language level default is 1.8 or the language default was changed manually), then you will be able to code in 1.8 BUT at the mvn compile, the properties will NOT be seen and you will default to Maven 1.5 and the compilation will NOT succeed !

当然如果你使用properties,假设你放入pom 1.8,那么如果你在Intellij中使用1.8 JDK(语言级别默认为1.8或语言默认手动更改),那么你将可以在1.8中编码但是在 mvn 编译时,将不会看到属性,您将默认使用 Maven 1.5 并且编译不会成功!

回答by Amit Kaneria

Adding below lines to root(project level) pom.xml worked me to resolve above issue: (both of the options worked for me)

将以下几行添加到 root(项目级别)pom.xml 帮助我解决了上述问题:(这两个选项都对我有用)

Option 1:

选项1:

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

Option 2:

选项 2:

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

originally posted at: IntelliJ IDEA 13 uses Java 1.5 despite setting to 1.7

最初发布于:IntelliJ IDEA 13 尽管设置为 1.7,但仍使用 Java 1.5

回答by Anuj Teotia

There are two ways of doing this :

有两种方法可以做到这一点:

First- Add Properties

首先 - 添加属性

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

second- Add Plugin

第二个添加插件

<plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
</plugin>

回答by papigee

I don't think any response to the question addressed the concern - ". . . in IntelliJ".

我认为对这个问题的任何回答都没有解决这个问题——“......在 IntelliJ 中”。

Here are the steps: -

以下是步骤:-

  • Go to Preferences in IntelliJ ( or Shortcut ? + ,)
  • Build, Execution, Deployment > Maven > Importer- select the "JDK for Importer" dropdown then select your preferred java version, Click Apply
  • Build, Execution, Deployment > Maven > Runner- select the "JRE" dropdown then select your preferred java version, Click Apply
  • Click OK
  • 转到 IntelliJ 中的首选项(或快捷方式?+,)
  • Build, Execution, Deployment > Maven > Importer- 选择“ JDK for Importer”下拉菜单,然后选择你喜欢的java版本,点击Apply
  • Build, Execution, Deployment > Maven > Runner- 选择“ JRE”下拉菜单,然后选择你喜欢的java版本,点击Apply
  • 单击确定

回答by madroid

You should add below code in your pom.xml to force the language level to change

您应该在 pom.xml 中添加以下代码以强制更改语言级别

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

now intelliJ2019.3 CE does this for you if you go to import then alt+enter where you will get an option saying "change language level to 8 to use this functionality"

现在,intelliJ 2019.3CE 会为您执行此操作,如果您要导入然后按 alt+enter,您将看到一个选项,说“将语言级别更改为 8 以使用此功能”