Multiple ways to add local jar file to maven projects
In this tutorial, we will explore the process of installing a local JAR repository. By default, Maven installs dependencies from either the Maven remote repository or Nexus repository into the local repository on the first encounter.
However, there are situations where we may need to install JAR files from a local repository, a lib
folder, or a custom path folder. Fortunately, there are multiple approaches available for incorporating local dependencies into Maven projects:
- Using the mvn install:install-file command
- Adding local dependencies’ systemPath in pom.xml
- Adding a local repository:
How to install jar file to the local repository with command line
The install-file
goal is a maven commands used to install local JAR files.
This method offers a straightforward and convenient way to install a JAR from a local repository or the path of the JAR file itself.
Below is a command demonstrating how to install a JAR file directly using this approach:
mvn install:install-file \
-Dfile=jar-file-path> \
-DgroupId=com.company.feature \
-DartifactId=feature \
-Dversion=version \
-Dpackaging=packaging \
-DgeneratePom=true
-DcreateChecksum=true
The install:install-file
goal is utilized to install a Local JAR file into the local Maven repository and includes the following parameters:
file-path
: Represents the path to the JAR file and can be specified as either an absolute or relative path.groupId
: Denotes the group ID of the JAR file.artifactId
: Specifies the artifact ID of the JAR file.version
: Indicates the version of the JAR file.packaging
: Specifies the packaging type of the JAR file, which can be java-archive (jar
), web application (war
), or Enterprise Application(ear
).
This command both outputs and installs the JAR/WAR file to the local repository at .m2/repository/com.company.feature/feature.jar
.
With this command and the provided parameters, Maven correctly installs the specified JAR file into the local repository, including the specified group ID, artifact ID, version, and packaging information.
This makes it easier to seamlessly integrate the JAR file into Maven projects.
Configure the local repository to load a JAR file.
The second approach involves including the path to the lib
folder in the repository tag of the pom.xml
file. In the code snippet below, a local repository is created with a reference to the lib
folder within the project directory. It’s worth noting that this repository configuration can also be added to the .m2/settings.xml
, providing a global application to all Maven projects.
<repositories>
<repository>
<id>local-repo</id>
<url>file:///${project.parent.basedir}/lib</url>
</repository>
</repositories>
The next step is to include a local dependency in the dependencies
section of the pom.xml
.
For instance, if there’s a feature.jar
in the lib
folder, it can be added as a dependency in the project’s pom.xml file as given below
<dependency>
<groupId>com.company.feature</groupId>
<artifactId>feature</artifactId>
<version>1.0.0</version>
</dependency>
Next, execute the following 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
In this method, a local folder is added as a repository to the Maven project. When the install
command is executed, it downloads the JAR file from the local repository and installs it in the project. However, there is a drawback to this approach. If there is a new version of the JAR file, you need to repeat the same steps for the updated file.
This process applies only to the local scope of the current project. It doesn’t facilitate the reuse of the local repository in other projects, and it also lacks automatic handling of new versions.
How to install the local repository jar with systemPath of dependency?
Finally, utilizing the systemPath approach makes it straightforward to load the JAR file.
It’s important to note that the systemPath
in the dependency section of the pom.xml
contains the location of the JAR file. The systemPath
includes the absolute
or relative
path to the file.
In this example, project.basedir
is a variable that points to the base directory of the project.
<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>
The JAR file is installed in the local repository when the ‘install’ command is executed.
Wrap up
There are several methods available for loading JAR files from a local folder. You can select the approach that best suits your requirements.