Search This Blog

How to add a jar in your local maven repository ?

There are so many jars which we need for our development purposes and now that we have Maven as build and project management tool it become easy to access those jars. Even though you can get most of the jars from maven repository still there are few jars which are not available in maven repository. Also, there are situations when you need a jar internal to your organisation in your specific application. Now the question is how do we add those jars which are not there in maven repository.

Lets take an example of ojdbc14 jar. In almost every application we need to interact with database and Lets say its not available in maven repo. There are two ways you can get this jar added to your Maven dependencies until your organization add the jar in its local maven repo.

#1

Step 1 : In this case ,download jar from oracle website and keep it at any specific location .(C:\jars\db\ojdbc14.jar) You can get the jar from your required source as well.
Step 2 : execute below command
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=C:\jars\db\ojdbc14.jar -DgeneratePom=true
This command will add your jar to your local maven repository . (In your .m2 folder)
Step3 : Add the below dependency to your pom.xml
        <dependency>       

            <groupId>com.oracle</groupId>

            <artifactId>ojdbc14</artifactId>

            <version>10.2.0.3.0</version>

   </dependency>

Now update your maven project. It should be available in Maven Dependencies.

#2
Step 1: add that jar in your project under WEB-INF\lib (or any other folder)
Step2 :  add following dependency in your pom.xml
<dependency>

    <groupId>{group_Id}</groupId>

    <artifactId>{artifact_Id}</artifactId>

   <version>{version}</version>

   <scope>system</scope>

   <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/yourJar.jar</systemPath>

</dependency>

The important thing to note here is <scope> tag whose value is set as system, which means that for that particular jar maven would not look into the repository instead it would check it at systempath defined for that jar. 
Now update your maven project. It should be available in Maven Dependencies.

No comments:

Post a Comment