Learn How to Handling External Dependencies in Maven

1
1457
Handling External Dependencies

Handling External Dependencies

In the last article, we discussed how to build and test the JAVA application project created through Maven. In this article, we going to discuss on handling external dependencies in Maven. In the earlier articles, we have discussed about different types of Maven repositories and through these repositories, the actual dependency management is taken care by Maven. Consider the scenario when the required project dependency is not available in local, remote as well as in the central repository. In such scenario, the concept of external dependency comes into picture.

Let’s quickly recap what are local, central and remote repositories in Maven and then we will jump to external dependencies concept.

Local Maven Repository
A local Maven repository is nothing but a folder which is located on your local machine. The Maven plugin creates a folder for when you execute any maven command for the very first time. The purpose of a local repository is to keep all your project related dependencies such as library JARs, plugin JARs, etc. at one place. These dependencies are automatically downloaded into your local repository when you kick in a Maven build. Such a practice is very beneficial as it helps to mitigate the references to dependencies again and again which are available on remote machine during building a Maven project.

By default, a local Maven repository is created at %USER_HOME% directory by Maven plugin. If you want to override this default location, then you need to provide a different path in then Maven’s setting.xml file which is present at %M2_HOME%\conf directory. The following will be the content of setting.xml file which has the customized path defined for local Maven repository.

Upon execution of the Maven command, the required project dependencies will be downloaded at the given customized path on your local machine.

Central Maven Repository
Central Maven repository is referred to the repository which is provided by Maven community. It is a giant repository that contains a large number of library JARs which are used very commonly. The following are the key concepts of central Maven repository.
• It is a repository which is wholly managed by Maven community.
• No configuration is required for this repository.
• Central repository can be searched through internet access. Its content can browsed through a URL − https://search.maven.org/#browse provided by Maven community. All the library JARs which are available at central repository can be searched and used by developer through this URL.

Remote Maven Repository
Remote Maven repository is very useful and comes into picture when the required library JARs are not even spotted in the central Maven repository that may result into the failure of build process after throwing the error message on the console. In order to mitigate such situations, the concept of remote Maven repository comes into the picture which is nothing but a customized repository that contains required project’s library JARs. In the following example, the POM.xml has number of remote repositories which will download the required dependencies (i.e. library JARs) when they are not present on the local repository as well as central repository.

Handling External Dependencies
As an example, we have an existing Java project created in the last article where we are going make the following changes.
• Adding a lib folder under the src folder.
• Copying any customer jar (say hamcrest-core-1.3.jar) into the lib folder.
After making above changes in the project, our project structure should look as shown below.
our project structure
Here, we have our custom lib folder that contains a JAR library. This library is specific to our project and it is not present in the local, central and remote repositories. If we build our code with this library through Maven, then our Maven build is going to fail as it cannot refer to or download this library from any of its available repositories during the compilation phase. We can still handle this situation by adding this external dependency into maven pom.xml in the following manner.

Source code for about how to handle external dependencies in Maven

Conclusion: –
In this article, we discussed about handling the external dependencies in Maven when they are not present in local, remote and central repositories.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here