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.
The Maven Clean Plugin, as the name implies, attempts to clean the files and directories generated by Maven during its build. While there are plugins that generate additional files, the Clean Plugin assumes that these files are generated inside the target directory.
The Clean Plugin is used when you want to remove files generated at build-time in a project's directory.
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.
A clean install is a software installation in which any previous version is eradicated. The alternative to a clean install is an upgrade, in which elements of a previous version remain. The terms are often heard in reference to operating systems (OSes) and software applications.
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.
Certain plugins require a clean
in order to work properly. For instance (at least in Maven 2), the maven-war-plugin
explodes each dependent WAR into an existing directory tree. It requires a clean
to get rid of files that have been removed from the dependent WARs.
Another problem is that when you rename a class, the old compiled version can hang around in the build tree, and will get included in JAR files, etcetera ... until you run mvn clean
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With