关于 maven-compiler-plugin 插件的使用心得

两种方式

maven 项目是通过 maven-compiler-plugin 插件来对 Java 代码编译的,如果不指定 JDK 版本,maven-compiler-plugin 会自动使用一个默认的版本,该版本可能与你使用的 IDE 所使用的 JDK 版本不一致,这种情况可能会导致代码无法通过 maven 的编译,例如:在 IDE 指定 JDK 1.8 ,coding 的时候使用了JDK 1.8 的特性,而 maven-compiler-plugin 默认的 JDK 版本为 1.5,此时 JDK 1.5 是不可能将带有 JDK 1.8 特性的代码编译通过的。此类问题的出现可通过在 pom 文件中指定 JDK 版本来避免,有如下两种方式:

  1. 方式一
    <properties>
        <!-- maven-compiler-plugin 将会使用指定的 JDK 版本将 java 文件编译为 class 文件(针对编译运行环境) -->
        <maven.compiler.target>1.8</maven.compiler.target>
        <!-- maven-compiler-plugin 将会使用指定的 JDK 版本对源代码进行编译(针对编译运行环境) -->
    	<maven.compiler.source>1.8</maven.compiler.source>
    <properties>
  1. 方式二
<build>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.8.0</version>
               <configuration>
                   <!-- 同【方式一】的 maven.compiler.target -->
                   <source>1.8</source>
                   <!-- 同【方式一】的 maven.compiler.source -->
                   <target>1.8</target>
               </configuration>
           </plugin>
       </plugins>
   </build>
  1. 方式三

    首先要先说明一下,该方式并非 Maven 官方配置,要使该方式能够生效首先必须满足以下两个条件:

  • 项目为一个 SpringBoot 工程
  • 项目的 POM 继承了 spring-boot-starter-parent 注:以2.1.3.RELEASE版本为例
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
</project>    

如上边的代码片段,只要指定 java.version 就等同于指定了maven.compiler.target、maven.compiler.source,那么问题就来了!!!

  • java.version 并非 Maven 官方配置,那做到这一点的肯定就不是 Maven 了,那会是谁呢?!

  • 确定了是谁,那 TA 又是如何实现的呢?!

    要想找到以上问题的答案,先看下图:
    在这里插入图片描述
    通过上边的截图,答案很明显了吧,该截图是 spring-boot-starter-parent POM 文件的片段,java.version 是 spring-boot-starter-parent 自定义的 properties,而 maven.compiler.target、maven.compiler.source 都依赖于 java.version,所以设置了 java.version 也就等同于同时设置了maven.compiler.target、maven.compiler.source。

相关问题

  1. IntelliJ IDEA 里面 Maven 插件,如果 pom.xml 里面没有指定 JDK 版本,那么 IntelliJ IDEA 将默认使用 1.5 版本进行编译。此时, IntelliJ IDEA 编译时就会出现如下警告:

Warning:java: source value 1.5 is obsolete and will be removed in a future release

或者

Warning:java: 源值1.5已过时, 将在未来所有发行版中删除

要解决这个问题,除了上述的【方式一】【方式二】,就只能通过手动设置来更改默认的版本,如下图:
在这里插入图片描述
2. 如果你的 project 是一个 SpringBoot,且 POM 继承了 spring-boot-starter-parent(这里以 2.1.3.RELEASE 为例),那么可以先查看一下 spring-boot-starter-parent 中的 pom.xml,在 父POM 里已对 maven.compiler.target、maven.compiler.source、project.reporting.outputEncoding、project.build.sourceEncoding、java.version 进行了设置,如果已满足你的需求,则不需要再在 子POM 里再次设置进行覆盖了,如下图:
在这里插入图片描述
3. 如果你的 project 是一个 SpringBoot,且 POM 继承了 spring-boot-starter-parent(这里以 2.1.3.RELEASE 为例),在 子POM 和 父POM 中是无法找到 maven-compiler-plugin 插件装载的配置(即 … 中不存在此插件的配置),但该 project 确实实在在地可以使用此插件,这是为什么呢?我们可以在 IDEA 中打开 子POM,在 子POM 编辑域内,右键选择 Maven -> Show Effective POM ,如下图:
在这里插入图片描述
在新开的窗口内,可以在…中找到 maven-compiler-plugin 插件关于装载的配置,如下图:
在这里插入图片描述


版权声明:本文为yangchao1125原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>