Using the Maven Clean plugin

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.

Getting ready

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.

How to do it...

Let's start using the Maven Clean plugin by performing the following steps:

  1. Open the command prompt.
  2. Run the following Maven command in the simple Maven project that we created in the Creating a simple project with Maven recipe in Chapter 1, Getting Started:
    mvn clean
  3. Observe the various steps that get executed:
    [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
    

How it works...

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.

There's more...

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.

Cleaning automatically

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:

  1. Let us add the following code 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.

  2. Now run the following command on the command prompt:
    mvn package
    
  3. You will see the following screenshot:
    Cleaning automatically

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.

Skipping the deletion of the working directory

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:

  1. Configure the plugin as follows:
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>2.6</version>
          <configuration>
            <skip>true</skip>
          </configuration>
        </plugin>
  2. Run the following command on the command prompt:
    mvn clean
    
  3. Observe the output, which is as follows:
    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.

Deleting additional folders/files

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:

  1. Configure the plugin as follows:
      <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

  2. Create a report folder for the purpose of testing.
  3. Run the following command on command prompt:
    mvn clean
    
  4. You will now see the following output:
    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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.137.117