Though Apache Maven downloads dependencies from public or private repositories while running from our application. Sometimes, we might need 3rd party JAR’s which wouldn’t be available in public or private repositories like Maven central. In those scenarios, we would need to be installing 3rd party JARs in Maven repository of our local machine.

Apache Maven picks up JAR’s only when it was placed in correct location in the local repository. To make this easier and error free, Maven provides a goal in the maven-install-plugin which should make this relatively painless. To install a JAR in the local repository using the following command:

Commands for Installing 3rd Party JAR’s in Maven Respository

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

If there is POM file available for the JAR, you can install using below command

mvn install:install-file -Dfile=<path-to-file> \
                         -DpomFile=<path-to-pomfile>

If the JAR was built by Apache Maven, it’ll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default. In that case, all you need to do is:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=<path-to-file>

Apache Maven Install Plugin

The Install Plugin uses the information in the POM (groupId, artifactId, version) to determine the proper location for the artifact within the local repository.

By default, it is located within the user’s home directory (~/.m2/repository) but the location can be configured in ~/.m2/settings.xml using the <localRepository> element.

Goals Overview of Install plugin

The Install Plugin has 3 goals:

  • install:install is used to automatically install the project’s main artifact (the JAR, WAR or EAR), its POM and any attached artifacts (sources, javadoc, etc) produced by a particular project.
  • install:install-file is mostly used to install an externally created artifact into the local repository, along with its POM. In that case the project information can be taken from an optionally specified POM file, but can also be given using command line parameters.
  • install:help displays help information on maven-install-plugin.

Usage

Apache Maven searches artifacts in local repository, which is the artifact cache on your system, by default located at ${user.home}/.m2/repository. When executing Maven, it’ll first look in this local cache for artifacts. If the artifact cannot be found, Maven will connect with remote repositories to find the artifact. Once found it will be downloaded and will be installing 3rd party JARs into the local Maven repository, so it’s be available for current and future usage.

Conclusion

We went through the commands for installing 3rd party JARs in local repository using Apache Maven install plugin. And we had overview about the goals in install plugin.