Languages
[Edit]
EN

Maven - add *.jar library to *.pom (import local *.jar file)

4 points
Created by:
Violet-Hoffman
652

In this short article, we would like to show how to import *.jar file as a dependency inside *.pom configuration in Java Maven Project.

Quick solution:

<dependencies>
	<dependency>
		<groupId>my-jar</groupId>
		<artifactId>my-jar</artifactId>
		<version>1.0.0</version>
		<scope>system</scope>
		<systemPath>/path/to/my.jar</systemPath>
	</dependency>
</dependencies>

Note: see to the below example to know how to use a relative path to *.jar file,
e.g. ${pom.basedir}/lib/my.jar or ${project.basedir}/lib/my.jar.

 

Practical example

In this section, *.jar files are located in lib/ directory located in the project (on the same level where *.pom is placed).

Project structure:

/C/
 |
 +-- my-project/
      |
      +-- lib/
      |    |
      |    +-- my.jar
      |
      +-- src/
      |   |
      |   +-- Program.java
      |
      +-- pom.xml

pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>

	<groupId>my-project</groupId>
	<artifactId>my-project</artifactId>
	<version>1.0.0</version>

	<name>my-project</name>
	<packaging>jar</packaging>

	...

	<dependencies>

		...

		<dependency>
			<groupId>my-jar</groupId>
			<artifactId>my-jar</artifactId>
			<version>1.0.0</version>
			<scope>system</scope>
			<systemPath>${pom.basedir}/lib/my.jar</systemPath>
		</dependency>

        ...

    </dependencies>

</project>

Where:

  • <groupId>, <artifactId> and <version> describes an attached library - useful when JAR file is not described,
  • <scope> with system value, indicates file loading from the file system,
  • <systemPath> indicates path to *.jar file the file system,
  • ${pom.basedir} represents the directory where the pom.xml file is located.

Alternative titles

  1. Maven - attach *.jar library to *.pom (import local *.jar file)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join