73

What are the differences between Maven's mvn install and mvn verify commands?

How does the keyword clean modify these commands?

0

3 Answers 3

86

mvn verify - as said before - performs any integration tests that maven finds in the project.

mvn install implicitly runs mvn verify and then copies the resulting artifact into your local maven repository which you usually can find under C:\Users\username\.m2\repository if you are using windows.

If you run maven multiple times without the clean command and without changing any source code, you may notice that it says Nothing to compile - all classes are up to date during the compile phase. If you add the clean command before any other command, maven will simply delete the entire target directory resulting in all classes being recompiled.

1
  • How to run a particular integration test? not all @vatbub Commented Oct 13, 2022 at 11:15
25

From https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html:

mvn install - install the package into the local repository, for use as a dependency in other projects locally

mvn verify - run any checks on results of integration tests to ensure quality criteria are met

clean is a lifecycle that handles project cleaning. Commands involving clean before it will clear the entire directory, meaning that all the classes have to be recompiled.

2
3

I would like to add more points to elaborate on the accepted answer.

Maven has 3 build life cycles default, clean and site

The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's website.

Each of these life cycles consists of phases. default has 23 phases, clean has 3 phases and site consists of 4 phases.

(For a complete list of phases please refer to this Lifecycle Reference)

  • install and verify are phases in the default build lifecycle.
  • phases are executed in a sequential manner. eg : When you execute mvn install it will execute all the preceding phases as well.(Which includes verify phase)

Each of the said phases consists of goals. Goals are provided by the plugins.

A Build Phase is Made Up of Plugin Goals

However, even though a build phase is responsible for a specific step in the build lifecycle, the manner in which it carries out those responsibilities may vary. And this is done by declaring the plugin goals bound to those build phases.

So, The outcome of these two commands may differ with the plugins you are using and the phases they are bound to in your project.


When we execute phases or goals the order of execution is derived from the order we invoke them.

(ref: Execution Order)

For example, consider the command below. The clean and package arguments are build phases, while the dependency:copy-dependencies is a goal (of a plugin).

mvn clean dependency:copy-dependencies package

Don't get confused by the mention of clean as a phase here. clean lifecycle has a phase called clean as well.

enter image description here

If this were to be executed, the clean phase will be executed first (meaning it will run all preceding phases of the clean lifecycle, plus the clean phase itself), and then the dependency:copy-dependencies goal, before finally executing the package phase (and all its preceding build phases of the default lifecycle).


In answer to your questions,

  1. What are the differences between Maven's mvn install and mvn verify commands?
  • mvn verify

In most cases the effect is the same as package. However, in case there are integration-tests, these will be executed as well. And during the verify phase some additional checks can be done, e.g. if your code written according to the predefined checkstyle rules. ( ref: Usual Command Line Calls, FailSafe plugin, Checkstyle Plugin)

  • mvn install

install the package into the local repository, for use as a dependency in other projects locally.

  1. How does the keyword clean modify these commands?

As mentioned before clean is a phase of the clean lifecycle. It will run both the pre-clean and clean phases in the clean lifecycle. It will invoke prior to the install and verify phases in each case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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