Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when I add a Maven dependency?

When I add a dependency to the pom.xml file and build with "mvn package", what steps does Maven take with that dependency code?

Does it physically grab the java code from that dependency's package and put it into the JAR? And why do I need to add the dependency at all ... why can't Maven just look at my import statements and fully qualified names and figure out which dependencies I need?

like image 939
user1956609 Avatar asked Apr 13 '15 20:04

user1956609


People also ask

What does Maven dependency do?

Transitive Dependencies. Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.

What happens when we add dependency in POM xml?

Maven will download the new dependencie(s) to your local repository and then use this to compile your JAR. If you are packaging your application as a web archive (WAR file), it would include the binaries of the dependency (and any binaries it depends on) in your WAR file. Save this answer.

Why do we add dependency?

So, whenever you add a dependency to your gradle file, it will download those libraries, and resolves it so that is available in your project. It makes it easy to manage external libraries for your project, rather than adding jar files manually.


2 Answers

If you have only the standard maven plugins from the default superpom, adding a dependency will:

  • download the specified version to your local repository
  • use it to compile
  • use it to run tests

There are several maven plugins which can be used to include dependencies in a package. These include:

  • maven-war-plugin
  • maven-ear-plugin
  • maven-assembly-plugin
  • appassembler-maven-plugin

In addition, the maven-shade-plugin can be used to combine your dependencies into the jar file of your own code.

None of these are used unless you add them to your pom.

like image 190
bmargulies Avatar answered Nov 15 '22 15:11

bmargulies


In general in all pom.xml we found dependency like this -

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

Here groupId, artifactId and version are 3 keys by which a jar is uniquely identified. These 3 combination works like a coordinate system for uniquely identifying a point in a space using x, y and z coordinates.

Whenever you issue a mvn package command maven tries to add the the jar file indicating by the dependency to you build path. For doing this maven follows these steps -

  1. Maven search in your local repository (default is ~/.m2 for linux). If the dependency/jar is found here then it add the jar file to you build path. After that it uses required class file from the jar for compilation.

  2. If the dependency is not found in ~/.m2 then it looks for your local private repository (If you already have configured any using setting.xml file) and maven central remote repository respectively. If you don't have any local private repository then it directly goes to the maven central remote repository.

  3. Whenever the jar is found in the local/remote repository it is downloaded and saved in ~/.m2.

Going forward, when you again issue a mvn package command then it's never search for the dependency to any repository since it already in your ~/.m2.

like image 43
Razib Avatar answered Nov 15 '22 17:11

Razib