271

What is the difference between mvn clean install and mvn install?

hichris123
  • 10,145
  • 15
  • 56
  • 70
Leonid
  • 22,360
  • 25
  • 67
  • 91

5 Answers5

280

clean is its own build lifecycle phase (which can be thought of as an action or task) in Maven. mvn clean install tells Maven to do the clean phase in each module before running the install phase for each module.

What this does is clear any compiled files you have, making sure that you're really compiling each module from scratch.

nateyolles
  • 1,851
  • 5
  • 17
  • 23
Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 22
    I always thought install including clean as one of its phase... until i see this – Junchen Liu Mar 17 '15 at 14:45
  • 20
    @shanyangqu Sadly, no. `clean` is in a [separate lifecycle](http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference), so it's not called by default. – Powerlord Mar 17 '15 at 16:01
  • 1
    1. Re "_can be thought of as an action or task_": Maven phases are not _actions_ or _tasks_. If there's no plugin goal bound to a phase, neither an action nor a task, i.e. _nothing_ is done. A phase is more [a step or stage](https://stackoverflow.com/a/30953905/1744774). 2. Re "_do the `clean` phase_": With `mvn clean` it passes the `pre-clean` and the `clean` phase of the `clean` lifecycle and executes plugin goals bound to each phase. 3. Re "_clear any compiled files_": Not only those but everything that resides under `${project.build.directory}`: `.jar`, resources, folders, etc. – Gerold Broser Nov 22 '20 at 02:37
  • 1
    @JunchenLiu There are actually four items named `clean`: `clean` lifecycle with its `clean` phase, `clean` [prefix](https://maven.apache.org/guides/introduction/introduction-to-plugin-prefix-mapping.html) for the `maven-clean-plugin` with its `clean` goal. See also the comment to [this answer to _Maven: Lifecycle vs. Phase vs. Plugin vs. Goal_](https://stackoverflow.com/a/41466540/1744774). – Gerold Broser Sep 17 '21 at 12:51
61

Maven lets you specify either goals or lifecycle phases on the command line (or both).

clean and install are two different phases of two different lifecycles, to which different plugin goals are bound (either per default or explicitly in your pom.xml)

The clean phase, per convention, is meant to make a build reproducible, i.e. it cleans up anything that was created by previous builds. In most cases it does that by calling clean:clean, which deletes the directory bound to ${project.build.directory} (usually called "target")

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
44

You can call more than one target goal with maven. mvn clean install calls clean first, then install. You have to clean manually, because clean is not a standard target goal and not executed automatically on every install.

clean removes the target folder - it deletes all class files, the java docs, the jars, reports and so on. If you don't clean, then maven will only "do what has to be done", like it won't compile classes when the corresponding source files haven't changed (in brief).

we call it target in ant and goal in maven

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
32

To stick with the Maven terms:

  • "clean" is a phase of the clean lifecycle
  • "install" is a phase of the default lifecycle

http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

Puce
  • 37,247
  • 13
  • 80
  • 152
  • There are actually four items named `clean`: `clean` lifecycle with its `clean` phase, `clean` [prefix](https://maven.apache.org/guides/introduction/introduction-to-plugin-prefix-mapping.html) for the `maven-clean-plugin` with its `clean` goal. See also the comment to [this answer to _Maven: Lifecycle vs. Phase vs. Plugin vs. Goal_](https://stackoverflow.com/a/41466540/1744774). – Gerold Broser Sep 17 '21 at 13:19
  • True, but clean in `mvn clean install` refers to the phase. – Puce Sep 18 '21 at 14:00
  • 1
    Yes, it does. My comment was not meant to be criticism but additional information for all future readers and seekers here. – Gerold Broser Sep 18 '21 at 14:04
7

Ditto for @Andreas_D, in addition if you say update Spring from 1 version to another in your project without doing a clean, you'll wind up with both in your artifact. Ran into this a lot when doing Flex development with Maven.

mezmo
  • 2,441
  • 19
  • 22