Maven
Dependency Management
JAR files
Java Build Tools
Project Management

Maven: add a dependency to a JAR by relative path with systemPath

Codemia Knowledge Hub ·

To depend on a JAR by relative path, declare it in pom.xml with <scope>system</scope> and point <systemPath> at the file using $&#123;project.basedir&#125;. This works for quick tests and development, but installing the JAR into a repository with mvn install:install-file is usually the better long-term option.

Maven normally resolves dependencies from a local or remote repository. A relative-path JAR comes up with proprietary libraries or pre-release builds that are not published anywhere, and Maven was not really designed for it.

Declaring a system-scoped dependency

Dependencies go in the <dependencies> section of pom.xml, the XML file (Project Object Model) that holds the project's build configuration. For a JAR on disk, use system scope and give the path in the systemPath element:

xml
1    <dependency>
2        <groupId>com.example</groupId>
3        <artifactId>example-jar</artifactId>
4        <version>1.0</version>
5        <scope>system</scope>
6        <systemPath>${project.basedir}/libs/example-jar-1.0.jar</systemPath>
7    </dependency>

Maven then looks for the JAR relative to the project's base directory (for example ./libs/example-jar-1.0.jar). $&#123;project.basedir&#125; is the directory that contains pom.xml.

AttributeDescriptionExample
groupIdThe group ID of the project.com.example
artifactIdThe ID of the artifact (project).example-jar
versionThe version of the artifact.1.0
scopeScope of the dependency usage.system
systemPathPath to the JAR on the local file system.$&#123;project.basedir&#125;/libs/example-jar.jar

Why system scope is discouraged

System dependencies make builds less portable, because the JAR must exist at the same location on every machine that builds the project. They also get hard to manage in large projects or when the JAR changes often. Alternatives:

  • Install the JAR into your local repository with Maven's install-file goal. The library then behaves like any other Maven artifact.
  • Deploy it to a private or in-house repository. For teams and larger projects, this lets the artifact be managed like every other dependency.

Installing the JAR with install-file

bash
1mvn install:install-file -Dfile=path-to-your-artifact-jar \
2                         -DgroupId=your.groupId \
3                         -DartifactId=your-artifactId \
4                         -Dversion=version \
5                         -Dpackaging=jar

After this, declare the dependency in pom.xml normally, without a system path.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms