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?
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.
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.
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.
If you have only the standard maven plugins from the default superpom, adding a dependency will:
There are several maven plugins which can be used to include dependencies in a package. These include:
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.
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 -
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With