Especially in enterprise application development, there can be 3rd party dependencies that are not available in public Maven repositories. These may be internal, business specific libraries or licensed libraries that have limitations on usage.
When this is the case, you can either publish to a private Maven repository that controls authorization or you can put them into your local cached maven repository.
Copying into local Maven cache
Ultimately, all the dependency jars for your project are retrieved and cached on your local disk in ‘~/.m2/repository’ directory. You can insert a 3rd party jar directly into this local cache.
Let’s assume that the company ‘mycompany’ has another department that emails you a jar named graphing.jar:
> mvn install:install-file -Dfile=graphing.jar -DgroupId=mycompany -DartifactId=graphing -Dversion=1.0 -Dpackaging=jar
The downside to this method is that it only satisfies the dependency on this single host. If you have a multi-person development team, each one of them will need to run this same command to satisfy the dependency.
Publishing to private remote Maven Repository
Once again let’s assume another department sends you a jar named graphing.jar, but instead of putting it in your local maven cache, you instead want to place it into your group’s private Maven repository so that everyone in your group has access to this dependency.
First, make sure that the ‘~/.m2/settings.xml’ file has a <server> element that has an id and credentials for the repository. For a private Artifactory server as described in the article I wrote here, it would look like this:
<settings> <servers> <server> <id>central</id> <username>admin</username> <password>password</password> </server> </servers> </settings>
And then you could publish the jar to the repository with the following syntax:
mvn deploy:deploy-file -DgroupId=mycompany \ -DartifactId=graphing \ -Dversion=1.0 \ -Dpackaging=jar \ -Dfile=graphing.jar \ -DrepositoryId=central \ -Durl=http://<server>:8081/artifactory/test1
Where the ‘repositoryId’ is the <id> in settings.xml. And the ‘url’ property at the end is completely dependent upon your Maven server/port/repository name.
Usage in pom.xml
For the pom.xml definition of the project, you would need to define the dependency like:
<dependency> <groupId>mycompany</groupId> <artifactId>graphing</artifactId> <version>1.0</version> </dependency>
But also need a <repositories> element pointing at the remote Maven repository. This can be put after the <properties> element.
<repositories> <repository> <id>central</id> <url>http://<server>:8081/artifactory/test1</url> </repository> </repositories>
The values you use for ‘url’ and ‘id’ are dependent upon your particular deployment.
Now you can run package or deploy on the Maven project, and the graphing.jar will be automatically pulled from the private Maven repository.