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:
1 |
mvn install:install-file -Dfile=<path to jar file> -DgroupId=<maven group id> -DartifactId=<maven artifact id> -Dversion=<version you want to provide" -Dpackaging=<type of packaging> |
You might find more options in maven’s official website, please feel free to explore more here.
Example:
1 |
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.
1 2 3 4 5 |
<dependency> <groupId>com.3rdparty</groupId> <artifactId>recorder</artifactId> <version>1.0</version> </dependency> |
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
1 2 3 4 5 6 7 |
<dependency> <groupId>com.3rdparty</groupId> <artifactId>recorder</artifactId> <scope>system</scope> <version>1.0</version> <systemPath>${basedir}/src/main/resources/recorder.jar</systemPath> </dependency> |
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.
- 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.
- 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.
Founder of TestingTools.co, constantly shares knowledge on different test automation tools. Has experience in building proof of concepts, solutions, frameworks, platforms & implementation of test automation projects.
In pursuit of building a platform with inbuilt framework and reusable components for Oracle Cloud applications ( Oracle HCM, CX, SCM, Financials clouds, Salesforce and other cloud applications )
Major accomplishments in his career:
- Product architect and development manager for test automation platforms
- Oracle Flow Builder @ Oracle
- CloudTestr @ Suneratech
- 2 times to oracle open world
- More than 20 successful POCs ( Proof of concepts )
- 100 demos
- Designed pricing models.
- Trained more than 50 members.
- Built more than 100 re-usable functions.
Worked with tools:
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
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