DEV Community

loading...

Adding local jar files to a Maven project

wakeupmh profile image Marcos Henrique ・1 min read

I was developing a project with spring and maven, however when performing the build I came across a strange error

org.springframework.beans.factory.unsatisfieddependencyexception error creating bean with name 

Even though my jar is in the build path, the error persisted, I tried to install it with the java -cp of life, but the problem persisted, that's when I tried something a little different, directly add the dependency in pom.xml, after all it is an abstraction of the manifest in spring application

Solution

There are many ways to add local jar files to a Maven project:

  • Install manually the JAR into a local Maven repository use this command:
mvn install:install-file –Dfile=C:\dev\app.jar -DgroupId=com.mh.tutorial-DartifactId=my-example-app -Dversion=1.0

Now, add the dependency to your Maven project by adding these lines to your pom.xml file:

<dependency>
    <groupId>com.mh.tutorial</groupId>
    <artifactId>my-example-app</artifactId>
    <version>1.0</version>
</dependency>
  • Adding directly the dependency as system scope Consider that the JAR is located in <PROJECT_ROOT_FOLDER>/lib. After this add the dependency in your pom.xml file as following:
<dependency>
    <groupId>com.mh.tutorial</groupId>

    <artifactId>my-example-app</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/myCustomJAR.jar</systemPath>

</dependency>

Discussion

pic
Editor guide
Collapse
khmarbaise profile image
Karl Heinz Marbaise

I have to mentioned that the system scope will produce a WARNING since Maven 3.5.2 furthermore if you use a system scope dependency it will become hard for consumers of your project cause they don't have the directory nor the jar on their hard drive which results in failures. That means in other words you should prevent using system scope under all circumstances cause it will cause many issues.