Maven的maven-compiler-plugin插件详解

mvn compile

mvn compile 命令会将 src/main/resources 下的资源文件复制到编译输出目录下;接着会将 src/main/java 目录下源代码编译输出到编译输出目录下。编译输出目录默认是 target/classes 目录。

打开命令终端,切换到 pom.xml 所在目录下,执行下面的命令:

[~/documents/IdeaProjects/demo02]$ mvn compile
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< com.example:demo02 >-------------------------
[INFO] Building demo02 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo02 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/liaowenxiong/Documents/IdeaProjects/demo02/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.094 s
[INFO] Finished at: 2022-01-27T08:25:03+08:00
[INFO] ------------------------------------------------------------------------

从上述的执行结果可以得知,执行 mvn compile 之后会先后调用 maven-resources-plugin 插件和 maven-compiler-plugin 插件,两个插件分别先后完成对应的任务。

mvn compile 也属于生命周期命令,默认不需要你在 pom.xml 文件中配置。

mvn test-compile

mvn compile 命令会将 src/test/resources 下的资源文件复制到编译输出目录下;接着会将 src/test/java 目录下源代码编译输出到编译输出目录下。这里的编译输出目录默认是 target/test-classes 目录。

打开命令终端,切换到 pom.xml 所在目录下,执行下面的命令:

[~/documents/IdeaProjects/demo02]$ mvn test-compile
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< com.example:demo02 >-------------------------
[INFO] Building demo02 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo02 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo02 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo02 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.755 s
[INFO] Finished at: 2022-01-27T08:53:47+08:00
[INFO] ------------------------------------------------------------------------

从上面的执行结果可以得知,执行 mvn test-compile 会去调用 maven-resources-plugin 插件和 maven-compiler-plugin,而且还分别调用了两次,说明执行 mvn test-compile 命令不仅会编译 src/test/ 目录下的源码文件还会编译 src/main 目录下的源码文件。

编译插件的配置

maven-compiler-plugin 指定编译源文件的 JDK 版本和项目的编码方式。

<build>
	<pluginManagement>
		<plugins> 
           <plugin> 
              <groupId>org.apache.maven.plugins</groupId> 
              <artifactId>maven-compiler-plugin</artifactId> 
              <configuration> 
                  <source>9.0.4</source> 
                  <target>9.0.4</target> 
                  <encoding>UTF-8</encoding> 
              </configuration> 
           </plugin> 
		</plugins> 
	</pluginManagement>
</build>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值