76

I am wondering what the major difference between running mvn compile and mvn clean compile are, in practicality.

I understand what the actual difference is, that mvn clean compile deletes all the generated files and starts again from scratch, but why would we want to do this? I can assume mvn compile will regenerate files if it's necessary, right?

One thing I noticed in my project was that if you had deleted a source file, without running clean, the compiled file remains, which usually wouldn't be a problem, but could be I suppose.

2
  • Interesting question. I feel that when using "make" for c/c++ applications it is not often a clean is required as it handles dependencies between source files. I guess it handles the case of deletion/renames too.
    – Marius K
    Jun 22, 2012 at 9:24
  • Especially useful when checking out and running a previous commit or after bulk copying files.
    – phil294
    May 20, 2018 at 5:11

4 Answers 4

53

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.

0
41

As noted in Gareth's answer, when you rename or remove a source class, Maven doesn't have sufficient information to know to remove the corresponding compiled file from the previous build. The presence of the stale file can cause unexpected runtime problems. A clean is required to get rid of the stale files so that they doesn't get accidentally included in WARs, JARs and so on.

In addition, certain plugins require a clean in order to work properly. For example (at least in Maven 2), the maven-war-plugin explodes each dependent WAR into an existing directory tree. A clean is required to get rid of stale files left over from previous versions of the dependent WARs.

I can assume "mvn compile" will regenerate files if it's necessary, right?

For mainstream plugins, that is a fair assumption. However, if you are using a plugin to generate source code components, I'd look carefully at the documentation, and at where you put the generated source code. For instance, there are a couple of unsupported plugins whose purpose is to drive the Eclipse EMF code generator.

0
1

If you don't do clean compile then it means you are still allowing to work with some obsolete classes. If your module suppose to migrate to new class then even you missed that, there won't be any compilation error due to old class exist in target/classes. This will remain unnoticed till same module is built at some other place/machine with clean compile goal.

0

On Maven, each time you want to compile, the best practice is to run mvn clean. It clears out the existing classes that you compiled from last compile. If you don't want to run 3 lines, just do "mvn test" after mvn clean. You don't have to always do mvn compile.

1
  • 2
    Maybe I didn't understand your answer but it seems like you can run mvn test to avoid the compilation step compile. However mvn test will run the phase compile too Jan 21, 2019 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.