454

What exactly are the differences between mvn clean package and mvn clean install? When I run both of these commands, they both seem to do the same thing.

hichris123
  • 10,145
  • 15
  • 56
  • 70
  • 14
    Please look at [Introduction to the Build Lifecycle - Lifecycle Reference](http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference) and looking for `package` and `install` . – Charlee Chitsuk May 17 '13 at 05:31
  • 4
    One packages (builds it in target) and on installs (packages and places it in you repository) You need to do the later if you want to use this version in another module. – Peter Lawrey May 17 '13 at 05:40
  • 3
    Perhaps this should be reopened? It's perfectly possible to answer and I didn't find it difficult to answer. Also, if someone is brand new to maven, it's easy to see how the two phases appear to do the same thing. I'll edit it in an attempt to make it less vague. – Daniel Kaplan Sep 12 '14 at 21:52

6 Answers6

547

Well, both will clean. That means they'll remove the target folder. The real question is what's the difference between package and install?

package will compile your code and also package it. For example, if your pom says the project is a jar, it will create a jar for you when you package it and put it somewhere in the target directory (by default).

install will compile and package, but it will also put the package in your local repository. This will make it so other projects can refer to it and grab it from your local repository.

Documentation

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
357

What clean does (common in both the commands) - removes all files generated by the previous build


Coming to the difference between the commands package and install, you first need to understand the lifecycle of a maven project


These are the default life cycle phases in maven

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • verify - run any checks on results of integration tests to ensure quality criteria are met
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

How Maven works is, if you run a command for any of the lifecycle phases, it executes each default life cycle phase in order, before executing the command itself.

order of execution

validate >> compile >> test (optional) >> package >> verify >> install >> deploy

So when you run the command mvn package, it runs the commands for all lifecycle phases till package

validate >> compile >> test (optional) >> package

And as for mvn install, it runs the commands for all lifecycle phases till install, which includes package as well

validate >> compile >> test (optional) >> package >> verify >> install


So, effectively what it means is, install commands does everything that package command does and some more (install the package into the local repository, for use as a dependency in other projects locally)

Source: Maven lifecycle reference

Ketan R
  • 4,039
  • 1
  • 13
  • 15
  • 27
    This should be marked as accepted answer. This clearly defines and helps the OP to understand what the different phases of maven lifecycle and why mvn install includes mvn package but not the other way around. – Jasmeet Singh Sep 30 '19 at 20:17
28

package will generate Jar/war as per POM file. install will install generated jar file to the local repository for other dependencies if any.

install phase comes after package phase

martin
  • 2,150
  • 1
  • 34
  • 48
Abdul Gafoor
  • 913
  • 10
  • 12
20

package will add packaged jar or war to your target folder, We can check it when, we empty the target folder (using mvn clean) and then run mvn package.
install will do all the things that package does, additionally it will add packaged jar or war in local repository as well. We can confirm it by checking in your .m2 folder.

Nisarg Patil
  • 1,509
  • 1
  • 16
  • 27
11

Package & install are various phases in maven build lifecycle. package phase will execute all phases prior to that & it will stop with packaging the project as a jar. Similarly install phase will execute all prior phases & finally install the project locally for other dependent projects.

For understanding maven build lifecycle please go through the following link https://ayolajayamaha.blogspot.in/2014/05/difference-between-mvn-clean-install.html

Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105
  • 1
    What do you mean by " & finally install the project locally for other dependent projects " – Arun Gowda Jan 23 '18 at 06:25
  • 2
    @ArunGowda it means in the .m2 which is created in your system upon installing of the maven will be added with projects you are packaging and will be available for other projects as dependency. – JAVA Sep 19 '18 at 12:53
  • a picture is a 1000 words, anyone got a diagram? –  Nov 20 '18 at 21:53
  • @MrCholo *mvn install* will put the jar/war into your maven repo. The place where all your maven dependencies get stored, usually ~/.m2 – brt Mar 14 '19 at 14:45
6

mvn package command will compile source code and also package it as a jar or war as per pom file and put it into the target folder(by default).

mvn install command will compile and package, but it will also put the package in your local repository. So that other projects can refer to it and grab it from your local repository.

mvn install command is mostly used when you wants to compile a project(library) which other projects in your repository are depending on.

ABODE
  • 958
  • 2
  • 15
  • 13