1

Note: maven-dependency-plugin isn't suitable here for reasons specified below

I'm deploying projects to Artifactory with sources attached. I'd like to be able to run a command to download and unpack sources for a given artifact and its dependencies. I'll be using this to diff two versions of an artifact.

What I'd like to do is basically this:

mvn extract:sources -DgroupId=[groupId] -DartifactId=[artifactId] -Dversion=[version]

Have tried combining a couple of goals from the maven-dependency-plugin but this doesn't seem capable of doing what I need:

  • :unpack-dependencies requires a project
  • :get requires me to explicitly specify a remote repo. Why can't it use those in my settings.xml?

I've tried writing my own mojo to do this but am flummoxed because I can't seem to get a handle on remote repositories unless I'm in a project directory. Thus I can't download the project. And even once I have downloaded the project, the mojo will have already initialised its ${project} hence I won't be able to get its dependencies etc

Would appreciate your help.

Chris Beach
  • 4,302
  • 2
  • 31
  • 50

2 Answers2

0

The best I found was to use 3 maven commands:

  • one that get your artefact
  • one that get your artefact pom
  • and one that fetch its dependencies using its pom

Here is a bash snippet:

#!/bin/bash

DIR=some/dir
ARTEFACT_ID=your-artefact
GROUP_ID=com.your.group.id
VERSION=1.7
ARTEFACT=${GROUP_ID}:${ARTEFACT_ID}:${VERSION}

rm -fR $DIR

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy \
  -Dartifact=$ARTEFACT \
  -DoutputDirectory=$DIR

# now get the pom: it will be needed by the copy-dependencies goal
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy \
  -Dartifact=$ARTEFACT:pom \
  -DoutputDirectory=$DIR

mvn -f ${DIR}/${ARTEFACT_ID}-${VERSION}.pom org.apache.maven.plugins:maven-dependency- plugin:2.8:copy-dependencies \
  -DoutputDirectory=.
Bruno Bieth
  • 2,317
  • 20
  • 31
0

The apache ivy jar can be used as a CLI program to download Maven artifacts.

The following example downloads ivy from Maven Central, then uses it to download the commons-lang source jar:

wget -O ivy.jar \
     http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.2.0/ivy-2.2.0.jar

java -jar ivy.jar \
     -dependency commons-lang commons-lang 2.6 \
     -confs sources \
     -retrieve "[artifact](-[classifier]).[ext]"
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • Thanks, that's handy, although since asking the question I've started writing a Maven plugin to do what I need. Will share on github and post the link back here. – Chris Beach Feb 24 '12 at 10:39