每次重新加载pom时停止IntelliJ IDEA切换java语言级别(或更改默认项目语言级别)

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

stop IntelliJ IDEA to switch java language level every time the pom is reloaded (or change the default project language level)

javamavenintellij-ideapom.xml

提问by Quentin

Using IntelliJ 12, I have a java project and I use maven with a pom.xml. My project is using java8, but it seems the default project language level has been set to 6 while importing the project.

使用 IntelliJ 12,我有一个 java 项目,我使用带有 pom.xml 的 maven。我的项目使用的是java8,但在导入项目时似乎默认项目语言级别已设置为6。

I can change the language level to 8.0 (F4 -> Modules -> Language level) however every time I edit my pom.xml the project level is switched back to "use project language level", and I have to edit this settings again and again.

我可以将语言级别更改为 8.0(F4 -> 模块 -> 语言级别),但是每次我编辑 pom.xml 时,项目级别都会切换回“使用项目语言级别”,我必须再次编辑此设置并且再次。

Is there something I need to add to the pom.xml to set the default language level to 8.0?

是否需要向 pom.xml 添加一些内容以将默认语言级别设置为 8.0?

采纳答案by vikingsteve

As per Mark's comment, here is how to do it:

根据马克的评论,这是如何做到的:

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

回答by Peter N. Steinmetz

I think this has to do with a conceptual conflict between the Maven compiler plugin and IntelliJ idea. Apparently the newer versions of the compiler plugin have a default level of 1.5 (see http://maven.apache.org/plugins/maven-compiler-plugin/). So if the compiler plugin is used at all in a project, and the compiler level is not explicitly set in the pom.xml, whenever the POM is re-processed the level will revert to the default.

我认为这与 Maven 编译器插件和 IntelliJ 想法之间的概念冲突有关。显然,较新版本的编译器插件的默认级别为 1.5(请参阅http://maven.apache.org/plugins/maven-compiler-plugin/)。因此,如果在项目中完全使用了编译器插件,并且没有在 pom.xml 中明确设置编译器级别,则无论何时重新处理 POM,级别都将恢复为默认值。

So there is a conceptual conflict which is ignored by Intellij IDEA. The IDE still allows one to set the project and module settings, but provides no warning or feedback that this setting is controlled by pom.xml. Solutions would either be to explicitly allow overriding the POM compiler plugin setting (perhaps not wise because what then happens when you use maven on the command line), or to deactivate the controls in the IDE when this setting from the POM is in effect.

因此,Intellij IDEA 忽略了一个概念冲突。IDE 仍然允许设置项目和模块设置,但不提供此设置由 pom.xml 控制的警告或反馈。解决方案要么是明确允许覆盖 POM 编译器插件设置(可能不明智,因为在命令行上使用 maven 时会发生什么),或者在 POM 中的此设置生效时停用 IDE 中的控件。

The solution at the present time is to set the desired compiler level in the compiler plugin in the pom, the re-import, rather than trying to set it in module settings.

目前的解决方案是在pom中的编译器插件中设置所需的编译器级别,重新导入,而不是尝试在模块设置中设置。

回答by DieterDP

A shorter version of vikingsteve's answer is:

vikingsteve 答案的简短版本是:

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

回答by user3677636

I struggled a lot with this problem, due to building microservices with Dropwizard. Eventually I found out that I had my build properties in the wrong pomfile (The main service's pom.xml).

由于使用 Dropwizard 构建微服务,我在这个问题上挣扎了很多。最终我发现我的构建属性在错误的pom文件(主服务的pom.xml)中。

So, even though the other packages are more like libraries, I were not able to use the Java 8 syntax.

因此,即使其他包更像是libraries,我也无法使用 Java 8 语法。

When I refactored the build plugin into the "global" .pom.xml" file, all child containers were able to use the new syntax.

当我将构建插件重构到“全局”.pom.xml文件中时,所有子容器都能够使用新语法。

May help someone having issues with multi-container projects

可以帮助遇到多容器项目问题的人

回答by Anuj Teotia

There are two ways of doing this, add either one of them in your pom.xml file:

有两种方法可以做到这一点,在 pom.xml 文件中添加其中一种:

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>

Let me know if it helps.

如果有帮助,请告诉我。

回答by kleinfreund

None of the solutions helped in my case. I didn't need to specify any Java version in my pom.xml.

在我的情况下,所有解决方案都没有帮助。我不需要在我的pom.xml.

I needed to open the <project-name>.imlfile and change the JDK version there.

我需要打开<project-name>.iml文件并在那里更改 JDK 版本。

Original:

原来的:

<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
    <!-- ...                                                   ^ -->
    <!-- ...                                                   | -->

Updated:

更新:

<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
    <!-- ...                                                   ^ -->
    <!-- ...                                                   | -->

This makes no sense at all. At no point have I specified a JDK version for Java 1.5.

这完全没有意义。我从来没有为 Java 1.5 指定过 JDK 版本。

回答by Shirish Sakhare

For me the solution of updating the POM (plugins plus properties) to the required Java compiler version (1_7 in my case) worked. However as the .iml file for each project was generated with original pom (with default compiler version of 1_5 as explained by someone above) has a JDK version of 1_5, this still overrides the pom version.

对我来说,将 POM(插件加属性)更新到所需的 Java 编译器版本(在我的情况下为 1_7)的解决方案有效。然而,由于每个项目的 .iml 文件是使用原始 pom 生成的(默认编译器版本为 1_5,如上文所述)具有 1_5 的 JDK 版本,这仍然会覆盖 pom 版本。

I deleted the .idea folder manually and imported the module into IntelliJ with a reimport from the updated pom. When I reimported the Module from updated POM,I could see that the iml files had the updated JDK version (1_7 in my case) .

我手动删除了 .idea 文件夹,并通过从更新的 pom 重新导入将模块导入 IntelliJ。当我从更新的 POM 重新导入模块时,我可以看到 iml 文件具有更新的 JDK 版本(在我的情况下为 1_7)。

回答by hosford42

There was one additional step I had to follow, in addition to setting the maven build properties, adding the maven-compiler-plugin, and modifying the Java version in the .iml file. (each documented already in the other answers). You also have to set the compiler version in the settings.

除了设置 maven 构建属性添加 maven-compiler-plugin修改 .iml 文件中的 Java 版本之外,我还必须遵循一个额外的步骤。(每个都记录在其他答案中)。您还必须在设置中设置编译器版本

回答by danw

I'm upgrading a project from JDK 8 to JDK 10+. I had the compiler properties specified correctly as follows:

我正在将一个项目从 JDK 8 升级到 JDK 10+。我正确指定了编译器属性,如下所示:

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

However the Idea project would keep resetting the language level to 8.

然而,Idea 项目会不断将语言级别重置为 8。

Eventually I figured out that Idea's Maven import process was using JDK 8 to import the project which limited the language level to <= 8.

最终我发现 Idea 的 Maven 导入过程是使用 JDK 8 来导入将语言级别限制为 <= 8 的项目。

To fixI updated the 'JDK for importer' property under Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Importing to use JDK 11.

为了修复,我更新了设置 -> 构建、执行、部署 -> 构建工具 -> Maven -> 导入以使用 JDK 11 下的“JDK for importer”属性。

enter image description here

在此处输入图片说明