我是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)
我应该在哪里更改-source参数的值?我尝试在“设置”中添加它作为附加参数 - >编译器 - > Java编译器,但我得到了相同的结果。
项目和模块的语言级别都设置为8.0。
我使用的是Maven 3.2.3和IntelliJ IDEA Community Edition 13.1.2。
答案 0 :(得分:94)
或者更容易,将其添加到您的pom的properties
部分:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
答案 1 :(得分:33)
摘要:
要更改语言级别,请使用
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
</build>
属性不始终更改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并且编译将不会成功!
答案 2 :(得分: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>
答案 3 :(得分:6)
对于该问题,我认为没有任何回应可以解决问题-“ .. in IntelliJ ”。
以下是步骤:-
答案 4 :(得分:4)
将以下行添加到root(项目级别)pom.xml让我解决了上述问题:(这两个选项对我有用)
选项1:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
选项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>
答案 5 :(得分:1)
有两种方法可以做到这一点:
首先添加属性
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
second-添加插件
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
答案 6 :(得分:0)
您应该在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>
现在 intelliJ 2019.3如果您要导入,CE将为您执行此操作,然后alt + ent进入,您将获得一个选项,提示“将语言级别更改为8以使用此功能”
答案 7 :(得分:0)
打开ubuntu终端,转到您的根目录并键入:
export JAVA_HOME = <path to jdk>
例如,它对我来说很好{在IntelliJ终端中执行相同操作}。
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
检查设置值类型echo $JAVA_HOME
检查Maven版本类型:mvn -version
通过键入此命令可以找到JDK的所有路径,并且可以设置JDK版本。
sudo update-alternatives --config java
还要检查您是否拥有相同的java -version
和javac -version
。
答案 8 :(得分:0)
Here are the steps for intellij version# 2021.2 Ultimate Edition: -
Go to Settings in IntelliJ
Build, Execution, Deployment > Build Tools > Maven > importing > "JDK for Importer" then select your preferred java version, Click Apply
Build, Execution, Deployment > Build Tools > Maven > Runner > For "JRE" option select your preferred java version,
Click Apply
Click OK