Roufid

3 ways to add local jar to maven project

Maven

Introduction

You may need to add a custom JAR as a dependency to your Maven project. This tutorial shows 3 ways to do it:

  1. Install manually the JAR into your local Maven repository
  2. Adding the dependency as system scope
  3. Creating a different local Maven repository
  4. Using a Nexus repository manager

1- Install manually the JAR into your local Maven repository

The first solution is to add manually the JAR into your local Maven repository by using the Maven goal install:install-file. The use of the plugin is very simple as below:

Note that we didn’t specify groupId, artifactId, version and packaging of the JAR to install. Indeed, since the version 2.5 of Maven-install-plugin, these information can be taken from an optionally specified pomFile.

These information can also be given in command line:

Where:

For example:

You can then add the dependency to your Maven project by adding those lines to your pom.xml file:

This solution can be very expensive. Why ? You have to consider that the day you change your local Maven repository you have to re-install the JAR. Or again, if there are many persons working on the project, each must install the JAR in his local repository. The portability of the project must be taken into account.

Another solution is to use the maven-install-plugin in your pom.xml which will install the jar during the Maven “initialize” phase. To do this, you must specify the location of the jar you want to install. The best way is to put the JAR in a folder created at the root of the project (in the same directory as the pom.xml file).

Let’s consider that the jar is located under <PROJECT_ROOT_FOLDER>/lib/app.jar. Below the configuration of maven-install-plugin:

${basedir} represents the directory containing pom.xml.

You may encounter an error while adding the previous lines, add the following plugin to your project to allow the lifecycle mapping:

2- Adding directly the dependency as system scope

Another solution – dirty solution – is by adding the dependency as system scope and refer to it by its full path. Consider that the JAR is located in <PROJECT_ROOT_FOLDER>/lib. Then add the dependency in your pom.xml file as following:

${basedir} represents the directory containing pom.xml.

3- Creating a different local Maven repository

The third solution is quite similar to the first one, the difference lies in the fact that the JARs will be installed in a different local Maven repository.

Let’s consider the new local Maven repository is named “maven-repository” and is located in ${basedir} (the directory containing pom.xml). First you have to do is deploying the local JARs in the new local maven repository as below:

Normally, the Maven deploy:deploy-file installs the artifact in a remote repository but in our case the repository is located in the local machine.

After installing the JARs your need to add the repository in your pom.xml file:

Then you can add the dependency into your pom.xml

4- Using Nexus repository manager

The best solution is to use a Nexus Repository Manager which will contain all your JARs and you will use it as repository to download the dependency.

This book from the official Nexus site will show you how to install and use Nexus repository manager.

Refrences