How to add local jar from local repository in maven projects


In this tutorials, We are going to learn how to installs local jar repository

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

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

maven load and install local repository jar

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 local repository with command line

install-file goal command used to install local jar files

This is easy and simple way to install jar from local repository or path of jar file.

Here is an command to install 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 following parameters file - path of jar file, can be absolute or relative path groupId- jar file group id artifactId - artifact id of an jar file version - jar version packaging - jar or war or ear file

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

load jar file with local repository configuration

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

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

This repositories 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 an pom.xml

For example feature.jar presented in lib folder

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

Next run below command to install 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 and simple to load 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 file from local folder can be achieved by multiple ways. You can choose the approach as per your needs.

Similar Posts