Lately I’ve been tasked with developing a Java library for internal use. For testing, its proved useful to package the library for local use. This article describes how to add a Jar file to a local Maven repository for use in your own testing and development.

Create your local Maven repository

Your local Maven repository lives within the project you are developing for. Creating your local repository is as simple as making a new directory.

mkdir repo

Adding your artifact into your local Maven repository

Once you have a repository, you can deploy your artifact into it. This uses the mvn deploy goal and requires a few parameters:

url         # the location of your repo
file        # the location of your jar file
groupId     # your group id (com.sookocheff)
artifactId  # your artifact id (devlib)
packaging   # jar
version     # version number

Putting this together gives us the following command:

mvn deploy:deploy-file -Durl=file:///path/to/repo/ -Dfile=devlib-0.1.jar
-DgroupId=com.sookocheff -DartifactId=devlib -Dpackaging=jar -Dversion=0.1

After running this command, Maven will place your jar file in the local repo/ directory in the required format.

Adding your local repository to your Pom file

Now you can edit your pom.xml file to add the local repository to use for testing.

<repositories>
  <repository>
    <id>project.local</id>
    <name>project</name>
    <url>file:${project.basedir}/repo</url>
  </repository>
</repositories>

Adding your dependency

Now you can add your jar file as a dependency in the usual way.

<dependency>
  <groupId>com.sookocheff</groupId>
  <artifactId>devlib</artifactId>
  <version>0.1</version>
</dependency>

Committing your Changes

You can also safely commit the repo/ folder and your updated pom.xml for other developers to use.