Apache Maven Clean Plugin

1. Overview

The Clean Plugin is used when you want to remove files generated at build-time in a project's directory.

2. Plugin Goals

The Clean Plugin only has one goal.
  • clean:clean attempts to clean a project's working directory of the files that we're generated at build-time. By default, it discovers and deletes the directories configured in project.build.directory, project.build.outputDirectory, project.build.testOutputDirectory, and project.reporting.outputDirectory.

3. Usage

The Maven Clean Plugin, as the name implies, attempts to clean the files and directories generated by Maven during its build. While there are plugins that generate additional files, the Clean Plugin assumes that these files are generated inside the target directory.

3.1 Cleaning a Maven project using the command-line

The Clean Plugin can be called to execute in the command-line without any additional configurations. Like the other plugins, to run the Clean Plugin, you use:
  mvn clean:clean
where the first clean refers to the plugin's alias, and the second clean refers to the plugin goal.
However, the Clean Plugin is a special plugin and is bound to its own special lifecycyle phase called clean. Thus, for simplicity, it can also be executed by using:
  mvn clean
or with other phases/goals like:
  mvn clean package site

3.2 Running the Clean Plugin automatically during a build

If for some reason, adding clean to the command-line is not option, the Clean Plugin can be put into a project's pom.xml so that it gets executed everytime the project is built. Below is a sample pom.xml for running the Clean Plugin in the initialize phase everytime the project is built:
<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <id>auto-clean</id>
            <phase>initialize</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

4. Examples

4.1 Delete Additional Files Not Exposed to Maven

The Maven Clean Plugin will delete the target directory by default. You may configure it to delete additional directories and files. The following example shows how:
<build>
  [...]
  <plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
      <filesets>
        <fileset>
          <directory>some/relative/path</directory>
          <includes>
            <include>**/*.tmp</include>
            <include>**/*.log</include>
          </includes>
          <excludes>
            <exclude>**/important.log</exclude>
            <exclude>**/another-important.log</exclude>
          </excludes>
          <followSymlinks>false</followSymlinks>
        </fileset>
      </filesets>
    </configuration>
  </plugin>
  [...]
</build>

4.2 Ignoring Clean Errors

To ignore errors when running the cleanup for a particular project, set the failOnError property to false.
<build>
  [...]
    <plugin>
      <artifactId>maven-clean-plugin</artifactId>
      <version>3.1.0</version>
      <configuration>
        <failOnError>false</failOnError>
      </configuration>
    </plugin>
  [...]
</build>
You can also ignore them via command line by executing the following command:
mvn clean -Dmaven.clean.failOnError=false

6. Conclusion

In this quick guide, we went over the clean plugin and gave instructions on using and customizing it. Also we have seen the different plugin goals and their usage.

Comments