Skip to content

Instantly share code, notes, and snippets.

@timmolderez
Last active June 25, 2024 01:46
Show Gist options
  • 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.
@thhart
Copy link

thhart commented Apr 26, 2017

Cool, thanks for that. Is it possible to add multiple JAR files per library?

@romangromov
Copy link

romangromov commented Jan 22, 2018

That is the only one correct answer how to do it right! Thanks a lot!

@nguni52
Copy link

nguni52 commented May 4, 2018

This helped. Been foraging the internets for this solution. The thing that I was missing was the path to the jar file in the project lib directory. and that pom file in the directory with the pom. THANKS.

@erijonhson
Copy link

Thanks a lot!

@ratikanta131
Copy link

Worked for me! Thanks.

@davilima
Copy link

Thanks!!!

@jfranciscos4
Copy link

Thanks!!!

@gowthambala
Copy link

gowthambala commented Jun 25, 2019

Thanks for the great post. I tried this and my jar file is about 200 mb in size and when I use maven to do a build, it just hangs when its building that module and then eventually crashes with a out of memory heap error. If I undo my changes, everything works fine. Also if I move the dependency to the parent pom file, then it crashes without building any modules. So the issues I am having is definitely related to this specific change. Any idea why this might be happening?

@timmolderez
Copy link
Author

Thanks for the great post. I tried this and my jar file is about 200 mb in size and when I use maven to do a build, it just hangs when its building that module and then eventually crashes with a out of memory heap error. If I undo my changes, everything works fine. Also if I move the dependency to the parent pom file, then it crashes without building any modules. So the issues I am having is definitely related to this specific change. Any idea why this might be happening?

Hi @gowthambala , I suspect you'll need to give Maven a bit more memory to complete the build. Haven't tested this myself, but this should do the trick: https://subscription.packtpub.com/book/application_development/9781783983865/1/ch01lvl1sec10/configuring-the-heap-size

Cheers,
Tim

@SuperGod
Copy link

Thanks!!!

@anreddy4u
Copy link

Please explain how to include this jar in different sub modules.

@hyhypepe
Copy link

Thank you so much. It worked.

@neesonqk
Copy link

neesonqk commented Apr 8, 2020

FYI: you have to put

 <repositories>
      <repository>
        <id>in-project</id>
        <name>In Project Repo</name>
        <url>file://${project.basedir}/libs</url>
      </repository>
    </repositories>

at the top place before your jar

@TheDiamondDoge
Copy link

Thank you so much, good sir

@danicobe1
Copy link

Until now this still works!! Muchas pero muchas gracias!!! Thanks for sharing your experience and your knowledge me ayudaste bastante!! sobre 10 te pongo 1000 !!

@qin-liu
Copy link

qin-liu commented Jan 25, 2021

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

@MahmoudAbdelhameed
Copy link

so helpful , thanks a lot , by the way i faced a warning about the jar's pom file and i created the folder structure with this command instead of manually and it work like charm
mvn deploy:deploy-file -Durl=file:///path/to/yourproject/repo/ -Dfile=mylib-1.0.jar -DgroupId=com.example -DartifactId=mylib -Dpackaging=jar -Dversion=1.0

@GavinRay97
Copy link

Thank you so much!

@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