Deep Dive into the Clean Lifecycle in Maven
The Clean Lifecycle in Maven is responsible for removing old build artifacts and ensuring that the workspace is clean before a new build starts. This prevents outdated or conflicting files from interfering with a fresh build.
1️⃣ Clean Lifecycle Overview
The Clean Lifecycle consists of three phases:
-
pre-clean→ Runs any custom tasks before cleaning. -
clean→ Deletes all previously compiled and packaged artifacts. -
post-clean→ Runs any custom tasks after cleaning.
Each phase can have goals (specific tasks assigned to it), which execute underlying tasks (actions performed inside each goal).
2️⃣ Clean Lifecycle Structure
Lifecycle: Clean │ ├── Phase: pre-clean │ ├── (Custom goals can be added) │ ├── Phase: clean │ ├── Goal: maven-clean-plugin:clean │ │ ├── Task: Locate the `target/` directory │ │ ├── Task: Delete all files and subdirectories inside `target/` │ │ ├── Task: Remove additional directories (if configured) │ │ ├── Task: Log cleanup results │ └── Phase: post-clean ├── (Custom goals can be added) The main goal used in the clean phase is:
-
maven-clean-plugin:clean→ Deletes old build artifacts.
3️⃣ Clean Lifecycle Phases, Goals, and Tasks
🔹 Phase 1: pre-clean
📌 Purpose:
- Runs before cleaning starts.
- No default goals are assigned, but developers can add custom goals.
📌 Possible Custom Goals:
- Backing up important files before cleaning.
- Logging information before cleanup.
📌 Example Custom Plugin
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>pre-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Backup completed before cleaning</echo> </tasks> </configuration> </execution> </executions> </plugin> 👉 This adds a custom goal that logs a message before cleaning.
🔹 Phase 2: clean
📌 Purpose:
- Deletes old build artifacts.
- Ensures a clean workspace for a fresh build.
📌 Default Goal:
maven-clean-plugin:clean
📌 Tasks Inside This Goal:
- Locate the
target/directory (default build output location). - Delete everything inside
target/(compiled.classfiles, packaged.jarfiles, logs, and reports). - Remove additional directories (if configured in
pom.xml). - Log cleanup status (successful or failed deletions).
📌 How to Execute:
mvn clean 🚀 This will trigger:
- The
cleanphase, which calls themaven-clean-plugin:cleangoal. - This goal will delete the
target/directory.
📌 Example Plugin Configuration in pom.xml:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version>3.2.0</version> <configuration> <filesets> <fileset> <directory>logs</directory> </fileset> <fileset> <directory>temp</directory> </fileset> </filesets> </configuration> </plugin> </plugins> </build> 👉 This configuration tells Maven to delete logs/ and temp/ directories in addition to target/ when mvn clean is executed.
📌 Before Running mvn clean:
my-project/ ├── src/ ├── pom.xml ├── target/ │ ├── my-project-1.0.jar │ ├── compiled-classes/ │ ├── test-reports/ └── logs/ ├── error.log 📌 After Running mvn clean:
my-project/ ├── src/ ├── pom.xml 🚀 Result:
- The
target/andlogs/directories are deleted. - The workspace is clean.
🔹 Phase 3: post-clean
📌 Purpose:
- Runs after cleaning is completed.
- No default goals, but can be customized.
📌 Possible Custom Goals:
- Logging success/failure of the clean operation.
- Running additional cleanup scripts.
📌 Example: Logging Cleanup Completion
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.8</version> <executions> <execution> <phase>post-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Cleaning completed successfully.</echo> </tasks> </configuration> </execution> </executions> </plugin> 👉 This logs "Cleaning completed successfully." after mvn clean runs.
4️⃣ Clean Lifecycle Summary Table
| Phase | Goal | Task Performed |
|---|---|---|
pre-clean | (None by default, custom only) | Runs pre-cleanup actions (e.g., backups, logging). |
clean | maven-clean-plugin:clean | Deletes target/ and configured directories. |
post-clean | (None by default, custom only) | Runs post-cleanup actions (e.g., logging success). |
5️⃣ Key Takeaways
✅ Maven Clean Lifecycle ensures a fresh build environment by removing old artifacts.
✅ It has three phases (pre-clean, clean, post-clean).
✅ The main goal is maven-clean-plugin:clean, which deletes the target/ directory.
✅ Custom goals can be added to pre-clean and post-clean for extra functionality.
✅ Running mvn clean executes only the clean lifecycle (does not trigger compilation or testing).
6️⃣ FAQ
❓ Q: Does mvn clean also compile my project?
No! mvn clean only deletes files. It does not compile anything.
❓ Q: Can I delete additional folders like logs/?
Yes! Add them in pom.xml using the maven-clean-plugin configuration.
❓ Q: What happens if target/ doesn't exist?
Nothing bad—it simply skips the deletion step.
❓ Q: Can I run mvn clean package together?
Yes! It will first clean, then compile, test, and package:
mvn clean package 👉 This deletes old files first, then builds a fresh JAR/WAR.
7️⃣ Final Thoughts
The Clean Lifecycle is simple but essential for avoiding old, stale files in your project. It ensures that each build starts with a fresh environment, reducing the risk of conflicts.
Top comments (0)