Adding a custom jar as a maven dependency

Using maven in a Java project is great. It manages builds (as customized as you may need), executions, dependencies… In fact, dependencies is, in my opinion, the key feature of maven. Whenever a dependency is added to a project, maven will search for it at repositories, download it and store it, tagging versions.

Some weeks ago I had to get an old project without maven and make it work with it. Everything went right at first: I had only to search for the correct dependency on the repository and config my project to get it. But suddenly I found a jar that wasn’t available on the repository: it was a custom generated jar to include some fonts that were being used by Jasper on a pdf generation.

I had to get it working without adding the jar to the repository. I got some working solutions:

Adding a system dependency


I wouldn’t do it. System scope was created to add dependencies that once where external libraries but now are packed into the JDK. Also, the Assembly plugin ignores this kind of dependencies and it doesn’t pack them with the others.

Creating a maven repo inside the project

The idea is to create a maven repo that runs on our own computer instead or a remote server. Every file needed would be on the version control system so every developer can build the project once it is downloaded.

Three steps are needed to accomplish this:

1. Add the local repository to the project pom:


In this case the repository will be stored on a directory called “my-repo” and it will be located in the project root directory.

2. Install jar in the repository through install-plugin.

On maven 2.2:

mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
-Dfile=<path-to-file> -DgroupId=<myGroup> \
-DartifactId=<myArtifactId> -Dversion=<myVersion> \
-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>

On maven 2.3 and onwards:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \
-DartifactId=<myArtifactId> -Dversion=<myVersion> \
-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>

There are several options to be filled on the command:

  • path-to-file: the path to the file of the artifact to install.
  • myGroup, myArtifactId, myVersion: the group, artifact name and version of the artifact to install.
  • myPackaging: the packaging of the artifact (ie: jar).
  • path: the path to the local repo.

3. Add the dependency to the project as usual.

After running the command of the second step, several files will be created on the local repository (like pom and checksum files). This is the reason I don’t like this solution, I find it ugly with those files added to the VCS.

Installing the jar by using install-plugin

The idea is to store the jar into a project directory and install it into the m2repo directory (where all mvn dependencies are downloaded to) on build time. To get this working the dependency should be added to the repository1 as usual, and also add the plugin to the pom configuration:


This is the solution I decided to use on the project I was working on. It’s clean and it’ll work from the first moment a new developer downloads the project from the VCS.

  1. I mean the source code repository: SVN, GIT or whatever you use. []

26 thoughts on “Adding a custom jar as a maven dependency

  1. Pingback: Add “mpicbg” to Maven dependency for SIFT | Guangyu Wu

  2. Marcelo Fonseca

    Thanks for the post. It was very useful to me!
    There is only one problem: the correct phase for install plugin is ‘install’, not ‘initialize’

  3. sunil

    Can you please share your whole pom.xml if you were to choose the option “Installing the jar by using install-plugin”, I can’t seem to get it working? What command did you run after changing the pom? Thanks

  4. giancarlo

    Thank you.
    I’m new to Maven.
    Could you explain a bit more what “the dependency should be added to the repository as usual” means?
    Thanks in advance

    1. val Post author

      Sorry for the late response. I’m very busy these days, I wasn’t able to take a look to new comments…

      It’s been a while since I wrote the post, I think I meant to add the library to your project sources in a folder (in the example, in a folder called “lib” inside the project base dir).

      I hope this fast response has been helpful 🙂

  5. Emmanuel

    Thank you. This was very helpful. Just to clarify:

    Do we use the same code and just change the jar file name if we want to include more than one library?

    e.g twilio and spring jar files

  6. Nick

    thanks a lot for this note. I’ve spent quite a lot of time to find this solution. Because initialize doesn’t work.

  7. manolinux

    Thanks man, you saved my day.

    For not so interesting reasons I had to use the second way (Creating a maven repo inside the project).

    The problem was that maven required the repo to had checksums (I don’t know if it’s because is a more modern version).

    [WARNING] Checksum validation failed, no checksums available for file: …

    The solution was to add the following to the command that imports the file into the repo:


    Maybe you want to update the post, or at least someone finds useful the comment.

  8. LastTerese

    I see you don’t monetize your website, don’t waste your traffic, you
    can earn extra bucks every month because you’ve got hi quality content.
    If you want to know how to make extra money, search for: Mertiso’s tips best adsense alternative

  9. mai danh

    Hi @Eduardo
    With maven install plugin how i can install that jar file in remote repository instead of local repository.
    Thank you very much

  10. BestRoseanna

    I see you don’t monetize your website, don’t waste your traffic,
    you can earn extra bucks every month. You can use the best adsense alternative for any type of website (they approve all websites), for
    more info simply search in gooogle: boorfe’s tips monetize your website

  11. BestElizabet

    I have noticed you don’t monetize, don’t waste your traffic, you can earn extra cash every month with new monetization method.
    This is the best adsense alternative for any type of website (they approve all sites), for more
    details simply search in gooogle: murgrabia’s tools


Leave a Reply

Your email address will not be published. Required fields are marked *