简体   繁体   English

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

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

Using IntelliJ 12, I have a java project and I use maven with a pom.xml.使用 IntelliJ 12,我有一个 java 项目,我使用 maven 和 pom.xml。 My project is using java8, but it seems the default project language level has been set to 6 while importing the project.我的项目使用的是 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?

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>

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>

I think this has to do with a conceptual conflict between the Maven compiler plugin and IntelliJ idea.我认为这与 Maven 编译器插件和 IntelliJ 想法之间的概念冲突有关。 Apparently the newer versions of the compiler plugin have a default level of 1.5 (see http://maven.apache.org/plugins/maven-compiler-plugin/ ).显然,较新版本的编译器插件的默认级别为 1.5(请参阅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.因此,如果在项目中完全使用了编译器插件,并且没有在 pom.xml 中明确设置编译器级别,则无论何时重新处理 POM,级别都将恢复为默认值。

So there is a conceptual conflict which is ignored by Intellij IDEA.因此,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. IDE 仍然允许设置项目和模块设置,但不提供此设置由 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.解决方案要么是明确允许覆盖 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中的编译器插件中设置所需的编译器级别,重新导入,而不是尝试在模块设置中设置。

I'm upgrading a project from JDK 8 to JDK 10+.我正在将一个项目从 JDK 8 升级到 JDK 10+。 I had the compiler properties specified correctly as follows:我正确指定了编译器属性,如下所示:

<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 fix I 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”属性。

在此处输入图片说明

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.如果有帮助,请告诉我。

None of the solutions helped in my case.在我的情况下,所有解决方案都没有帮助。 I didn't need to specify any Java version in my pom.xml .我不需要在我的pom.xml指定任何 Java 版本。

I needed to open the <project-name>.iml file 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 版本。

What solved the issue for me was a list of updates:为我解决问题的是更新列表:

  • In Project Structure > modules: changes the language level Java version to 8在项目结构>模块中:将语言级别 Java 版本更改为 8
  • In Project Structure > Project: Java version should be 1.8在 Project Structure > Project: Java 版本应该是 1.8
  • In pom file, same changes specified in the responses above在 pom 文件中,上面的响应中指定了相同的更改
  • In Settings > Java compiler > changed the bytecode versions to 8在设置 > Java 编译器 > 将字节码版本更改为 8
  • In Settings > maven > importing > JDK for importer should be 1.8在设置 > maven > 导入 > 导入器的 JDK 应该是 1.8

I struggled a lot with this problem, due to building microservices with Dropwizard.由于使用 Dropwizard 构建微服务,我在这个问题上挣扎了很多。 Eventually I found out that I had my build properties in the wrong pom file (The main service's pom.xml ).最终我发现我在错误的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可以帮助遇到多容器项目问题的人

thank you it works.谢谢它的工作原理。

be careful not to make the same mistake I did.小心不要犯和我一样的错误。

if you have profiles, add the plugin in the right profile.如果您有配置文件,请在正确的配置文件中添加插件。

<profiles>
    <profile>
        <id>foo</id>
        <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
        </build>
    </profile>
    <profile>
        <id>bar</id>
        .......
    </profile>
<profiles>

For me the solution of updating the POM (plugins plus properties) to the required Java compiler version (1_7 in my case) worked.对我来说,将 POM(插件加属性)更新到所需的 Java 编译器版本(在我的情况下为 1_7)的解决方案有效。 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.然而,由于每个项目的 .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.我手动删除了 .idea 文件夹,并通过从更新的 pom 重新导入将模块导入 IntelliJ。 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) .当我从更新的 POM 重新导入模块时,我可以看到 iml 文件具有更新的 JDK 版本(在我的情况下为 1_7)。

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 .除了设置 maven 构建属性添加 maven-compiler-plugin修改 .iml 文件中的 Java 版本之外,我还必须执行一个额外的步骤。 (each documented already in the other answers). (每个都记录在其他答案中)。 You also have to set the compiler version in the settings .您还必须在设置中设置编译器版本

Problem: Expected Java version: 11 but Stuck at version: 8问题:预期 Java 版本:11 但停留在版本:8

  • I tried almost all the answers, still I was stuck with language_level 8 and nowhere in my project or in the Intellij IDE I found any trace that could relate to Java version 8.我尝试了几乎所有的答案,但我仍然被language_level 8卡住,在我的项目或 Intellij IDE 中无处可寻,我发现任何可能与 Java 版本 8 相关的痕迹。

  • Then I explored the pom.xml of the parent with no success but in the pom.xml of the grand-parent , I found that the release version is 8.然后我探索了parentpom.xml没有成功但是在grand-parentpom.xml中,我发现发布版本是8。

Solution : Adding the following 3 lines in the properties made the difference.解决方案:在属性中添加以下 3 行会有所不同。

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

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

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