學習Maven之Maven Clean Plugin



1.maven-clean-plugin是個什么鬼?

maven-clean-plugin這個插件用maven的人都不陌生.我們在執行命令mvn clean時調用的就是這個插件.

這個插件的主要作用就是清理構建目錄下得全部內容,構建目錄默認是target,但是有時候我們會配置project.build.directory, project.build.outputDirectory, project.build.testOutputDirectory, project.reporting.outputDirectory這四個目錄,調用這個插件時同時也會清理這幾個目錄下得文件.

2.helloworld

一般不需要在pom.xml配置maven-clean-plugin.如果要手動配置,大體配置如下:

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.0.0</version>
        </plugin>
    </plugins>
</build>

然后執行mvn cleanmvn clean:clean來調用這個插件清理項目.

qyfmac$ mvn clean
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building learn-maven 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ learn-maven ---
[INFO] Deleting /Users/qyfmac/git/learn-maven/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.437 s
[INFO] Finished at: 2015-12-22T17:25:23+08:00
[INFO] Final Memory: 5M/156M
[INFO] ------------------------------------------------------------------------

3.跳過執行

跳過執行有兩種方式,都很簡單,加個參數就行.

  1. 命令行中追加參數mvn clean -Dmaven.clean.skip=true.

  2. pom.xml文件中配置參數

org.apache.maven.plugins maven-clean-plugin 3.0.0 true ```

跳過清理,我是還沒在項目中遇到需要這么做的地方,可能有些項目的構建目錄只允許手動清理的需要跳過清理吧.

4.忽略錯誤

當執行mvn clean package這樣的命令時,如果clean執行失敗,會導致整個構建停止.為了讓clean執行出錯后還能繼續執行其他命令,就需要配置讓忽略錯誤.配置如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <failOnError>false</failOnError>
    </configuration>
</plugin>

5.清理構建目錄外的文件

有些項目,構建時需要清理構建目錄以外的文件,比如制定的日志文件.這時候就需要配置<filesets>來實現了.配置方式如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <!--<skip>true</skip>-->
        <!--<failOnError>false</failOnError>-->
        <!--當配置true時,只清理filesets里的文件,構建目錄中得文件不被清理.默認是flase.-->
        <excludeDefaultDirectories>false</excludeDefaultDirectories>
        <filesets>
            <fileset>
                <!--要清理的目錄位置-->
                <directory>${basedir}/logs</directory>
                <!--是否跟隨符號鏈接 (symbolic links)-->
                <followSymlinks>false</followSymlinks>
                <!--默認有些文件是不會被清理的,比如.svn文件,如果設置成false,則全部按照自定義的來處理-->
                <useDefaultExcludes>true</useDefaultExcludes>
                <!--對這些文件進行清理-->
                <includes>
                    <include>**/*</include>
                </includes>
                <!--對這些文件不清理-->
                <excludes>
                    <exclude>nc*</exclude>
                </excludes>
            </fileset>
        </filesets>
    </configuration>
</plugin>

每個配置的作用在代碼里加了注釋.不過我查遍所有能找到的資料,都沒搞懂<followSymlinks>這個標簽干什么用的.官方文檔是這么說的:

followSymLinks:

Sets whether the plugin should follow symbolic links while deleting files from the default output directories of the project. Not following symlinks requires more IO operations and heap memory, regardless whether symlinks are actually present. So projects with a huge output directory that knowingly does not contain symlinks can improve performance by setting this parameter to true. 

Starting with 3.0.0 the property has been renamed from clean.followSymLinks to maven.clean.followSymLinks.

Type: boolean
Since: 2.1
Required: No
User Property: maven.clean.followSymLinks
Default: false

這段話看的我雲里霧里,如果誰知道還望不吝賜教,給我博客留言或發我郵箱告知.

6.clean的生命周期

clean什么周期分三個階段.

名稱 說明
pre-clean executes processes needed prior to the actual project cleaning
clean remove all files generated by the previous build
post-clean executes processes needed to finalize the project cleaning

maven-clean-plugin這個插件綁定的就是clean階段.

7.進階

不管是clean生命周期還是maven-clean-plugin插件,都比較簡單,現在說個有趣的東西.我們經常執行命令 mvn clean package打包項目,目的是為了防止部分之前的輸出目錄里有臟數據導致打出的包有問題.其實我們可以把清理命令綁定到打包的什么周期里,然后執行所有命令都會先進行清理,那樣我們的mvn packagemvn clean package就是等價的了.配置如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>auto-clean</id>
            <phase>initialize</phase>
            <goals>
                <goal>clean</goal>
            </goals>
        </execution>
    </executions>
</plugin>

以后執行的絕大多數命令都會先清理項目了.

initialize屬於maven的生命周期的哪個階段?

[validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy]

答案是第二個階段.

8.結語

這個插件就一個命令clean:clean,做的事情就是清理,然后就是可以配置清理目錄和清理策略.

示例代碼github地址: https://github.com/qyf404/learn-maven/tree/maven-clean-plugin

參考

關於作者


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM