- Maven clean is a plugin as the name suggests, that approaches to clean the files and directories generated by Maven at the time of its build.
- The plugin removes the target folder that contains all the class files, docs, JAR files.
- Phases of clean Lifecycle :
- pre-clean,
- clean,
- Post-clean
- Maven clean goal (clean:clean) is bound to the clean phase in the clean lifecycle. Its clean:cleangoal deletes the output of a build by deleting the build directory. Thus, when mvn clean command executes, Maven deletes the build directory.
- We can customize this behavior by mentioning goals in any of the above phases of clean life cycle.
- In the following example, We’ll attach maven-antrun-plugin:run goal to the pre-clean, clean, and post-clean phases. This will allow us to echo text messages displaying the phases of the clean lifecycle.
Phase | Description |
pre-clean | execute processes needed prior to the actual project cleaning |
clean | remove all files generated by the previous build |
post-clean | execute processes needed to finalize the project cleaning |
Sample pom.xml with clean lifecycle phase :
<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.companyname.projectgroup</groupId> <artifactId>project</artifactId> <version>1.0</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.pre-clean</id> <phase>pre-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>pre-clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.clean</id> <phase>clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.post-clean</id> <phase>post-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>post-clean phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> |
C:\Users\gajan\artifactId-MavenApp > mvn pre-clean |
C:\Users\gajan\artifactId-MavenApp > mvn clean |
C:\Users\gajan\artifactId-MavenApp > mvn post-clean |
We have 3 clean commands, instead of running all three commands, we can run one top command post-clean , this will run previous two commands pre-clean and clean
C:\Users\gajan\artifactId-MavenApp > mvn post-clean |
C:\Users\gajan\artifactId-MavenApp > mvn post-clean C:\Users\gajan\artifactId-MavenApp > mvn post-clean [INFO] Scanning for projects… [INFO] [INFO] —————-< com.companyname.projectgroup:project >—————- [INFO] Building project 1.0 [INFO] ——————————–[ jar ]——————————— [INFO] [INFO] — maven-antrun-plugin:1.1:run (id.pre-clean) @ project — [INFO] Executing tasks [echo] pre-clean phase [INFO] Executed tasks [INFO] [INFO] — maven-clean-plugin:2.5:clean (default-clean) @ project — [INFO] [INFO] — maven-antrun-plugin:1.1:run (id.clean) @ project — [INFO] Executing tasks [echo] clean phase [INFO] Executed tasks [INFO] [INFO] — maven-antrun-plugin:1.1:run (id.post-clean) @ project — [INFO] Executing tasks [echo] post-clean phase [INFO] Executed tasks [INFO] ———————————————————————— [INFO] BUILD SUCCESS [INFO] ———————————————————————— [INFO] Total time: 0.740 s [INFO] Finished at: 2021-12-10T20:03:53+05:30 [INFO] ———————————————————————— |