Apache Maven – Installing custom library into local repository

In this article, we will learn and understand how to insert/upload custom or third party libraries into local maven repository

Technology Used

  • Apache Maven 3.2.3
  • Windows 7 OS
  • Microsoft SQL server library

Q) Why we need this ? Isn’t central repository capable of handling these libraries ?

Sometimes, there are some project specific libraries aren’t available in central repository. These cases can be briefly described into following scenarios

  • Some third-party libraries aren’t available in central maven repository
  • You built a custom library and you or your team want to re-use for some other projects through local maven repository

Q) How can we insert/upload custom libraries into local maven repository ?

There are two approaches available

  1. Maven command approach
  2. Manual folder creation approach

Note: It’s recommended to go for first approach –> Maven command approach

1. Maven command approach:

Assume we are using Microsoft SQL server for database interaction in our project, so we require “sqljdbc” jars for our project to build and run successfully

Maven co-ordinates for Microsoft SQL server

groupId          --> com.microsoft.sqlserver
artifactId       --> sqljdbc4
version          --> 4.0

Let us assumes, we have stored the downloaded Microsoft SQL server jar at location D:\Downloads\Jars\sqljdbc-{version}.jar (download from here)

So maven command to insert/upload this jar into local maven repository

mvn install:install-file \
-Dfile=D:\Downloads\Jars\sqljdbc4-4.0.jar \
-DgroupId=com.microsoft.sqlserver \
-DartifactId=sqljdbc4 \
-Dversion=4.0 \
-Dpackaging=jar \
-DgeneratePom=true

Note: “\” indicates maven command is in continuation, so while executing command should be in one single line as seen in the below command prompt

Output in Console:

1_Apache-Maven-Installing-Custom-library_build_successful

Now you can use this library into any of the Maven based project with below co-ordinates in pom.xml

pom.xml

<dependencies>
    <dependency>
         <groupId>com.microsoft.sqlserver</groupId>
         <artifactId>sqljdbc4</artifactId>
         <version>4.0</version>
    </dependency>
</dependencies>

2. Manual approach:

This approach is quite unconventional, but easy to use

We will try to insert/upload the same library used in the above maven command approach, so our Maven co-ordinates for Microsoft SQL server is

groupId          --> com.microsoft.sqlserver
artifactId       --> sqljdbc4
version          --> 4.0

Our local maven repository location (i.e.; MAVEN_HOME) is at “D:\M2_HOME\.m2\repository”, so create folders as directed below in tag-wise

  1. First create folders with name as you see in the <groupId> tag
MAVEN_HOME>>com>>microsoft>>sqlserver
  1. Second create folders with name as you see in the <artifactId> tag
Continuing_from_above_path>>sqljdbc4
  1. Similarly, create folders with name for the <version> tag
Continuing_from_above_path>>4.0

After creating above folder structure, place/copy the relevant jars i.e.; for this example jar name should/must be sqljdbc4-4.0.jar (i.e.; <artifactId>{hyphen}<version>)

Complete JAR location is “D:\M2_HOME\.m2\repository\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar

Note: while creating folders, if folder already exists then leave it and move on it create next folder

Useful Eclipse IDE shortcuts :

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Apache Maven - Transitive dependencies explanation
Apache Maven - Local, Central and Remote Repositories