Maven add a dependency to a jar by relative path
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Maven is a build automation and dependency management tool used primarily for Java projects. It simplifies the build process, managing project dependencies, and enhancing project standardization and reporting. However, Maven typically relies on dependencies defined within a remote or central repository. There are scenarios, such as dealing with proprietary libraries or pre-release versions not available in a public repository, where you might need to include a JAR file located by a relative path. This process can be somewhat counterintuitive as Maven is designed to work with artifacts specified in the Maven repository format.
Understanding Maven Project Object Model (POM)
The Project Object Model or POM is Maven's fundamental unit of work. It is an XML file that contains information about the project and configuration details used by Maven to build the project. Dependencies in Maven are usually managed by adding them to the pom.xml file under the <dependencies> tag.
Adding a Dependency by a Relative Path
To use a JAR not available in a repository, include it directly by using a system scope and specifying the relative path. This approach can quickly become hard to manage in large projects or ones where the JAR needs to be updated frequently. However, it's suitable for quick tests or during the development phase.
Steps to Include a JAR by Relative Path:
- Local System Dependency: Define the dependency in your
pom.xmlwithsystemscope and provide the path to the JAR using thesystemPathelement, which specifies the path to the dependency. - Example Specification:
This configuration tells Maven to look for the specified JAR in the relative path under the project's base directory (e.g., ./libs/example-jar-1.0.jar). Here, ${project.basedir} represents the directory containing the pom.xml.
Best Practices and Considerations
Using system dependencies is generally discouraged unless necessary. It can lead to builds that are not portable, as the JAR needs to be present in the same location on every machine the project is built. Consider these alternatives:
- Install JAR to Local Repository: Use Maven’s
install-filegoal to install the JAR into your local Maven repository. This integrates the library into your Maven ecosystem, making it manageable and reusable without altering the Maven standard behavior. - Deploy to a Private or In-house Repository: For teams and larger projects, deploying the artifact to a private Maven repository might be a better solution. This way, it can be managed and maintained just like any other dependency.
Executing the install-file command:
This command will add your JAR to the local repository, and you can declare it in the pom.xml without using a system path.
Summary Table
| Attribute | Description | Example |
groupId | The group ID of the project. | com.example |
artifactId | The ID of the artifact (project). | example-jar |
version | The version of the artifact. | 1.0 |
scope | Scope of the dependency usage. | system |
systemPath | Path to the JAR on the local file system. | ${project.basedir}/libs/example-jar.jar |
Conclusion
Adding a JAR file by a relative path in Maven is straightforward but should be used cautiously due to the portability and maintainability issues it introduces. For a more scalable and robust solution, consider alternatives such as installing the JAR to the local repository or deploying it to a private repository. These methods integrate seamlessly with Maven's lifecycle and dependency resolution mechanisms, thereby preserving the advantages of using Maven.
Related reading
- Maven and Spring Boot - non resolvable parent pom - repo.spring.io Unknown host
- Maven best way of linking custom external JAR to my project?
- Maven clean install Failed to execute goal org.apache.maven.pluginsmaven-resources-plugin3.2.0resources
- Maven compile with multiple src directories
- Maven dependencies are failing with a 501 error
- Maven dependency for Servlet 3.0 API?
- Maven does not find JUnit tests to run
- Maven Failed to read artifact descriptor

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.