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.