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

  <dependencies>
    <dependency>
      <groupId>groupId</groupId>
      <artifactId>artifactId</artifactId>
      <version>1.0</version>
      <scope>system</scope>
      <systemPath>${basedir}/lib/xx.jar</systemPath>
    </dependency>
  </dependencies>

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:

<repositories>
  <repository>
    <id>my-local-repo</id>
    <url>file://${basedir}/my-repo</url>
  </repository>
</repositories>

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:

    <plugin>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-install-plugin</artifactId>
	    <version>2.4</version>
	    <executions>
	        <execution>
	            <phase>initialize</phase>
	            <goals>
	                <goal>install-file</goal>
	            </goals>
	            <configuration>
	                <groupId>myGroupId</groupId>
	                <artifactId>myArtifactId</artifactId>
	                <version>myVersion</version>
	                <packaging>jar</packaging>
	                <file>${basedir}/lib/xxx.jar</file>
	            </configuration>
	        </execution>
	    </executions>
	</plugin>

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. []

21 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’
    Thanks!

    Reply
    1. Alex

      Caracas Marcelo, valeu! ajudou pra caramba… eu tava sofrendo pra caramba aqui, quando resolvi olhar os comentários rsrs

      Reply
  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

    Reply
  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

    Reply
    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 :)

      Reply
  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

    Reply
  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.

    Reply
  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:

    -DcreateChecksum=true

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

    Reply
  8. 95Michal

    Hello blogger, i must say you have high quality articles here.
    Your website can go viral. You need initial traffic boost only.
    How to get it? Search for: Mertiso’s tips go viral

    Reply

Leave a Reply

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