当需要为特定的Java版本编译项目时,可以配置maven编译器插件来设置source和target版本。以下pom.xml文件配置显示了如何进行。
<project> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
项目中source和target版本的无效值会使我们的项目编译过程失败。例如,当我们尝试使用<>Java 7中可用的菱形运算符()且maven编译器插件设置为version时1.5,会产生如下编译器错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project servlet-example: Compilation failure [ERROR] /Users/wsaryada/Studio/kodejava.org/webapp-example/servlet-example/src/main/java/org/nhooo/example/servlet/SessionCounter.java:[10,51] diamond operator is not supported in -source 1.5 [ERROR] (use -source 7 or higher to enable diamond operator)