Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Maven, Why Run 'mvn clean'?

Tags:

java

maven

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.

like image 837
Kevin Dolan Avatar asked Jan 11 '11 20:01

Kevin Dolan


People also ask

Why do we use mvn clean?

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.

When should you run Maven clean?

The Clean Plugin is used when you want to remove files generated at build-time in a project's directory.

What happens when we call mvn 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.

What is the use of clean install?

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.


2 Answers

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.

like image 111
Gareth Davis Avatar answered Sep 28 '22 14:09

Gareth Davis


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.

like image 30
Stephen C Avatar answered Sep 28 '22 15:09

Stephen C