Java Programming Tutorials

Java programming tutorials with many code examples!

Maven dependency to local JAR

Did you ever need to configure Maven dependency to local JAR? It’s rarely needed, but when it is, it’s good to know how to do that correctly.

Usually such libraries should be installed into company or local repository, but sometimes you can’t or want to keep them together with your project.

Frankly, I didn’t have such a case for a long, long time, until… recently! I’ve enrolled to “Algorithms 1” at Coursera by Robert Sedgewick and Kevin Wayne. The thing is that they give a library (JAR) that should be used to solve programming assignments. I’m TDD type of person and like to have IDE properly configured to run Unit tests for things I’m working on. Therefore I’ve decided to configure Maven to make their JAR available in my project.

Maven local dependency options

There are a few options to include 3rd party JARs:

  1. Install into local repository (~/.m2/repository).
  2. Keep in a lib directory along the project.
  3. Add as system dependency (deprecated).

I won’t talk about the third option as it will be removed from Maven soon and should not be used.

Install dependency into local repository

The first and the simplest option is to just install 3rd Party libraries to a local repository:

mvn install:install-file \
    -Dfile=algs4.jar -DgroupId=algs -DartifactId=algs4 \
    -Dversion=1.0 -Dpackaging=jar

Here file should be a path to a JAR you want to install. groupId and artifactId are just made up ids – use whatever you want.

This will copy the jar into your local Maven repository (~/.m2/repository) and create a meta-POM for it (Maven configuration file that describes the library). Then you can just add it into your dependencies:


3rd Party lib in project directory

Sometimes installation to local Maven repository is not an option and you want to distribute 3rd Party libraries together with project sources. In this case you can define Maven that a directory should become a repository and install 3rd Party dependencies there.

To make things concrete, let’s say we want to have algs4.jar (see Percolation assignment) in a lib directory, along with project sources. To do that we need to do the following steps:

  1. Make project directory structure:

    mkdir -p my-project/src/main/java
    mkdir -p my-project/src/test/java
    mkdir -p my-project/lib
  2. Go to project directory:

    cd my-project
  3. Install algs4.jar into repository in lib directory:

    mvn install:install-file \
        -Dfile=algs4.jar -DgroupId=algs -DartifactId=algs4 \
        -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=lib

    As previously, the file should be a path to a JAR you want to install. groupId and artifactId are just made up ids – use whatever you want.

  4. Tell Maven about repository with 3rd Party libs:

  5. Use 3rd Party library in Maven project:


Full pom.xml for “Algorithms 1” Course

A full Maven configuration for the course may look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""



    <!-- Tell Maven about repository in the "lib" dir: -->

        <!-- Dependency to 3rd Party library -->



To check that the library is really available to Maven we can write a sample test that is using a class from the algs4.jar library. Add the following content into src/test/java/

import edu.princeton.cs.algs4.StdRandom;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class PercolationTest {

    public void shouldHaveAccessToAlgs4() {
        assertEquals(0, StdRandom.uniform(1));

Run tests in Maven

mvn test

The above code produces the following output:

[INFO] Scanning for projects...
... maven runs
[INFO] Surefire report directory: /home/farenda/algorithms1/target/surefire-reports

 T E S T S
Running PercolationTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.264 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------

As you can see the test have been compiled and run successfully! :-)


Share with the World!