Apache Maven Clean Plugin

The Apache Maven Clean Plugin is an essential tool for any Maven-based project. It is responsible for removing the files generated during the build process, ensuring a clean slate for each new build. This blog post will provide an in-depth look at the Maven Clean Plugin, including its configuration, usage, and a complete example.

Introduction to Maven Clean Plugin

The Maven Clean Plugin is used to delete the target directory and its contents, which are created during the build process. This helps in maintaining a clean build environment by removing all the artifacts from the previous build.

Latest Version

As of this writing, the latest version of the Maven Clean Plugin is 3.2.0. Always ensure you are using the latest version to benefit from the latest features and bug fixes.

Setting Up the Maven Clean Plugin

To use the Maven Clean Plugin, you need to configure it in your project's pom.xml file. Here is a step-by-step guide on how to do it, including a complete project example.

Step 1: Create a Maven Project

Run the following command to create a new Maven project:

mvn archetype:generate -DgroupId=com.example -DartifactId=clean-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command generates a simple Maven project with the following structure:

clean-plugin-demo
|-- src
|   |-- main
|   |   `-- java
|   |       `-- com
|   |           `-- example
|   |               `-- App.java
|   `-- test
|       `-- java
|           `-- com
|               `-- example
|                   `-- AppTest.java
|-- pom.xml
`-- target

Step 2: Add Maven Clean Plugin Configuration

Navigate to the project directory and open the pom.xml file. Add the Maven Clean Plugin configuration inside the <build> section:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>clean-plugin-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <verbose>true</verbose>
                    <filesets>
                        <fileset>
                            <directory>${project.basedir}/logs</directory>
                            <includes>
                                <include>**/*.log</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Explanation

  • <groupId>: This is the group ID for the Maven Clean Plugin.
  • <artifactId>: This specifies the Maven Clean Plugin.
  • <version>: The version of the plugin you are using. Ensure you use the latest version.
  • <configuration>: This section allows you to specify various options for the clean plugin.
    • <verbose>: When set to true, it enables detailed output during the clean process.
    • <filesets>: Allows you to specify additional directories or files to be deleted.
    • <fileset>: Represents a set of files to be deleted.
      • <directory>: Specifies the directory to be deleted.
      • <includes>: Specifies the patterns of files to be included for deletion.

Step 3: Create an Additional Directory

For this example, create a logs directory in your project root and add some dummy log files. The project structure should look like this:

clean-plugin-demo
|-- logs
|   `-- dummy.log
|-- src
|   |-- main
|   |   `-- java
|   |       `-- com
|   |           `-- example
|   |               `-- App.java
|   `-- test
|       `-- java
|           `-- com
|               `-- example
|                   `-- AppTest.java
|-- pom.xml
`-- target

Step 4: Add Hello World to App.java

Open src/main/java/com/example/App.java and add a simple Hello World program:

package com.example;

public class App {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Step 5: Build the Project

Run the following command to compile and package the project:

mvn clean package

This command will compile your project and package it into a JAR file located in the target directory.

Step 6: Clean the Project

To clean the project and delete the target directory along with the additional directories specified, run:

mvn clean

You will see detailed output if you have set the verbose configuration to true. The target directory and the logs directory with its contents will be deleted.

Step 7: Running the Application (Run Packaged JAR)

After packaging the project, you can run the packaged JAR to see the "Hello, World!" output:

java -jar target/clean-plugin-demo-1.0-SNAPSHOT.jar

You should see the following output:

Hello, World!

Conclusion

The Maven Clean Plugin is an important plugin for maintaining a clean build environment. By properly configuring and using this plugin, you can ensure that your project is always built from a clean slate, avoiding potential issues caused by leftover artifacts from previous builds. This guide provided a complete example of setting up and using the Maven Clean Plugin in a simple Maven project, helping you get started with customizing it according to your project's needs.

Comments