How can we add the local JAR files to the Maven Project in Java

0 votes
How can we add local JAR files to our Maven Project source?
Jun 1, 2018 in Java by Sushmita
• 6,920 points
11,697 views

2 answers to this question.

0 votes

You can add local dependencies directly into pom.xml file.

<dependency>
    <groupId>com.sample</groupId>
    <artifactId>sample</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>

Hope it helps!

To know more about Java, It's recommended to join our Java Training in Chennai today.

answered Jun 1, 2018 by sharth
• 3,370 points

edited Oct 7, 2021 by Sarfaraz
0 votes

Firstly I would like to give credit for this answer to anonymous stackoverflow user - I am pretty sure I've seen similar answer here before - but now I cannot find it.

The best option for having local jar files as a dependency is to create local maven repository. Such repo is nothing else than proper directory structure with pom files in it.

On my example: I have master project on ${master_project} location and subroject1 is on ${master_project}/${subproject1}

then I am creating mvn repository in: ${master_project}/local-maven-repo

In pom file in subproject1 located ${master_project}/${subproject1}/pom.xml repository needs to be specified which would take file path as an url parameter:

<repositories>
    <repository>
        <id>local-maven-repo</id>
        <url>file:///${project.parent.basedir}/local-maven-repo</url>
    </repository>
</repositories>

Dependency can be specified as for any other repository. This makes your pom repository independent. For instance once desired jar is available in maven central you just need to delete it from your local repo and it will be pulled from default repo.

<dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.servicebinder</artifactId>
        <version>0.9.0-SNAPSHOT</version>
    </dependency>

The last but not least thing to do is to add jar file to local repository using -DlocalRepositoryPath switch like here:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file  \
    -Dfile=/some/path/on/my/local/filesystem/felix/servicebinder/target/org.apache.felix.servicebinder-0.9.0-SNAPSHOT.jar \
    -DgroupId=org.apache.felix -DartifactId=org.apache.felix.servicebinder \
    -Dversion=0.9.0-SNAPSHOT -Dpackaging=jar \
    -DlocalRepositoryPath=${master_project}/local-maven-repo

Onece jar file is installed such mvn repo can be committed to code repository and whole set up is system independent. (working example in github)

I agree that having JARs committed to source code repo is not a good practice but in real life quick and dirty solution sometimes is better than full blown nexus repo to host one jar that you cannot publish.

answered Nov 5, 2018 by Sushmita
• 6,920 points

Related Questions In Java

0 votes
2 answers

How can we add leading zeros to the number in Java?

From Java 1.5 you can use the String.format method. ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 59,230 points
5,136 views
0 votes
1 answer

How can we upload the files using JSP/ Servlets in Java?

<form action="upload" method="post" enctype="multipart/form-data"> ...READ MORE

answered Jun 8, 2018 in Java by Sushmita
• 6,920 points
859 views
0 votes
1 answer

How can we add local .jar file dependency to build.gradle file?

You can refer the below code if ...READ MORE

answered Jun 27, 2018 in Java by Akrati
• 960 points
5,339 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
13,089 views
0 votes
1 answer

How to add local jar files to a Maven project?

Hello @kartik, You can add local dependencies directly ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,880 points
2,269 views
0 votes
1 answer

How can I create an executable JAR with dependencies using Maven?

<build> <plugins> <plugin> ...READ MORE

answered Apr 26, 2018 in Java by sophia
• 1,400 points
1,237 views
0 votes
1 answer

Retrieving the path of a running jar file

Its quite simple. Try using the below ...READ MORE

answered May 25, 2018 in Java by geek.erkami
• 2,680 points
10,684 views
0 votes
1 answer

How to run the JAR files in windows?

Following are the steps to run the ...READ MORE

answered May 28, 2018 in Java by Parth
• 4,640 points
1,122 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,274 views
0 votes
6 answers

How can we define global variables in java?

To define Global Variable you can make ...READ MORE

answered Dec 15, 2020 in Java by Gitika
• 65,890 points
117,658 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP