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

tea*_*sus 76 java intellij-idea maven

我是Maven和IntelliJ IDEA的新手.

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

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

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

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

我正在使用Maven 3.2.3和IntelliJ IDEA Community Edition 13.1.2.

ham*_*mid 107

或者更容易,将其添加到您的pom properties部分:

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

  • 工作正常,但不要忘记重新导入pom.xml文件,右键单击 - > Maven - > Reimport (8认同)

Rud*_*ers 34

摘要:

  • 'maven-compiler-plugin' 总是有效!这是我建议你使用的.

要更改语言级别,请使用

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.4</source>
                <target>1.4</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

它经过双重检查!这是一个例子.大多数情况下,您不会将JDK的版本降级到1.4!

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


pap*_*gee 19

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

以下是步骤:-

  • 转到 IntelliJ 中的首选项(或快捷方式?+,)
  • Build, Execution, Deployment > Maven > Importer - 选择“ JDK for Importer ”下拉菜单,然后选择你喜欢的java版本,点击Apply
  • Build, Execution, Deployment > Maven > Runner - 选择“ JRE ”下拉菜单,然后选择你喜欢的java版本,点击Apply
  • 单击确定


Rah*_*hul 6

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