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
- Maven command approach
- 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:
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
- First create folders with name as you see in the <groupId> tag
MAVEN_HOME>>com>>microsoft>>sqlserver
- Second create folders with name as you see in the <artifactId> tag
Continuing_from_above_path>>sqljdbc4
- 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 :
- Eclipse IDE – How to show line numbers ?
- Eclipse IDE – How to GO TO any line number directly ?
- Eclipse IDE – How to remove unused imports ?
- Eclipse IDE – How to clean project ?
- Eclipse IDE – How to build Java project automatically ?
- Eclipse IDE – How to comment and un-comment line & block ?
- Eclipse IDE – How to generate constructor using fields ?
- Eclipse IDE – How to generate getters and setters ?
- Eclipse IDE – How to search files ?
- Eclipse IDE – How to locate methods in Java file ?
- Eclipse IDE – How to open editor using CTRL + E ?
- Eclipse IDE – Java compiler compliance level issue
Related Articles:
- Apache Maven – Introduction
- Apache Maven – Install on Windows 7 OS
- Apache Maven – Settings.xml explanation
- Apache Maven – Proxy setting explanation
- Apache Maven – pom.xml explanation
- Apache Maven – Plugins explanation
- Apache Maven – Changing default Maven repository location in Windows 7 OS
- Apache Maven – Local, Central and Remote Repositories
- Apache Maven – Installing custom library into local repository
- Apache Maven – Transitive dependencies explanation
- Apache Maven – Exclusion of Transitive dependencies
- Apache Maven – Dependency Scopes
- Apache Maven – Skipping unit test using surefire plugin
- Apache Maven – Exclusions and Inclusions of unit test
- Apache Maven – offline execution
- Apache Maven – Co-ordinates explained
- Eclipse + Maven – Integration
- Eclipse + Maven – How to import Maven project with pom.xml ?
- Eclipse + Maven – Setting M2_REPO classpath variable in IDE
- Eclipse + Maven – M2_REPO is Non Modifiable
- Eclipse + Maven – Creating and exploring projects using archetypes
- Eclipse + Maven – Converting Web project to Maven project
- Eclipse + Maven – mvn eclipse:eclipse command
- Eclipse + Maven – Plugin execution not covered by lifecycle configuration
References:
- http://maven.apache.org/plugins/maven-install-plugin/examples/specific-local-repo.html
- http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
Happy Coding !!
Happy Learning !!