Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adding and Working With JAR Files in Your Local Maven Setup

DZone's Guide to

Adding and Working With JAR Files in Your Local Maven Setup

In this post, we take a look at a few different ways you can work with an external local dependency within your local Maven setup.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Sometimes you want to work with a JAR file that isn’t hosted in Maven Central.

It might be a third-party JAR, it might be one that you have written, but regardless, you have a lot of options for this. The approaches that I have used include:

  • Adding .jar files as an IntelliJ project dependency.
  • Installing it locally to your .m2 repository.
  • Adding it to your project as a system-scoped file.
  • Using a repository management tool like Nexus or Archiva.
  • Publishing the dependency to Maven Central.

Quick Hack: Adding a JAR as an IntelliJ Project Dependency

For very quick hacks, add the .jar as an IntelliJ project dependency and bypass Maven.

I demonstrate this in my free publication Java Desktop Application Technical Testing, and you can read a blog post here containing this information.

This is a very tactical approach:

  • It doesn’t scale
  • It doesn’t help you work with other people
  • It isn’t very good for CI or version control

But it might help you get your immediate work done:

  • Experiment
  • Try something out
  • Get a task completed

And then adopt one of the following approaches if it works.

Adding It to the Project as a System-Scoped File

As a short-term tactic, I have also added the .jar as a system-scoped file.

I did this in the past when working with a ‘bug fix’ version of Selenium WebDriver that had not yet propagated through to Maven Central — but was available for download.

<dependency>
    <groupId>selenium_2_53_1</groupId>
    <artifactId>com.seleniumhq.selenium_2_53_1 </artifactId>
    <version>2.53.1</version>
    <scope>system</scope>
    <systemPath>
        C:/Users/Alan/Downloads/selenium-2.53.1/selenium-server-standalone-2.53.1.jar
    </systemPath>
</dependency>


This can be a useful tactic, but I don’t think it really scales strategically, e.g. http://seleniumsimplified.com/2016/06/use_selenium_webdriver_jar_locally/

Installing the JAR Locally to Your .m2 Repository

To have the JAR available as a dependency that I can bring in using a normal maven include, I can install the JAR locally to my .m2 directory and repository.

This is explained on the Maven website:

And I use this approach at the moment when working with my RestMud game engine.

I have split my game into two projects:

I have not released the game engine to Maven Central, but the code is available on GitHub, as is a release .jar file.

I can build a snapshot .jar locally for my current work.

Installing it into my .m2 folder:

mvn install:install-file \
-Dfile=target/restmud-engine-1.4-SNAPSHOT-jar-with-dependencies.jar \
-DpomFile=pom.xml


If I didn’t have the pom.xml file, I could still do this. I just add the details from the pom.xml into my command line:

mvn install:install-file \
-Dfile=target/restmud-engine-1.4-SNAPSHOT-jar-with-dependencies.jar \
-DgroupId=uk.co.compendiumdev \
-DartifactId=restmud-engine \
-Dversion=1.4-SNAPSHOT \
-Dpackaging=jar


And if I want the source code JAR to be associated with the .jar (which I usually do), then I add the following argument to the command line:

-Dsources=target/restmud-engine-1.4-SNAPSHOT-sources.jar


This allows me to keep the pom.xml of my projects that use the .jar to remain as though the .jar was on a repository manager or in Maven Central.

I think this is a good tactical approach that supports a longer-term strategic development of your development and automated execution approach.

You could install a dependency management tool like Nexus or Archiva.

Then your .jar files are installed into this repository, which is accessible by your team, and CI process, and not just your local development machine.

Maven docs on repositories:

You have to:

  • Install the dependency management tool
  • Configure the pom.xml to have a <repositories> section and point to your dependency management tool

You’ll probably want to evaluate which of the repository management tools works best for you and your environment.

This is a much more strategic approach and is good for teamwork and continuous integration processes.

Publishing to Maven Central

This is probably the most strategic long-term approach, but it requires you to make your work public.

All of the other approaches mentioned allow you to keep your work to yourself.

It is quite a long process, so I won’t describe it here, but I have a full write-up on my blog.

Summary

  • The ultimate short-term hack — add it as a dependency in your IDE
  • For quick hacks — add it as system-scoped maven dependency
  • For personal work, or moving towards a strategic approach, install locally to .m2
  • For longer-term tactical work as a team, use a repository manager
  • For strategic open source work, release to maven central

Useful Links

Hack Tactic: Adding a .jar to IntelliJ

Installing a .jar Into Your Local .m2 Folder or as a System-Scoped File

Example code that has the mvn install instructions in it is my restmud-game-engine

Using a Repository Manager

Releasing Your JAR to Maven Central

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
intellij ,dependency ,maven ,jar ,java ,tutorial

Published at DZone with permission of Alan Richardson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}