3 Ways to add local jar to maven project

June 2, 2016

原文地址 http://roufid.com/3-ways-to-add-local-jar-to-maven-project/

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:

mvn install:install-file -Dfile=<path-to-file>

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:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>

Where:

  • _:_ Path to the JAR to install
  • : Group id of the JAR to install
  • : Artifact id of the JAR to install
  • Version of the JAR

For example:

mvn install:install-file –Dfile=C:\dev\app.jar -DgroupId=com.roufid.tutorials -DartifactId=example-app -Dversion=1.0

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

<dependency>
    <groupId>com.roufid.tutorials</groupId>
    <artifactId>example-app</artifactId>
    <version>1.0</version>
</dependency>

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 _/lib/app.jar._ Below the configuration of maven-install-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.roufid.tutorials</groupId>
                <artifactId>example-app</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>${basedir}/lib/app.jar</file>
            </configuration>
        </execution>
    </executions>
</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:

<pluginManagement>
    <plugins>
        <!--This plugin's configuration is used to store Eclipse m2e settings only. 
            It has no influence on the Maven build itself. -->
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>aspectj-maven-plugin</artifactId>
                                <versionRange>[1.0,)</versionRange>
                                <goals>
                                    <goal>test-compile</goal>
                                    <goal>compile</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute />
                            </action>
                        </pluginExecution>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>
                                    org.apache.maven.plugins
                                </groupId>
                                <artifactId>
                                    maven-install-plugin
                                </artifactId>
                                <versionRange>
                                    [2.5,)
                                </versionRange>
                                <goals>
                                    <goal>install-file</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute>
                                    <runOnIncremental>false</runOnIncremental>
                                </execute>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

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 _/lib._ Then add the dependency in your pom.xml file as following:

<dependency>
    <groupId>com.roufid.tutorials</groupId>
    <artifactId>example-app</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/yourJar.jar</systemPath>
</dependency>

_${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:

mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -Durl=file:./maven-repository/ -DrepositoryId=maven-repository -DupdateReleaseInfo=true

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:

<repositories>
    <repository>
        <id>maven-repository</id>
        <url>file:///${project.basedir}/maven-repository</url>
    </repository>
</repositories>

Then you can add the dependency into your pom.xml

<dependency>
    <groupId>com.roufid.tutorials</groupId>
    <artifactId>example-app</artifactId>
    <version>1.0</version>
</dependency>

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

2019/2/27 posted in  Java