Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Maven dependency to local JAR

farenda 2017-08-03 0

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:

<dependency>
   <groupId>algs</groupId>
   <artifactId>algs4</artifactId>
   <version>1.0</version>
</dependency>

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:

    <repositories>
       <repository>
         <id>lib-dir-repo</id>
         <url>file://${project.basedir}/lib</url>
       </repository>
    </repositories>
    
  5. Use 3rd Party library in Maven project:

    <dependency>
       <groupId>algs</groupId>
       <artifactId>algs4</artifactId>
       <version>1.0</version>
    </dependency>
    

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="https://maven.apache.org/POM/4.0.0"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>algorithms</groupId>
    <artifactId>java</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- Tell Maven about repository in the "lib" dir: -->
    <repositories>
        <repository>
            <id>lib-dir-repo</id>
            <url>file://${basedir}/lib</url>
        </repository>
    </repositories>

    <dependencies>
        <!-- Dependency to 3rd Party library -->
        <dependency>
            <groupId>algs</groupId>
            <artifactId>algs4</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.2</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Verification

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/PercolationTest.java:

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

import static org.junit.Assert.assertEquals;

public class PercolationTest {

    @Test
    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] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

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

References:

  • “Algorithms 1” at Coursera by Robert Sedgewick and Kevin Wayne
  • JUnit Tutorial for those who want to develop fast
Share with the World!
Categories Dev Tools Tags algorithms, java, maven
Previous: Caesar cipher in Java
Next: Java 8 Date Time concepts

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok