How do I take a jar file that I have and add it to the dependency system in maven 2? I will be the maintainer of this dependency and my code needs this jar in the class path so that it will compile.
5 Answers
You'll have to do this in two steps:
1. Give your JAR a groupId, artifactId and version and add it to your repository.
If you don't have an internal repository, and you're just trying to add your JAR to your local repository, you can install it as follows, using any arbitrary groupId/artifactIds:
mvn install:install-file -DgroupId=com.stackoverflow... -DartifactId=yourartifactid... -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jarfile
You can also deploy it to your internal repository if you have one, and want to make this available to other developers in your organization. I just use my repository's web based interface to add artifacts, but you should be able to accomplish the same thing using mvn deploy:deploy-file ...
.
2. Update dependent projects to reference this JAR.
Then update the dependency in the pom.xml of the projects that use the JAR by adding the following to the element:
<dependencies>
...
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
-
3Can you add more detail regarding the deploy? We have an internal repository, and I want to deploy my local jar installed file to that repository so other developers and our build server will get the jar as well. – Kieveli Jan 11 '09 at 19:01
-
This answer should get all the likes! I've used this to successfully add dependencies for my maven projects on both Google App Engine and Heroku. – BrockLee Jan 06 '15 at 18:53
You can also specify a dependency not in a maven repository. Could be usefull when no central maven repository for your team exist or if you have a CI server
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>commons-utils</artifactId>
<version>1.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/commons-utils.jar</systemPath>
</dependency>

- 8,733
- 4
- 28
- 27
Actually, on investigating this, I think all these answers are incorrect. Your question is misleading because of our level of understanding of maven
. And I say our because I'm just getting introduced to maven
.
In Eclipse
, when you want to add a jar file to your project, normally you download the jar manually and then drop it into the lib directory. With maven, you don't do it this way. Here's what you do:
- Go to mvnrepository
- Search for the library you want to add
- Copy the
dependency
statement into yourpom.xml
- rebuild via
mvn
Now, maven
will connect and download the jar
along with the list of dependencies, and automatically resolve any additional dependencies that jar
may have had. So if the jar
also needed commons-logging, that will be downloaded as well.

- 17,663
- 14
- 81
- 103

- 10,944
- 6
- 56
- 81
-
5I am using Spring Tool Suite (built on Eclipse). After steps 1 & 2 in above solution: 1. Open pom.xml in STS editor, click "Dependencies" tab at bottom 2. Click "Add" in Dependencies group 3. Copy/paste "Maven" tab dependency info from browser window (groupId, artifactId, version) into corresponding fields in "Select Dependency" popup in STS 4. Click OK 5. Save pom.xml Once saved, my maven directory on hard drive and maven dependency setting in STS is updated within seconds. You can edit the .xml directly, but using the tool may help avoid xml format problems. – Chris R Jan 16 '13 at 19:34
-
2This is indeed the preferred way to do things, but there are situations where you can't do that (e.g. proprietary jar not on mvnrepository). In that case you'll need a way to add it "manually", in which case another answer would have the solution. – Thunderforge Feb 13 '14 at 22:19
-
It's also possible that the question asked was less of a beginner question. I put this in for people looking to do something without the proper phrasing that comes from time spent working with maven. (As I was at the time) – Kieveli Feb 20 '14 at 12:08
-
K.I.S.S. Thank you, I read that this is the C# Nuget equivalent in Java but I was trying to figure out what button to click or cmd to run. Not exactly a 1:1 but does the job. – Terrance May 08 '15 at 17:54
-
-
Correct (reference dependencies versus download JAR) but Eclipse, IntelliJ and others are IDEs, having nothing to do with the build process (e.g., Maven) per se. Rather Eclipse can invoke Maven but a command-line example is more succinct. – Darrell Teague Jan 19 '16 at 16:10
I'd do this:
add the dependency as you like in your pom:
<dependency> <groupId>com.stackoverflow...</groupId> <artifactId>artifactId...</artifactId> <version>1.0</version> </dependency>
run
mvn install
it will try to download the jar and fail. On the process, it will give you the complete command of installing the jar with the error message. Copy that command and run it! easy huh?!

- 20,269
- 16
- 67
- 115

- 171
- 2
I'll assume that you're asking how to push a dependency out to a "well-known repository," and not simply asking how to update your POM.
If yes, then this is what you want to read.
And for anyone looking to set up an internal repository server, look here (half of the problem with using Maven 2 is finding the docs)

- 38,754
- 10
- 77
- 102
-
I would like to maintain a dependancy myself. This is an in-house library. – Milhous Dec 28 '08 at 15:33