When a project is built, it is important to ensure that it is not adversely affected by artifacts of an earlier build. Usually, build tools generate artifacts in a well-defined folder, namely the target
folder, called project working directory. Before a new build, this folder is usually deleted.
Maven is set up on your system and is verified to work. To do this, refer to the first three recipes of Chapter 1, Getting Started.
Let's start using the Maven Clean plugin by performing the following steps:
mvn clean
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ simple-project --- [INFO] Deleting C:projectsapache-maven-cookbooksimple-project arget
If there are no files/folders to delete, you will not see the following output:
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ simple-project --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
When the clean
phase is invoked, Maven automatically infers that the clean
lifecycle is invoked.
It uses the Maven Clean plugin for this. The plugin has only one goal, namely clean
, to clean the working directory.
In the case of Maven, the working directory is called target
. Maven creates this directory when a build is done. The clean
goal of the plugin attempts to delete this directory.
As clean
is a separate lifecycle from the default
(build) lifecycle, clean
needs to be explicitly called before the default
lifecycle if you need to ensure that the working directory is removed.
In this section, we will discuss how to run the Clean plugin automatically during the build, the steps to skip the deletion of working directory, and the process of deleting some additional files/folders.
In the previous example, as we used the default behavior of the plugin and did not need to make any configurations, we did not need to make any change to the pom configuration file. However, what if we want to ensure that the clean
goal is run without explicitly calling it?
To do this, we need to define the plugin with some parameters in our pom file:
<build> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>auto-clean</id> <phase>initialize</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Though the preceding declaration may look verbose, all we are asking is for the clean
goal to be invoked during the initialize
phase of the project. We are identifying this execution
with an id
called auto-clean
.
mvn package
Even though we did not call the clean
phase, the clean
goal got invoked because it was configured in the pom file to run in the initialize
phase.
Let us look at the converse of the preceding use case. For some reason, we do not want the working directory to be deleted, even if clean
is run. To do this, perform the following steps:
<plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.6</version> <configuration> <skip>true</skip> </configuration> </plugin>
mvn clean
C:projectsapache-maven-cookbookproject-with-clean-disabled>mvn clean [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Project with clean disabled 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.6:clean (default-clean) @ project-with-clean-disabled --- [INFO] Clean is skipped.
Setting the skip
plugin property to true
indicates to Maven that the clean
goal must be skipped.
What if your project has an additional folder, say report
, besides target
, which is perhaps created by another script, and you want that to be deleted as well? We use the following steps to do the same:
<plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.6</version> <configuration> <filesets> <fileset> <directory>${basedir}/report</directory> </fileset> </filesets> </configuration> </plugin>
You have now configured the plugin to delete an additional directory
report
folder for the purpose of testing.mvn clean
C:projectsapache-maven-cookbookproject-with-clean-additional-folder>mvn clean [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Project with clean additional folder 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.6:clean (default-clean) @ project-with-clean-additional-folder --- [INFO] Deleting C:projectsapache-maven-cookbookproject-with-clean-additional-folder eport (includes = [], excludes = [])
The report
folder is deleted as well. In fact, Maven can be configured to delete (or not delete) specific folders and files inside that folder as well.
3.16.137.117