How Maven automatically resolves jar library dependencies?


Publication Date:January 17, 2021



INFOMARTION > How Maven automatically resolves jar library dependencies?

summary

Maven automatically downloads jars from the Internet, but I would like to explain exactly how it works.

The premise of this article is that you need to understand what Maven is, so if you are not familiar with Maven itself, please also refer to the following article.

What is Maven and what does Maven do?

Table of Contents

  1. Maven's ability to resolve jar library dependencies
  2. summary

1. Maven's ability to resolve jar library dependencies

Maven can automatically retrieve the jar file by simply defining the required jar file in the pom.xml file. It also automatically learns the dependent jars needed to run that jar. I would like to explain how it works.

1-1. What to include in the pom.xml file

First, to resolve the dependency of the jar library, information is provided in the pom.xml file. Maven processes based on the information in the pom.xml file. If you need "poi-3.17.jar", for example, you can put it in pom.xml as follows. (Incidentally, "poi-3.17.jar" is a jar used to manipulate Microsoft Office files such as Excel.)

pom.xml


<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

Simply enter the above information, and "poi-3.17.jar" will be downloaded from the Internet, and the accompanying jar necessary to run "poi-3.17.jar" will also be downloaded from the Internet.

1-2. How is it downloaded from the Internet?

A simple explanation of the system is shown in the figure below.

Download from Maven repositories available on the Internet. If nothing is set, the default value is Central Repository. The URL is as follows

https://repo1.maven.org/maven2/

Please see the following site for a list of repositories available on the Internet if you are interested.

https://mvnrepository.com/repos

The first time the jar is fetched from the external repository, it is copied to the local repository and is not fetched from the second time onward. The location of the local repository is "C:\Users\user\.m2".

If you want to change the destination from the default value, you can also change the destination by including the following repository tag in pom.xml.

pom.xml


<repositories>
    <repository>
        <id>sonatype-nexus-snapshots</id>
        <name>Sonatype Nexus Snapshots</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
        </snapshots>
    </repository>
</repositories>

1-3. Mechanism to resolve dependencies of jars to be acquired

We have just described an example of obtaining "poi-3.17.jar", but "poi-3.17.jar" requires "commons-collections4-4.1.jar" to run. In other words, "poi-3.17.jar" depends on "commons-collections4-4.1.jar". The mechanism for resolving this dependency can be found in the Maven repository where "poi-3.17.jar" is stored. The URL is as follows

https://repo1.maven.org/maven2/org/apache/poi/poi/3.17/

I think "poi-3.17.pom" is stored in this file, and information that "poi-3.17.jar" depends on "commons-collections4-4.1.jar" is described in this file. In other words, when retrieving "poi-3.17.jar," the system not only retrieves the jar file but also the pom file and checks for other necessary jar files.

2. summary

Maven's mechanism for resolving jar library dependencies is that the dependencies are defined in the pom file where the jar files to be retrieved are stored.

Thank you for taking the time to read this to the end.




■INFORMATION

Please click here to go to the top page of INFORMATION.


■PROFILE

Please click here to view the profile.


■For inquiries, please contact

For inquiries about the article, please contact us here.