EXECUTIVE SUMMARY Craig S. Dickson is a software development professional with over 15 years of experience. He has proven leadership experience in both domestic and multi-national start-up and Fortune 500 corporations in the United States, Australia and Europe. Craig specializes in enterprise Java development and cloud architecture and holds multiple certifications including Sun Certified Architect for JavaEE and Certified Scrummaster. Craig brings specific expertise in enterprise software architecture and design, refining development processes and building development teams around Agile software engineering principles. Educated in Australia, Craig holds a BSc(Hons) in Computer Science. He is based in Huntington Beach, CA, and Brisbane, Australia. SPECIALTIES Enterprise Java - Software Development Best Practices - Software Development Team Leadership - Cloud Computing Craig is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

How To Easily Deploy Pre-Packaged Maven Artifacts

10.04.2011
| 4431 views |
  • submit to reddit

The 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/

Published at DZone with permission of Craig Dickson, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Shiva Anand Red... replied on Sun, 2011/10/09 - 12:24pm

Thanks craig.

 I forgot about deploy command, thx for reminding. I want to do the same thing in windows machine.

If u have a script ready can u please share on shiva.anand.teegala@gmail.com?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.