Most of the time when we built a maven project we use JAR files that are hosted in the Maven Central Repository.
Maven Central Repository can be accessed via the following URL: https://mvnrepository.com/
In this post let us explore, how to install third-party JAR to a project which is not hosted in the Maven Central Repository.
Third-party JAR can be a proprietary JAR that is not hosted publicly OR it can be a custom JAR which you have written.
There are a lot of options available to install a third-party JAR to the maven project.
- Installing the JAR locally to the .m2 repository.
- Adding it to the project as a system-scoped file.
- Adding the JAR as an IntelliJ project dependency.
- Using a repository management tool like Nexus Or Archiva.
- Publishing the dependency to Maven Central.
Installing the JAR locally to the .m2 repository
To use third party JARs that don’t exist in any public repository like Maven Central, we need to put them to the local repository to be used by the builds.
The JARs must be placed in the local repository in the correct place in order for it to be correctly picked up by Apache Maven. To make this easier, and less error-prone, there is a goal in the maven-install-plugin which makes this relatively painless.
To install a JAR in the local repository use the following command:
The groupId, artifactId, and version details can be found from the downloaded third party JAR itself. Generally we can find this information from the pom.properties file or the pom.xml file residing in the <Extracted_JAR>/META-INF/maven/<package_name>/<class_name>/ directory.
For example, let say you have downloaded the WSO2 claim manager
JAR file claim-manager-4.0.0.jar.
Extract the JAR file and navigate to claim-manager-4.0.0/META-INF/maven/org.wso2.samples.is/claim-manager/ directory.
There we can find the pom.properties file and pom.xml file.
We can find the groupId, artifactId, and version details from the pom.properties file.
To install the claim-manager jar file to the local repository we can execute the command as below.
Alternatively, if we have a pom.xml file available in the third party jar we can use it as well to install the jar to the local repository as below.
We can also associate a sources jar with the executable jar while installing the third-party jar locally.
Let’s say you have a jar file with sources (claim-manager-4.0.0-sources.jar) which you need to associate with the executable jar file (claim-manager-4.0.0.jar) while installing.
We can use the following command with an additional -Dsources option which lets us install the sources along with the executable jar.
Now that the Jar file is installed in our local repository, it can be referenced by the builds.
Let us continue with the remaining options in the upcoming blogs.
Thank you for reading!!!