DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Publish Artifacts to Maven Central
  • Private Remote Maven Repository With Artipie
  • How To Convert Image Files Into GIF or WebP Format Using Java
  • How To Validate Names Using Java

Trending

  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Start Coding With Google Cloud Workstations
  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Adding and Working With JAR Files in Your Local Maven Setup

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.

By 
Alan Richardson user avatar
Alan Richardson
·
Oct. 18, 17 · Tutorial
Likes (17)
Comment
Save
Tweet
Share
164.7K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

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

I have split my game into two projects:

  • Game engine (which is now open source on GitHub)
    • e.g. https://github.com/eviltester/restmud-game-engine
  • RestMud game, Web Server, and REST API

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:

  • https://maven.apache.org/repository-management.html

    • Archiva: https://archiva.apache.org/index.cgi
    • Nexus: https://www.sonatype.com/download-oss-sonatype

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

  • https://www.compendiumdev.co.uk/page.php?title=casestudyjavadesktop
  • http://blog.eviltester.com/2016/02/lessons-learned-testing-house-of-test.html

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

  • https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
  • https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project
  • http://seleniumsimplified.com/2016/06/use_selenium_webdriver_jar_locally/
  • http://roufid.com/3-ways-to-add-local-jar-to-maven-project

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

Using a Repository Manager

  • https://maven.apache.org/repository-management.html

Releasing Your JAR to Maven Central

  • http://blog.javafortesters.com/2016/10/how-to-create-and-release-jar-to-maven.html
Apache Maven JAR (file format) Continuous Integration/Deployment Repository (version control) Dependency Open source

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

Opinions expressed by DZone contributors are their own.

Related

  • How to Publish Artifacts to Maven Central
  • Private Remote Maven Repository With Artipie
  • How To Convert Image Files Into GIF or WebP Format Using Java
  • How To Validate Names Using Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!