Java8或Java8以前

只能通过两种方式:

  • 在POM中指定属性
  • Maven编译插件

通过配置属性

  1. <properties>
  2. <maven.compiler.target>1.8</maven.compiler.target>
  3. <maven.compiler.source>1.8</maven.compiler.source>
  4. </properties>

通过Maven插件

  1. <build>
  2. <pluginManagement>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.apache.maven.plugins</groupId>
  6. <artifactId>maven-compiler-plugin</artifactId>
  7. <version>3.6.1</version>
  8. <configuration>
  9. <source>1.8</source>
  10. <target>1.8</target>
  11. </configuration>
  12. </plugin>
  13. </plugins>
  14. </pluginManagement>
  15. <plugins>
  16. <plugin>
  17. <groupId>org.apache.maven.plugins</groupId>
  18. <artifactId>maven-compiler-plugin</artifactId>
  19. </plugin>
  20. </plugins>
  21. 或( 一般建议使用pluginManagement)
  22. <plugins>
  23. <plugin>
  24. <groupId>org.apache.maven.plugins</groupId>
  25. <artifactId>maven-compiler-plugin</artifactId>
  26. <version>3.6.1</version>
  27. <configuration>
  28. <source>1.8</source>
  29. <target>1.8</target>
  30. </configuration>
  31. </plugin>
  32. </plugins>
  33. </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:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <version>3.8.0</version>
  5. <configuration>
  6. <release>11</release>
  7. </configuration>
  8. </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.

本文摘要自:http://tutorials.jenkov.com/maven/java-compiler.html