Automatic version change of IntelliJ idea language level and Java compiler

Geeks, please accept the hero post of 2021 Microsoft x Intel hacking contest>>>

Overview

Recently, after upgrading the version of IntelliJ idea, when compiling or packaging Maven project, a lot of errors often appear. The error prompts are basically syntax errors caused by incorrect JDK version. I didn’t pay much attention at the beginning. I thought that the default configuration had not been modified. So we changed java compiler in settings and language level in project
settings to our own version, such as JDK1.8, and the problem was solved temporarily. However, the same problem will happen occasionally in the next compilation or packaging (it was found later that the reason why it happened accidentally was due to the change of POM file)

Cause analysis

After investigation, it turns out that the root cause of this problem is that the JDK version is not configured in the pom.xml file of Maven. When the JDK version is not configured, java compiler and language level will automatically change back to the original default version 1.6 once the POM file changes

Solutions

The above problem can be solved by adding Maven compiler plugin plug-in to POM file and specifying the JDK version used by JDK. The configuration of Maven compiler plugin is effective for both java compiler and language level

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

I now use JDK1.8, so I configure 1.8. In case of similar problems, the corresponding version number can be configured according to the specific JDK version

Similar Posts: