Thursday, 9 July 2015

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.

1. mvn install

put your jar file in some directory (directory name should not be contains spaces), for example, c:\dependency\test.jar drive.
Issue following command in command prompt without line break:

mvn install:install-file -Dfile=c:\dependency\test-{version}.jar -DgroupId=test -DartifactId=testclient -Dversion={version} -Dpackaging=jar

Demo :-

C:\Users\pbachu>mvn install:install-file -Dfile=c:\dependency\test-1.0.jar -DgroupId=test -DartifactId=testclient -Dversion=1.0 -Dpackaging=jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) @ standalone-pom ---
[INFO] Installing c:\dependency\test-1.0.jar to D:\m2repo\test\testclient\1.0\testclient-1.0.jar
[INFO] Installing C:\Users\pbachu\AppData\Local\Temp\mvninstall4496344945989873179.pom to D:\m2repo\test\testclient\1.0\testclient-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.412s
[INFO] Finished at: Thu Jul 09 08:56:05 IST 2015
[INFO] Final Memory: 3M/116M
[INFO] ------------------------------

Now, the “testclient-1.0” jar is copied to your Maven local repository.

2. pom.xml

After installed, just declares the test-1.0 coordinate in pom.xml.

  <dependency>
      <groupId>test</groupId>
      <artifactId>testclient</artifactId>
      <version>1.0</version>
 </dependency>

Done.....Build it, now the “testclient-1.0” jar is able to retrieve from your Maven local repository.

No comments:

Post a Comment