Most of the libraries are available in Maven repository. Yet there are some third-party APIs are given as standalone jar files. I wrote about this earlier.
Add local jar files to Maven Build Path
Eclipse project dependency and Maven
Those approach will still work fine for Core Java applications. However, the scene is different when you build Spring Boot jars. Though the build is successful, Spring Boot single jar file will not carry the local jar. It will lead to ClassNotFoundException at runtime.
The purpose of this post is to resolve that problem. There are different solutions suggested in the forums. But this is what worked for me today.
- Install Maven
- Install local jars to Maven repository
- Use the jar in pom.xml
Install Maven
Download Maven binary from here. I downloaded the zip file for windows. I extracted to C:\tools\apache-maven-3.9.6
Open a CMD to check if our Maven deployment is working fine. You need to set JAVA_HOME to make Maven working. I point at the OpenJDK at my C drive.
set JAVA_HOME = c:\openjdk17
Issue the following command to get the version of Maven.
Maven home: c:\tools\apache-maven-3.9.6
Install local jar
Installation of maven will create the repository at your user folder.
Let’s install our local jar into Maven repository using the following syntax:
Refer to Guide to installing 3rd party JARs by Maven documentation, following is the syntax to do the same.
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
You may insert with maven file pom.xml as well. I haven’t tried.
c:\tools\apache-maven-3.9.6\bin\mvn install:install-file -Dfile=c:\external_libraries\ireasoningsnmp.jar -DgroupId=com.snmp -DartifactId=ireasoning -Dversion=7.2 -Dpackaging=jar
This will create the folder structure as per the group id, artifact id and version
The jar file will be copied inside.
Use the jar in pom.xml
Add this to your dependencies.
<dependency>
<groupId>com.snmp</groupId>
<artifactId>ireasoning</artifactId>
<version>7.2</version>
</dependency>
This will not only help you to run the application from STS, but also helps to add the local jar to your Spring Boot embedded jar.
—
This post is written as part of #WriteAPageADay campaign of BlogChatter