Add a dependency in Maven
You'll have to do this in two steps:
1. Give your JAR a groupId, artifactId and version and add it to your repository.
If you don't have an internal repository, and you're just trying to add your JAR to your local repository, you can install it as follows, using any arbitrary groupId/artifactIds:
mvn install:install-file -DgroupId=com.stackoverflow... -DartifactId=yourartifactid... -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jarfile
You can also deploy it to your internal repository if you have one, and want to make this available to other developers in your organization. I just use my repository's web based interface to add artifacts, but you should be able to accomplish the same thing using mvn deploy:deploy-file ...
.
2. Update dependent projects to reference this JAR.
Then update the dependency in the pom.xml of the projects that use the JAR by adding the following to the element:
<dependencies>
...
<dependency>
<groupId>com.stackoverflow...</groupId>
<artifactId>artifactId...</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
How do I add a Maven dependency in Eclipse?
- On the top menu bar, open Window -> Show View -> Other
- In the Show View window, open Maven -> Maven Repositories
- In the window that appears, right-click on Global Repositories and select Go Into
- Right-click on "central (http://repo.maven.apache.org/maven2)" and select "Rebuild Index"
- Note that it will take very long to complete the download!!!
- Once indexing is complete, Right-click on the project -> Maven -> Add Dependency and start typing the name of the project you want to import (such as "hibernate").
- The search results will auto-fill in the "Search Results" box below.
What happens when I add a Maven dependency?
In general in all pom.xml
we found dependency like this -
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
Here groupId
, artifactId
and version
are 3 keys by which a jar is uniquely identified. These 3 combination works like a coordinate system for uniquely identifying a point in a space using x, y and z coordinates.
Whenever you issue a mvn package
command maven
tries to add the the jar file indicating by the dependency to you build path. For doing this maven follows these steps -
Maven search in your local repository (default is ~/.m2 for
linux
). If the dependency/jar is found here then it add the jar file to you build path. After that it uses required class file from thejar
for compilation.If the dependency is not found in ~/.m2 then it looks for your local private repository (If you already have configured any using
setting.xml
file) and maven central remote repository respectively. If you don't have any local private repository then it directly goes to the maven central remote repository.Whenever the jar is found in the local/remote repository it is downloaded and saved in ~/.m2.
Going forward, when you again issue a mvn package
command then it's never search for the dependency to any repository since it already in your ~/.m2
.
Adding a system dependency to Maven
this is how I add system dependency to my maven pom.xml. with in the project root path, I have created lib directory and there I have placed my jar file.
<dependency>
<groupId>com.sshx</groupId>
<artifactId>sshx</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sshxcute-1.0.jar</systemPath>
</dependency>
if still you face the same issue try adding the dependency manually by issuing the following command from the jar file location
mvn install:install-file -Dfile=sshxcute-1.0.jar -DgroupId=com.sshx -DartifactId=sshx -Dversion=1.0 -Dpackaging=jar
this command will add the jar to your .m2 repository as a dependency and you need to change the pom.xml dependency as follows:
<dependency>
<groupId>com.sshx</groupId>
<artifactId>sshx</artifactId>
<version>1.0</version>
</dependency>
once you are done, issue mvn clean install
command from command prompt and build your application.
However, another option is to create a local repository. See at this thread:
How to include local jar files in Maven project
How to add local jar files to a Maven project?
Install the JAR into your local Maven repository (typically .m2
in your home folder) as follows:
mvn install:install-file \
-Dfile=<path-to-file> \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<packaging> \
-DgeneratePom=true
Where each refers to:
<path-to-file>
: the path to the file to load e.g → c:\kaptcha-2.3.jar
<group-id>
: the group that the file should be registered under e.g → com.google.code
<artifact-id>
: the artifact name for the file e.g → kaptcha
<version>
: the version of the file e.g → 2.3
<packaging>
: the packaging of the file e.g. → jar
Reference
- Maven FAQ: I have a jar that I want to put into my local repository. How can I copy it in?
- Maven Install Plugin Usage: The
install:install-file
goal
Add a dependency from a maven project to a non-maven project
Maven supports three types of repository: local, central and remote. Normally, dependencies you add to your pom.xml
file are pulled from the central repository. What you can do is compile your library to a jar, and drop that in your local repository, which can be found in one of the following locations depending on your OS:
- Windows: C:\Users\<User_Name>\.m2
- Linux: /home/<User_Name>/.m2
- Mac: /Users/<user_name>/.m2
You can install your jar in your local repository as follows:
mvn install:install-file \
-Dfile=<path-to-file> \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<packaging> \
-DgeneratePom=true
After that the jar will be copied into your local repository in a folder structure that mirrors the groupId
. And since you've provided a custom groupId
, artifactId
and version
you can use those to add the dependency to your pom.xml
Alternatively, you can add a local repository to your project and install your jar there, then add the dependency to your pom.xml
as normal.
How can I create an executable/runnable JAR with dependencies using Maven?
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
and you run it with
mvn clean compile assembly:single
Compile goal should be added before assembly:single or otherwise the code on your own project is not included.
See more details in comments.
Commonly this goal is tied to a build phase to execute automatically. This ensures the JAR is built when executing mvn install
or performing a deployment/release.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Related Topics
Printing in Java to Label Printer
Is Gnu's Java Compiler (Gcj) Dead
Java Keylistener VS Keybinding
Cors Allowed-Origin Restrictions Aren't Causing the Server to Reject Requests
Enhanced 'For' Loop Causes an Arrayindexoutofboundsexception
Keylistener, Keypressed Versus Keytyped
Spark Strutured Streaming Automatically Converts Timestamp to Local Time
How to Handle Simultaneous Key Presses in Java
Array Initialization Differences Java
Abstractmethoderror Using Uribuilder on Jax-Rs
How to Properly Use Java Executor
Adding Points to Xyseries Dynamically with Jfreechart
Java Read Large Text File with 70Million Line of Text
Differencebetween Connection and Read Timeout for Sockets
Calling Virtual Method in Base Class Constructor
@Transactional Method Called from Another Method Doesn't Obtain a Transaction