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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. How To Easily Deploy Pre-Packaged Maven Artifacts

How To Easily Deploy Pre-Packaged Maven Artifacts

Craig Dickson user avatar by
Craig Dickson
·
Oct. 05, 11 · Interview
Like (0)
Save
Tweet
Share
7.80K Views

Join the DZone community and get the full member experience.

Join For Free

The Maven deploy:deploy-file goal is very useful for deploying JARs (and other artifacts) that have not been mavenized, to your own repository. It allows you to pass Maven coordinates and other Maven related meta data on the command line so that the artifact ends up in the right spot in your repository and has at least the bare minimum of Maven meta data associated with it to make it useful. Unfortunately, one less common scenario it does not currently handle is deploying an already mavenized artifact to a repository. I recently ran into this exact issue while doing some work for a client, so I put together a script to bridge the gap.

What’s Missing in the deploy:deploy-file Maven Target

The common scenario that the deploy:deploy-file target is intended to address is where you have an artifact (usually a JAR file) that you want to use as part of your project’s Maven build, however the artifact is not available in any repository that you have access too. The two most common reasons for this are:

  • the artifact was not developed with Maven in the first place
  • the artifact was developed with Maven, but has not been published to a repository you have access too

With the deploy:deploy-file target you can deploy the artifact to a repository that you have access too, upload the necessary Maven meta data at the same time and now the artifact is available to be declared as a Maven dependency in your project’s pom.xml file.

Unfortunately with the way the target works and the arguments it accepts, the 2nd scenario above is unnecessarily complex. Even though all the critical Maven meta data is available inside of the artifact, you still have to specify it all correctly on the command line.

Solution For Automating The Deployment of 3rd Party Maven Artifacts

For the particular situation with my client we not only had almost 160 JARs that were in the 2nd scenario above, we required a repeatable automated solution as we needed to be able to deploy the artifacts to many repositories (one for each new team/project environment we had to set up). So I ended up creating a script to extract the embedded pom.xml file from the JAR and then use that pom.xml on the Maven deploy:deploy-file command line.

#!/bin/sh
 
#
# deploys an already packaged Maven artifact to a remote repository by using the embedded pom.xml file
#
 
HELP_MSG="Usage: mvn-deploy-artifact <artifact file> <repository id> <repository base url>"
 
if [ $# -ne 3 ]; then
    echo $HELP_MSG
    exit -1
fi
 
ARTIFACT=$1
REPOSITORY_ID=$2
REPOSITORY_BASE_URL=$3
 
POM=$(jar tf $ARTIFACT | grep META-INF/maven/.*/pom.xml)
 
if [ -n "$POM" ]; then
    jar xf $ARTIFACT $POM
    if [ $? -ne 0 ]; then
        echo "ERROR: non-zero exit when attempting to extract $POM from $ARTIFACT"
        exit -1
    fi
    mvn deploy:deploy-file -Dfile=$ARTIFACT -DpomFile=$POM -DrepositoryId=$REPOSITORY_ID -Durl=$REPOSITORY_BASE_URL
    if [ $? -ne 0 ]; then
        echo "ERROR: non-zero exit when attempting to deploy $ARTIFACT to $REPOSITORY_BASE_URL"
        exit -1
    fi
    rm $POM
else
    echo "cannot find pom file, try deploying $ARTIFACT manually"
    exit -1
fi

Hope this script helps someone save some time. Let me know if you have any suggested improvements.

From http://craigsdickson.com/post/how-to-easily-deploy-pre-packaged-maven-artifacts/

Apache Maven Artifact (UML)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Distributed Tracing: A Full Guide
  • Create a CLI Chatbot With the ChatGPT API and Node.js
  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • What Are the Different Types of API Testing?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: