How to change Java version for Maven in IntelliJ?

后端 未结 8 2028
情书的邮戳
情书的邮戳 2020-12-05 01:43

I\'m new to both Maven and IntelliJ IDEA.

I have a Maven project written in Java 8. Whenever I try to build it (Maven Projects window -> Lifecycle -> compile -> Run

相关标签:
8条回答
  • 2020-12-05 02:25

    Or easier, add this to your pom's properties section:

    <properties>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    0 讨论(0)
  • 2020-12-05 02:25

    Summary:

    • 'maven-compiler-plugin' ALWAYS work! It is what I suggest you to use.

    To change the language level, make usage of

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.4</source>
                    <target>1.4</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    The properties do not always change the language level of Intellij!

    In the code below, 1.4 was configured in the pom using maven-compiler-plugin
    (the jdk of Intellij is 1.8) and the language level of the project was changed accordingly to 1.4:

    It was double-checked! It is an example. Most of the time you will not downgrade the version of the JDK to 1.4!

    Of course if you use properties, let's say you put in the pom 1.8, then if you use a 1.8 JDK in Intellij(the language level default is 1.8 or the language default was changed manually), then you will be able to code in 1.8 BUT at the mvn compile, the properties will NOT be seen and you will default to Maven 1.5 and the compilation will NOT succeed !

    0 讨论(0)
提交回复
热议问题