Java8或Java8以前
只能通过两种方式:
- 在POM中指定属性
- Maven编译插件
通过配置属性
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
通过Maven插件
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
或( 一般建议使用pluginManagement)
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Java9或Java9以后
For Java 9 and later, you need to use a slight variant of the Java compiler plugin. Instead of the source and target properties, you need to use the release property. Here is an example of how the Maven Java compiler plugin looks with a release property instead:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
This example sets the Java version to 11 - inside the release property. Java 11 is the latest long-term-support (LTS) version of Java at the time of writing.
Notice also that the version of the Maven Java compiler plugin has changed from 3.6.1 to 3.8.0 .
Please keep in mind, that when you upgrade to Java 9+ you will most likely get more issues, like assuring that all Maven plugins and dependencies your Java project uses, are also Java 9+ compatible. Going into detail about how this is done is beyond the scope of this Maven Java compiler tutorial though.