How to add local jar files to maven projects

We are going to learn how to install a local jar repository.

By default, Maven installs the dependencies from the maven remote repository or nexus repository.

Sometimes, we need to install the jar files from the local repository or lib folder, or custom path folder.

There are multiple ways we can install local dependencies into maven projects.

  • maven install:install-file command
  • Adding local dependencies systemPath in pom.xml
  • Adding local repository

How to install jar file to the local repository with commandline

install-file goal is a maven commands used to install local jar files

It is an easy and simple way to install a jar from a local repository or path of the jar file.

Here is a command to install the jar file directly

mvn install:install-file \
   -Dfile=jar-file-path> \
   -DgroupId=com.company.feature \
   -DartifactId=feature \
   -Dversion=version \
   -Dpackaging=packaging \
   -DgeneratePom=true
   -DcreateChecksum=true
  • install:install-file: goal installs to the local repository and has the following parameters
  • file-path: of jar file can be an absolute or relative path
  • groupId: jar file group id
  • artifactId: artifact id of a jar file
  • version: jar version
  • packaging: jar or war or ear file

this command outputs and installs to the local repository .m2/repository/com.company.feature/feature.jar/war file.

load jar file with local repository configuration

The second method, adding the lib folder path in the repository tag of pom.xml.

In the below code snippet, Created a local repo pointing to the lib of a project directory.

This repository can also be added in .m2/settings.xml too which applies to all maven projects.

<repositories>
    <repository>
        <id>local-repo</id>
        <url>file:///${project.parent.basedir}/lib</url>
    </repository>
</repositories>

You have to add the dependency in dependencies of a pom.xml

For example, feature.jar presented in the lib folder

<dependency>
        <groupId>com.company.feature</groupId>
        <artifactId>feature</artifactId>
        <version>1.0.0</version>
</dependency>

Next, run the below command to install the jar file in the application.

mvn deploy:deploy-file -DgroupId=com.company.feature -DartifactId=feature -Dversion=1.0.0 -Durl=file:./local-repo/ -DrepositoryId=local-repo -DupdateReleaseInfo=true -Dfile=file:///${project.parent.basedir}/lib

how to install local repository jar with systemPath of dependency?

Finally, This is straightforward to load the jar file.

Please note that systemPath contains jar file location.

<dependency>
    <groupId>com.company.feature</groupId>
    <artifactId>feature</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/feature.jar</systemPath>
</dependency>

Wrap up

Loading jar files from the local folder can be achieved in multiple ways. You can choose the approach as per your needs.