学习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文件中配置参数

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

跳过清理,我是还没在项目中遇到需要这么做的地方,可能有些项目的构建目录只允许手动清理的需要跳过清理吧.

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

参考

关于作者

posted @ 2015-12-23 17:29 辵鵵 阅读(...) 评论(...) 编辑 收藏