Skip to content

Instantly share code, notes, and snippets.

@timmolderez
Last active May 2, 2024 05:24
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save timmolderez/92bea7cc90201cd3273a07cf21d119eb to your computer and use it in GitHub Desktop.
Save timmolderez/92bea7cc90201cd3273a07cf21d119eb to your computer and use it in GitHub Desktop.
Adding dependencies to local .jar files in pom.xml
If you'd like to use a .jar file in your project, but it's not available in any Maven repository,
you can get around this by creating your own local repository. This is done as follows:
1 - To configure the local repository, add the following section to your pom.xml (inside the <project> tag):
<repositories>
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>file://${project.basedir}/libs</url>
</repository>
</repositories>
2 - Let's say we want to use the file myLibrary-1.0.jar in our project. First add a dependency to your pom.xml:
<dependency>
<groupId>be.ac.vub</groupId>
<artifactId>myLibrary</artifactId>
<version>1.0</version>
</dependency>
Two things to note:
- You can pick whatever you like for groupId.
- The name of your .jar has to follow this naming convention: artifactId-version.jar
If needed, rename your .jar file.
3 - Put myLibrary-1.0.jar in the following location, relative to the root of your git repo:
libs/be/ac/vub/myLibrary/1.0
In general, your path looks like this: repository-location/groupId/artifactId/version
(Just like Java packages.. if the groupId contains a ".", this corresponds to a "/" in the file structure..)
4 - In that location, also add a file myLibrary-1.0.pom , with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>be.ac.vub</groupId>
<artifactId>myLibrary</artifactId>
<version>1.0</version>
<description>POM was created from install:install-file</description>
</project>
5 - You have now created your own local Maven repository! To test if it works, compile the project via Maven:
mvn clean install -U
(The -U is optional; it forces Maven to re-download project dependencies)
6 - Finally, make sure to add the .jar and .pom files to your git repository as well, so it also works via continuous integration.
@fnwirth
Copy link

fnwirth commented Nov 4, 2021

Thanks!

I get the warning Could not validate integrity of download from file://[...]r: Checksum validation failed, no checksums available.
@qin-liu: How exactly did you solve this problem?

thank you very much, I searched a lot ways but not work。this worked good。 and I add a notice, some version need add jar file checksum file, just checksum jar file online and add myLibrary-1.0.jar.sha1 file, copy checksum in it。to do same to pom file with myLibrary-1.0.pom.sha1

@ThanigaiQa
Copy link

My Jar looks empty in github. And even when I clone it I can't open it inside my IDE. Can anyone help me on this

@codingarchitect
Copy link

Thanks a lot!!!

@wolf75222
Copy link

Thanks That was very good !!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment