【问题标题】:stop IntelliJ IDEA to switch java language level every time the pom is reloaded (or change the default project language level)每次重新加载 pom 时停止 IntelliJ IDEA 切换 java 语言级别(或更改默认项目语言级别)
【发布时间】:2015-01-18 04:47:39
【问题描述】:

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

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

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

【问题讨论】:

  • 你的 pom 是否在编译器插件配置中指定了源和目标级别?
  • 是的,源和目标设置为 1.8。但是在项目导入期间没有指定
  • @Quentin 在这种情况下,将项目重新克隆到新位置并让 IntelliJ 重新导入可能是最简单的。

标签: java maven intellij-idea pom.xml


【解决方案1】:

谢谢它有效。

小心不要犯我曾经犯过的错误。

如果您有配置文件,请将插件添加到正确的配置文件中。

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

【讨论】:

    【解决方案2】:

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

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

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

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

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

    【讨论】:

    • 解决了我的问题。似乎 IntelliJ 使用更高版本(大约在 2019.2 左右)更新了他们的内部 JRE。很难追查...
    • 谢谢!我没有运气就尝试了所有其他建议。这是丢失的部分,我不会自己找到它。
    【解决方案3】:

    除了setting the maven build propertiesadding the maven-compiler-pluginmodifying the Java version in the .iml file 之外,我还必须执行另外一个步骤。 (每个都记录在其他答案中)。您还必须set the compiler version in the settings

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        在我的情况下,这些解决方案都没有帮助。我不需要在 pom.xml 中指定任何 Java 版本。

        我需要打开 &lt;project-name&gt;.iml 文件并在那里更改 JDK 版本。

        原文:

        <?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">
            <!-- ...                                                   ^ -->
            <!-- ...                                                   | -->
        

        更新:

        <?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">
            <!-- ...                                                   ^ -->
            <!-- ...                                                   | -->
        

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

        【讨论】:

        • 这本身就为我解决了 IntelliJ 2019.3 中的问题,而无需我更改 pom 或其他任何地方的任何内容。同样,我从未在任何地方指定 JDK 1.5。
        • 示例:<... language_level="JDK_11">
        【解决方案6】:

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

        首先添加属性

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

        第二-添加插件

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

        如果有帮助,请告诉我。

        【讨论】:

        • 我已经完成了这两项工作,每次我尝试从 IntelliJ 中运行 mvn compile 时,编译器仍会重置为 1.5。但是,当从 IntelliJ 之外的命令行运行时,它可以正常工作。
        • 在IntelliJ中,修改后记得重新导入maven设置。该解决方案对我有用。
        【解决方案7】:

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

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

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

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

        【讨论】:

          【解决方案8】:

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

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

          【讨论】:

          • 插件存在于 pom, wuth 1.8 目标中,但它不会改变任何东西:(
          • 您是否启用了 maven pom 的自动导入功能?你检查过父母(和祖父母)pom,那里有任何可能冲突的 1.6 设置吗?
          • 确实,父项目是用 spring-boot-starter-parent-1.1.9.RELEASE.pom.xml 定义的,在 pluginManagement 中,maven-compiler-plugin 设置为 1.6 ...我试过了覆盖此设置而没有任何成功。看起来 spring boot 1.2.RC 不再包含 maven-compiler-plugin 了,我试试看。 - 启用自动导入。
          • 太好了,这可能就是原因。如果您指定相同版本的 Maven 编译器插件,则可能会覆盖源/目标设置。也许在你的项目根 pom.xml 中尝试一下。
          • 就我而言,这不起作用。我不得不删除此答案中提到的设置,而是添加以下内容:&lt;properties&gt; &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt; &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt; &lt;/properties&gt;
          【解决方案9】:

          vikingsteve 答案的简短版本是:

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

          【讨论】:

          • 少一个烦恼!
          • 这不仅仅是一个简短的版本,而且对我来说是必要的。 vikingsteve 的回答对我不起作用。所以我删除了那个设置并使用了这个。
          【解决方案10】:

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

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

          目前的解决方案是在pom中的compiler plugin中设置想要的编译器级别,重新导入,而不是尝试在module settings中设置。

          【讨论】:

          • 同意。缺少反馈表明 IDEA 中的设置将被忽略并从 POM 重新导入,这非常令人困惑,尤其是在使用父 POM 时。
          猜你喜欢
          • 2019-09-25
          • 2013-07-16
          • 1970-01-01
          • 2015-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多