Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always Using Maven `clean` Goal?

Tags:

java

maven

Should the Maven clean goal be applied to every build as a best practice to avoid stale CLASS files? Or perhaps always using clean is unnecessary since Maven is smart enough to know which source code needs to be re-compiled based on changes?

  • compile
  • install
  • etc?

Gareth Davis pointed out the potential danger of forgetting to running a clean after re-naming a CLASS file - https://stackoverflow.com/a/4662536/409976.

Example:

  1. Compile module (not whole project) - Foo.java -> target/Foo.class
  2. Re-name to Bar.java & re-compile module -> target/{Foo.class, Bar.class}
  3. re-compile main
  4. BOOM – other module code should’ve failed to compile since it relied on making a Foo class (but it still longer exists since we didn’t clean).

I'm looking into whether the absence of clean will improve build performance on a guest VM within a shared directory. However, I'm not aware of all of the consequences of not always calling the clean step first.

like image 449
Kevin Meredith Avatar asked Apr 28 '14 14:04

Kevin Meredith


1 Answers

Running clean before any build is always a good practice since the stale class scenario that you have mentioned is completely possible and if encountered, it can create problems in your final artifact.

I'm looking into whether the absence of clean will improve build performance on a guest VM.

By performance, my best guess is that you mean time efficiency. By avoiding the clean goal, you CAN save time since maven will only build the classes where any change was done and it will leave out the rest of the untouched class. So, technically speaking, yes you will save some time in the build process.

However, I'm not aware of all of the consequences of not always calling the clean step first.

Class staleness is by far the most common issue that you may and will encounter.

By experience I can share a couple of things. If you are using an IDE and your project structure is a bit large complex ( A couple of dependant modules etc etc ) then running mvn clean does sometimes confuse the IDE in managing the build path of the project and you may run into ClassNotFoundExceptions. Although not a big issue but you have to do a project --> maven --> Update project to set the build path right.

(Edit However, if you are running it via command lien, then this issue will never arise)

On your dev machines, its all good with mvn install, but when you are deploying a build on production or test server, its best to go with mvn clean before doing a mvn install to make sure no issues arise and all the classes are built once before wrapping them in a jar/war or anything

like image 101
Saif Asif Avatar answered Sep 17 '22 14:09

Saif Asif