HomeMaven

Steps to add external jar to local maven repository

Steps to add external jar to local maven repository
Like Tweet Pin it Share Share Email

Many a times we utilize 3rd party or external jar files which are not available in maven central / online repositories. In these situations we can look at couple of options. One is to add external jar to local maven repository and other is to include as part of the maven project.

Before getting in to the further steps and approaches, kinldy make sure that you have proper setup for maven environment, else you can follow the detailed steps here

Now let us look at the following 2 approaches.

Approach 1: Add external jar to local maven repository

Maven has provided a set of commands with which we can add external or custom jars to local maven repository. Later, we can refer them in your pom file.

Let us now look in to the steps

Step 1: Use the following command of Maven and add external jar to local repository

Syntax:

mvn install:install-file -Dfile= -DgroupId= -DartifactId= -Dversion= 

You might find more options in maven’s official website, please feel free to explore more here.

Example:

mvn install:install-file -Dfile="d:/path/target.jar" -DgroupId=com.3rdparty -DartifactId=recorder -Dversion=1.0 -Dpackaging=jar

Step 2: Include dependency in your pom file in the following format

Note: We need to make sure that our machine is equipped with proper environment conditions. Which is, in your machine need to make sure maven is installed and the environment variables are properly set. Which otherwise you will not be able to run the command successfully.


   com.3rdparty
   recorder         
   1.0
   

Note: Some times you may have to perform “Update Project” operation, which is present under the Maven context menu of the project.

Right click on your project > Highlight “Maven” Context Menu > click Update Project under the sub context menu. Once you click on this, the eclipse might build the project and wait for it to complete.

Approach 2: Include jar as part of the maven project.

In this approach you need to first create a folder in your maven project & add your external jar file. Once the jar file is added, include the jar file in your pom using following notation.

Let us now look in to the steps

Step 1: Create folder and include the external jar file in your maven project

Step 2: Use following syntax to specify dependency in pom file


    com.3rdparty
    recorder
    system
    1.0
    ${basedir}/src/main/resources/recorder.jar   

This approach allows you to include jar file as part of your maven project and be able to work in eclipse properly. That is you can call the respective classes and methods which are part of the included jar file.

Above is the example based on the syntax provided by maven. And in the path we need to specify the path where jar file is located.

${basedir} refers to your project’s base directory and the remaining part is the path to your jar from the base directory. You can even provide a hard coded or full absolute path instead of the above notation. It is all up to your needs.

Now let look at couple of reasons why we suggest to go for first approach and not for second approach.

  1. When we work with multiple maven projects and include them as dependencies to other projects. The main project which will include the sub project, will not be able to access the classes and methods which are present as part of the sub project’s jar file.
  2. Also, when we actually try to create a single jar / flat jar file that is created out of multiple dependent maven projects. Maven does not support to pick the jar file which specified in the format as mentioned in approach 2.

So, for simple projects you can use the approach 2, but if you want to generate a single flat jar file out of multiple maven projects, you will not be able to succeed with approach 2.

Hope this article has provided you good insights as how to add external jar file to local maven repository and why do we need to follow the first approach.

Comments (2)

  • Hi,

    I am trying to include msmqjava jar in 2nd approach. I am getting class not found exception when building the maven and running the generated jar in different system. Can you please let me know what I am doing wrong in the process.

    Thanks,
    Shashi

    Reply
    • Author

      Hi Sheshi,

      Thank you for reaching out to us, that is exactly why we do not recommend the second approach. Please try to implement the first approach and see you might be able to solve this issue

      Thank you for reaching out to us

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *