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.

  1. The jar you want to use doesn’t exist in the Maven center repository.
  2. 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.

References

  1. Maven install file documentation
  2. Kaptcha website
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: ionizer loans()

  • Pingback: water ionizer()

  • Pingback: level 2 electricians()

  • Pingback: electrician gloves 600v()

  • Pingback: house blue()

  • Pingback: water ionizer plans()

  • Pingback: stop parking()

  • Pingback: stop parking()

  • Pingback: water ionizer()

  • Pingback: laan penge billigt()

  • Pingback: mp4 mobile porn()

  • Pingback: lan penge online()

  • Pingback: Towheaded outdoor internal cumshot()

  • Pingback: streaming movies()

  • Pingback: stream movies()

  • Pingback: Blue Coaster33()

  • Pingback: Your first JavaFX app | Programming Percolatory()

  • Pingback: ADD JAR OR LIBRARY TO NETBEANS MAVEN PROJECT. | snippetjournal()

  • Suresh

    Hai it’s great..

    Thank u so much..

  • Amol Sharma

    Thanx Man !
    Very Helpful :)

  • gurpinars

    I did like that but im getting:
    missing artifact

  • Meenakshi

    Thanks!. Worked for me

  • nizar

    hi mkyong
    you are the best really

  • Pingback: Menambahkan Library Pada Repository Local Maven di Netbeans | HaPe()

  • Hiway

    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.

  • Sunil

    What is that you haven’t still written about! I found an article for everything I look around for! Thanks!

  • http://[email protected] anand babu

    Well, it worked for me too.. thanks man!

  • Parris

    Worked for me! Thanks :).

  • Osmel de jesus Quintero Muñoz

    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

  • tumit

    How to add dependencies of jar in command line?

  • Jayaram

    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

    • Jakub

      this command works fine when run in plain cmd instead of win powershell

  • Siddhanta Kumar Pattnaik

    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 ………

  • Siddhanta Kumar Pattnaik

    Hi All,
    Can any one tell me what is the purpose of , in tag and -Dpackaging property in the mvn command ………

  • gagaeloel

    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 … “

    • gagaeloel

      forget my answer, i was wrong

  • Abhishek

    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!! :)

  • AKS

    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?

    • http://www.mkyong.com mkyong

      Sorry, never use nexsus. But, nexsus should have a screen to let user to add the library manually, try search it.

  • praneeth

    How to create custom project structure with Maven (step by step mkyong style).

    Thank you,
    Praneeth.

  • red

    Hi! Thank you for tutorial but how can I add a jar library that uses native external libraries (.dll, .so)?

    • http://www.mkyong.com mkyong

      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.

  • Pingback: Maven Tutorial()

  • Pingback: How to add Oracle JDBC driver in your Maven local repository()

  • Sharaf

    thanx alot it made simple to add my own jar and not to place into nexus.

    • http://macluq.com Macluq

      Yeah, but sometimes you need to add it into Nexus (so other people can use it in your organization, for example).

      • http://www.mkyong.com mkyong

        Yes, if your company is using Nexus or others to host a Maven repository server.

        • tecowiz

          Hi mkyong,
          Thanks for such wonderful maven tutorials.

  • Pingback: How to include library manually into maven local repository? « Thinking Mind()

  • Pingback: Struts 2 + Hibernate integration with “Full Hibernate Plugin”()

  • serortlop

    Thanks a lot!!!!!
    it was really a great. Simple and easy as everything must be.

  • Issam

    Thanks a lot… Your steps were simple and very helpful…

  • Enkay

    Thanks for the detailed simple steps!!!

  • Pingback: How to convert Eclipse Java web project to Maven project | Maven()

  • prabhu

    Thanks a lot for your effort. It helped a lot. Thanks!

  • http://www.skoach.com Alex Turetsky

    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)

    • mkyong

      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.

      • Skb

        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.