Over a million developers have joined DZone.

Creating Maven Artifacts From a jar File

Here is a step-by-step guide for generating Maven artifacts from any third-party jar.

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

Today, we're going to create an artifact from mailapi.jar with version 1.4.5 and use it in our Maven project. This article will guide you through the steps to make that happen.

Background

While creating Maven projects, we often come across a scenario when we have to import a jar from local repositories instead of Maven.

The reasons could be: 

  • The jar was created locally, and it’s not present in the Maven repository.
  • We want to use a specific version of the jar, maybe because of:
    • An upgrade that can be inconsistent with other dependencies.
    • An upgrade that could break functionality if it’s written with assumptions made for the current version.

We need to find an answer to the following questions:

  1. How can we create a Maven artifact from a jar?

  2. How can we configure this artifact in pom.xml?

Steps

  1. Create a Maven artifact from a jar

 Maven's Install Plugin can help us generate an artifact. Run:

mvn install:install-file -Dfile=<path to jar>\mailapi.jar 
-DgroupId=com.study  
-DartifactId=mailapi 
-Dversion=1.4.5  
-Dpackaging=jar  
-DcreateChecksum=true

            

Maven Install command

At the C:\Users\<userName>\.m2\repository\com\study\mailapi\ directory, you can see generated artifacts:

Artifacts

A parameter createChecksum=true creates integrity checksums (MD5, SHA-1) for the artifacts during installation.

2. Create a repo folder in <project base directory>.

3. First, copy the com folder from C:\Users\<userName>\.m2\repository\ to <project base directory>/repo.

4. Configure pom.xml to look up the local repository for specific jars.

pom.xml

Remember to use System Scope for jars referred from local repositories.

I hope this article will be helpful.

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:
maven ,artifact ,java ,repository

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}