i have read about maven's clean and site lifecycle at lot of places, but not able to understand how exactly it works. Please explain me how Maven's clean Lifecycle processes with some example. how it is different from build. as before build starts, cleaning is done, right? what happens in pre and post clean phases.
-
2"before build starts, cleaning is done, right" No, not by default, although it's common to run `mvn clean install`. – Joe Oct 11 '14 at 10:37
3 Answers
The clean life cycle contains the following life cycle phases:
- pre-clean
- clean
- post-clean
If you call mvn clean
the following life cycle phases will be run through:
pre-clean, clean
If you would call mvn post-clean
the following life cycle phases would be run through:
pre-clean, clean, post-clean
By default in the clean lifeclyce only the maven-clean-plugin is bound to the clean phase which usually deletes the target folder. Usually there is nothing bound to post-clean
The other life cycles like site contains the following phases:
pre-site, site, post-site, site-deploy
while you usally call mvn site
or sometimes mvn site-deploy
.
The so called default
life cycle contains much more phases (only an exerpt):
validate, ..., compile, test, .., package, ..., install, deploy
where on each phase are bound different plugins which do their job to get the result you like to have.

- 1
- 1

- 92,914
- 28
- 189
- 235
-
I find the post-clean phase useful for resetting a local database. You can just invoke 'clean' for regular cleaning, then use 'post-clean' in places like Continuous Integration, or when you need to reset your test data. – ThrawnCA Sep 05 '19 at 23:05
-
For tests like this which are integration tests the `pre-integration-test`, `integration-test` and `post-integration-test` phase are more intended and even simpler to integration to use via maven-failsafe-plugin by using usual unit tests frameworks like junit-jupiter or test ng etc. – khmarbaise Sep 08 '19 at 10:36
-
Our builds typically use the integration-test phases for scenario testing with Selenium. It's still useful to be able to manually reset the database as needed. – ThrawnCA Sep 10 '19 at 22:56
Nothing happens by default in pre-clean or post-clean, but you can use them yourself if you want to. For example, if you want to preserve past builds for historical purposes, then you could run a backup in the pre-clean phase. Or you could use post-clean to do some extra cleanup, like resetting a local database or LDAP directory.

- 51
- 1
- 1
In Maven based project, many cached output existed in your “target” folder. When you want to build your project for deployment, you have to make sure clean all the cached output so that you are always get the latest for deployment.
For example: If you rename a class, the previous compiled version will remain in target/classes until you run clean. This maybe completely harmless, but it could cause issues if it is autodetected by classpath scanning and the like.

- 1,782
- 1
- 15
- 33