首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

Maven插件教程(2)——compile插件

2024-12-13 来源:花图问答

编译是构建工具的一项最基本的功能,Maven使用Compiler插件进行编译操作。Compiler插件提供了一些配置方式使得使用者可以灵活地对编译过程进行配置。

基本编译操作

最基本的操作就是mvn compile,由于compile阶段处于default生命周期内,所以该生命周期中处于compile之前的阶段都将被一一执行。

图片.png

以上是maven内置生命周期的阶段图,default生命周期中处于compile阶段之前的有validate、process-resource两个阶段。
尝试看看执行结果:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building utils 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ utils ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ utils ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ utils ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\BitBucket\utils\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.350 s
[INFO] Finished at: 2017-08-16T09:13:57+08:00
[INFO] Final Memory: 14M/214M
[INFO] ------------------------------------------------------------------------

不难看出,编译的class文件被放到了target/classes文件夹下。

图片.png

编译单元测试

细心的你可能发现了,以上compile没有编译单元测试文件。为啥?这是因为如果使用者没有明确指出需要单元测试,那maven就默认认为你不想进行单元测试。既然不想进行单元测试,那么有有什么理由去编译单元测试的代码呢?
所以,如果希望编译单元测试的代码并且进行单元测试,需要使用test阶段。
敲入mvn test,即可完成这个任务:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building utils 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (auto-clean) @ utils ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ utils ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ utils ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to F:\BitBucket\utils\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ utils ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\BitBucket\utils\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ utils ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\BitBucket\utils\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ utils ---
[INFO] Surefire report directory: F:\BitBucket\utils\target\surefire-reports
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom (3 KB at 1.1 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar (37 KB at 35.0 KB/sec)

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.hwzheng.ControllerTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.787 s
[INFO] Finished at: 2017-08-16T09:22:32+08:00
[INFO] Final Memory: 17M/162M
[INFO] ------------------------------------------------------------------------

由于我们指定了使用test阶段,所以maven会自动调用test之前的阶段,包括调用compile插件对testCompile目标编译单元测试文件。

使用compiler插件修改项目编译器

有时候,电脑上不止安装了一种JDK,如何指定我们的工程使用我们希望的JDK版本呢?
可以通过配置compiler插件,具体配置如下。

<properties>
     <JAVA6.HOME>C:\Program Files\Java\jdk1.6.0_29</JAVA6.HOME>
</properties>

//添加到bulid-->plugins元素下
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
        <verbose>true</verbose>
        <fork>true</fork>
        <executable>${JAVA6.HOME}/bin/javac</executable>
        <compilerVersion>1.6</compilerVersion>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>
显示全文