How to include custom library into maven local repository?
There are 2 cases that you need to issue Maven’s command to include a jar into the Maven local repository manually.
- The jar you want to use doesn’t exist in the Maven center repository.
- You created a custom jar, and need to use for another Maven project.
P.S Trust me, there are still many jars that doesn’t support Maven.
Case study
For example, kaptcha, a popular third party Java library, which is used to generate “captcha” image to stop spam, but it’s not available in the Maven center repository.
In this tutorial, we will show you how to install the “kaptcha” jar into your Maven’s local repository.
1. mvn install
Download the “kaptcha“, extract it and copy the kaptcha-version.jar to somewhere else, for example, c drive. Issue following command :
mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
Demo.
D:\>mvn install:install-file -Dfile=c:\kaptcha-2.3.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'install'. [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Default Project [INFO] task-segment: [install:install-file] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] [install:install-file] [INFO] Installing c:\kaptcha-2.3.jar to D:\maven_repo\com\google\code\kaptcha\2.3\kaptcha-2.3.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: < 1 second [INFO] Finished at: Tue May 12 13:41:42 SGT 2009 [INFO] Final Memory: 3M/6M [INFO] ------------------------------------------------------------------------
Now, the “kaptcha” jar is copied to your Maven local repository.
2. pom.xml
After installed, just declares the kaptcha coordinate in pom.xml.
<dependency> <groupId>com.google.code</groupId> <artifactId>kaptcha</artifactId> <version>2.3</version> </dependency>
3. Done
Build it, now the “kaptcha” jar is able to retrieve from your Maven local repository.

hi mkyong
you are the best really
Hi,mkyong, I am installing ojdbc6.jar to my local repository, the install command is just like you write in article and ojdbc6.jar is copied to local repo, then I try to search this artifact in eclipse pom Dependencies panel, I can’t find this artifact, I try to add in pom.xml manually, it works. So what can I do to make sure I can search out this newly installed artifact? My command line maven and eclipse maven use the same local repository.
I get answer from stackoverflow:http://stackoverflow.com/questions/16355108/why-cant-i-find-my-own-installed-artifact-in-eclipse-search-dialog, hope can help others with same question.
What is that you haven’t still written about! I found an article for everything I look around for! Thanks!
Well, it worked for me too.. thanks man!
Worked for me! Thanks :).
Hi mkyong,
I can not install any one plugin into my local repository, and I don´t know why.
Into cmd:
C:\>mvn install:install-file -Dfile=c:\maven-clean-plugin-2.4.1.jar -Dgroupid=or
g.apache.maven.plugins -DartefactId=maven-clean-plugin -Dversion=2.4.1 -Dpackagi
ng=jar
[INFO] Scanning for projects…
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-
clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom
14-dic-2012 16:39:54 org.apache.maven.wagon.providers.http.httpclient.impl.clien
t.DefaultRequestDirector tryExecute
INFO: I/O exception (java.net.SocketException) caught when processing request: C
onnection reset
How to add dependencies of jar in command line?
Hi,
I followed the steps you mentioned.
Step 1: using wsimport I generate the class file
Step 2: using jar -cvf command I create a jar named TestClient.jar
Step 3: From the same directory I issued the command like:
mvn install:install-file -Dfile= /Users/Jayaram/TestClient/FriendsMirrorRegisterClient.jar -DgroupId=com.friendsmirror.register.ws.client -DartifactId=friendsMirrorRegisterClient -Dversion=0.1 -Dpackaging=jar
OutPut:
[INFO] Scanning for projects…
[INFO] ————————————————————————
[INFO] BUILD FAILURE
[INFO] ————————————————————————
[INFO] Total time: 0.108s
[INFO] Finished at: Sun Jun 24 16:13:36 GMT+05:30 2012
[INFO] Final Memory: 2M/81M
[INFO] ————————————————————————
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/Users/Jayaram/TestClient). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException
I tried to run the same command from a maven based project directory where pom.xml is located and getting the below out put:
[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building FriendsMirrorWAR Maven Webapp 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-install-plugin:2.3.1:install-file (default-cli) @ FriendsMirrorWAR —
[INFO] Installing /Users/Jayaram/Program/FriendsMirrorWAR to /Users/Jayaram/mavenrepo/com/friendsmirror/register/ws/client/friendsMirrorRegisterClient/0.1/friendsMirrorRegisterClient-0.1.jar
[INFO]
[INFO] ————————————————————————
[INFO] Skipping FriendsMirrorWAR Maven Webapp
[INFO] This project has been banned from the build due to previous failures.
[INFO] ————————————————————————
[INFO] ————————————————————————
[INFO] BUILD FAILURE
[INFO] ————————————————————————
[INFO] Total time: 0.781s
[INFO] Finished at: Sun Jun 24 16:09:52 GMT+05:30 2012
[INFO] Final Memory: 3M/81M
[INFO] ————————————————————————
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file (default-cli) on project FriendsMirrorWAR: Error installing artifact ‘com.friendsmirror.register.ws.client:friendsMirrorRegisterClient:jar’: Failed to install artifact com.friendsmirror.register.ws.client:friendsMirrorRegisterClient:jar:0.1: /Users/Jayaram/Program/FriendsMirrorWAR (No such file or directory) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
I tried to google out for the same but not yet good luck.
Could you please help out on this.
Thaks
this command works fine when run in plain cmd instead of win powershell
Hi All,
Can any one tell me what is the purpose of groupId,arifactId tag in dependency tag and -Dpackaging property in the mvn command ………
Hi All,
Can any one tell me what is the purpose of , in tag and -Dpackaging property in the mvn command ………
For me works this:
mvn install:install-file -DgroupId=com.google.code.kaptcha -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar -Dfile=kaptcha-2.3.jar
instead of “…-DgroupId=com.google.code … “
forget my answer, i was wrong
Hi,
I have a requirement in which I have to uplaod the internally created jar(project related) to repo without version.
Please sujjest how to do that.
Thanks!! :)
Great, you are so good to explain things in a simple manner. How do we do the same if I have to install a library or project to nexsus repository?
Sorry, never use nexsus. But, nexsus should have a screen to let user to add the library manually, try search it.
How to create custom project structure with Maven (step by step mkyong style).
Thank you,
Praneeth.
Hi! Thank you for tutorial but how can I add a jar library that uses native external libraries (.dll, .so)?
I don’t think Maven cant handle it automatically. You can still use Maven to add Java library but put your .dll or .so manually into project classpath.
thanx alot it made simple to add my own jar and not to place into nexus.
Yeah, but sometimes you need to add it into Nexus (so other people can use it in your organization, for example).
Yes, if your company is using Nexus or others to host a Maven repository server.
Hi mkyong,
Thanks for such wonderful maven tutorials.
Thanks a lot!!!!!
it was really a great. Simple and easy as everything must be.
Thanks a lot… Your steps were simple and very helpful…
Thanks for the detailed simple steps!!!
Thanks a lot for your effort. It helped a lot. Thanks!
Make sure to add -Dpackaging=jar to that mvn:install command. This parameter is not optional. (In your instructions, it appears in the result section, but not in the command)
hi -Dpackaging=jar is a must in order for the install the mavan library manually into our local repository. Post is updated , thanks for it.
I Have one problem. This command is ok for a single Custom jar file. But we have 17 custom jars files in the file system. I do not want to write dependency in pom.xml file for each and every jars instead inside one dependecy through which all the jars will be available to build patha. Is there any way to to this. Please suggest me.