What does mvn install in maven exactly do
Asked Answered
A

11

219

I just started using Maven and I was told to do mvn install in a specific directory.

What does mvn install do, exactly?

I think it looks for pom.xml in the current folder and starts following the instructions specified in that file. Is that correct?

Aleron answered 10/5, 2012 at 12:18 Comment(1)
maven.apache.org/guides/getting-started/…Syringe
C
159

As you might be aware of, Maven is a build automation tool provided by Apache which does more than dependency management. We can make it as a peer of Ant and Makefile which downloads all of the dependencies required.

On a mvn install, it frames a dependency tree based on the project configuration pom.xml on all the sub projects under the super pom.xml (the root POM) and downloads/compiles all the needed components in a directory called .m2 under the user's folder. These dependencies will have to be resolved for the project to be built without any errors, and mvn install is one utility that could download most of the dependencies.

Further, there are other utils within Maven like dependency:resolve which can be used separately in any specific cases. The build life cycle of the mvn is as below: LifeCycle Bindings

  1. process-resources
  2. compile
  3. process-test-resources
  4. test-compile
  5. test
  6. package
  7. install
  8. deploy

The test phase of this mvn can be ignored by using a flag -DskipTests=true.

Corny answered 13/9, 2012 at 13:13 Comment(2)
Just a side note, if you want to skip all the test related goals, use: -Dmaven.test.skip=true (instead of -DskipTests=true)Wimer
This answer fails to clarify that the install command ultimately installs the artifact locally, that is, in the local repository.Steelworker
A
43

Have you looked at any of the Maven docs, for example, the maven install plugin docs?

Nutshell version: it will build the project and install it in your local repository.

Aorist answered 10/5, 2012 at 12:21 Comment(3)
Let's say you run "mvn install" on an open-source library. Then, you run "mvn install" on a project that uses the open-source library that we first compiled. When running "mvn install", will JARs first be looked up in the local repo? Or is it dependent on the POM file?Louls
@Kevin It should go local first, since that's kind of the point. If it's not in the local repository it will look in whatever repositories you have configured (or the defaults) and install it to your local repo if it's found. I suspect you could configure it to always skip the local repo, although I don't know how without looking it up.Aorist
Have you looked at the canonical documents for Maven? Apache's site explains the purpose of mvn install thus: "In most cases, install:install goal doesn't need any configuration, it needs the project's POM and the artifact file to be installed during the install phase of the default build lifecycle." It is because Apache's doc fails to explain what 'mvn install' actually does that people like the poster, and myself, are looking for the answer. Perfectly reasonable question.Hilliary
B
9

The install:install goal is provided by «Apache Maven Install Plugin»:

Apache Maven Install Plugin

The Install Plugin is used during the install phase to add artifact(s) to the local repository. The Install Plugin uses the information in the POM (groupId, artifactId, version) to determine the proper location for the artifact within the local repository.

The local repository is the local cache where all artifacts needed for the build are stored. By default, it is located within the user's home directory (~/.m2/repository) but the location can be configured in ~/.m2/settings.xml using the <localRepository> element.

Apache Maven Install Plugin - Introduction.

Having said that, the exact goal purpose:

install:install is used to automatically install the project's main artifact (the JAR, WAR or EAR), its POM and any attached artifacts (sources, javadoc, etc) produced by a particular project.

Apache Maven Install Plugin - Introduction.

For additional details on the goal, please refer to the Apache Maven Install Plugin - install:install page.

For additional details on the build lifecycle in general and on which place the goal has in the build lifecycle, please refer to the Maven – Introduction to the Build Lifecycle page.

Buehrer answered 26/10, 2017 at 10:39 Comment(0)
B
9

Short answer

mvn install

  • adds all artifact (dependencies) specified in pom, to the local repository (from remote sources).
Bailar answered 14/5, 2019 at 13:55 Comment(1)
You forgot to mention that it also builds the project.Trave
R
8

It will run all goals of all configured plugins associated with any phase of the default lifecycle up to the "install" phase:

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

Rhaetic answered 10/5, 2012 at 12:42 Comment(0)
B
4

We have -DskipTests=true and -Dmaven.test.skip=true

"maven.test.skip.exec=true" the tests will be compiled, but will not executed.

"maven.test.skip=true" doesn't compile or execute the tests.

"-DskipTests" is the same as "maven.test.skip.exec=true"

Make changes in Setting.xml in your .m2 folder. You can use link to local repo so that the jars once downloaded should not be downloaded again and again.

<url>file://C:/Users/admin/.m2/repository</url>
 </repository>
Bonedry answered 4/4, 2013 at 6:36 Comment(1)
-Dmaven.test.skip=true does not compile the tests. However, -DskipTests=true does. Therefore, they are not the same. They are common in not running tests.Hannan
C
3

At any stage of maven build life cycle, all the previous goals are performed.

Ex: mvn install will invoke mvn validate, mvn compile, mvn test, mvn package etc.

Castra answered 14/10, 2020 at 17:55 Comment(0)
I
3

It's important to point out that install and install:install are different things, install is a phase, in which maven do more than just install current project modules artifacs to local repository, it check remote repository first. On the other hand, install:install is a goal, it just build your current project and install all it's artifacts to local repository (e.g. into the .m2 directory).

Inhume answered 29/1, 2021 at 8:53 Comment(0)
S
2

mvn install primary jobs are to

1)Download The Dependencies

2)Build The Project

while job 1 is nowadays taken care by IDs like intellij (they download for any dependency at POM)

mvn install is majorly now used for job 2.

Sicanian answered 3/12, 2019 at 8:52 Comment(0)
J
1

In general we should use mvn package instead of mvn install, which mistake I have seen everywhere

Mvn install copy generated jar from target to .m2 repository, which may generate some problem for developer in future

After my 7+ years of experience I suggest developers to use mvn package insted of mvn install

Jejune answered 24/1, 2023 at 14:23 Comment(0)
D
1

The specific 'install' part of a mvn install simply just extracts your packaged jar file (typically in your project's target folder) to your .m2 local repository. That is it.

While these answers stating that it downloads all the remote dependencies are all technically true, this has caused me and other developers a great amount of confusion over the years. While yes, running mvn install technically pulls down the remote dependencies in your pom.xml, maven is actually doing this as a pre-step in one of the earlier phases of the lifecycle. The earliest phase in the lifecycle where this happens is the compile phase. If all you want to do is pull down pom dependencies and compile your code you could just run mvn compile. You can do this without running install and avoid running tests and packaging your code.

Install is simply just a phase in the default build lifecycle that runs all the previous phases in order: validate, compile, test, package, verify, install.

Defunct answered 28/10, 2023 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.