ChatGPT解决这个技术问题 Extra ChatGPT

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. My project is using java8, but it seems the default project language level has been set to 6 while importing the project.

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.

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

Does your pom specify the source and target level in the compiler plugin config?
yep, source & target set to 1.8. However it wasn't specified during the project import
@Quentin In such a situation it may be the easiest to reclone the project in a new location and let IntelliJ import that instead anew.

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

the plugin is present in the pom, wuth 1.8 target, but it doesn't change anything :(
Have you got auto-import of your maven pom's enabled? Have you checked the parent (and grandparent) poms, is there any 1.6 settings there that might be conflicting?
Indeed the parent project is defined with spring-boot-starter-parent-1.1.9.RELEASE.pom.xml and in pluginManagement the maven-compiler-plugin is set to 1.6... I tried to override this settings without any success. Looks like spring boot 1.2.RC doesn't include maven-compiler-plugin anymore, I'll give it a try. - auto import is enabled.
Great, that could be why. If you specify same version of maven compiler plugin, it might be possible to override the source / target settings. Perhaps try that in your project root pom.xml.
In my case this did not work. I had to remove the setting mentioned in this answer and instead add the following: <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
D
DieterDP

A shorter version of vikingsteve's answer is:

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

One annoyance less!
This wasn't just a short version but a necessary one in my case. vikingsteve's answer did not work for me. So I removed that setting and used this one.
d
danw

I'm upgrading a project from JDK 8 to 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.

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.

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

https://i.stack.imgur.com/Er7D7.png


Resolved my issue. Seems like IntelliJ updated their internal JRE with the later releases (somewhere around 2019.2). Pretty tough to track down...
Thank you! I had tried all the other suggestions without luck. This was the missing piece and I wouldn't have found it on my own.
P
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.

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.

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.


Agreed. The lack of feedback that the settings in IDEA are just going to be ignored and re-imported from the POM is very confusing, especially when using a parent POM.
A
Anuj Teotia

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

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.


I've done both of these and the compiler is still being reset to 1.5 every time I try to run mvn compile from within IntelliJ. However, it works fine when run from the command line outside of IntelliJ.
in IntelliJ, remember to re-import maven settings after the modification. The solution worked for me.
k
kleinfreund

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

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

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.


This, by itself, solved the issue for me in IntelliJ 2019.3, without requiring that I change anything in my pom's or anywhere else. I similarly never specified JDK 1.5 anywhere.
Example: <... LANGUAGE_LEVEL="JDK_11">
u
user666

What solved the issue for me was a list of updates:

In Project Structure > modules: changes the language level Java version to 8

In Project Structure > Project: Java version should be 1.8

In pom file, same changes specified in the responses above

In Settings > Java compiler > changed the bytecode versions to 8

In Settings > maven > importing > JDK for importer should be 1.8


Thanks! Changing the Project SDK and Module SDK in Project Structure > Project Settings worked for me.
u
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 pom file (The main service's pom.xml).

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

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

May help someone having issues with multi-container projects


9
936940

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>

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

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


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


N
Narnia_Optimus

Problem: Expected Java version: 11 but Stuck at version: 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.

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.

Solution: Adding the following 3 lines in the properties made the difference.

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