Wednesday, February 16, 2011

Copy Maven Dependency Tree jars

Well, it is the second time I need to get the jar on which a maven project depends on and copy them into a non-maven application. Sure mvn dependency:tree is your friend to see which jars you need to copy. And there are also maven tasks that can do the copy. But what if I want to copy them over without polluting the pom? I couldn't find a quick way (I am sure someone will tell me :)), so I created a groovy script that would do that for me.

Here it is, change the path to the repo, change the path into which to copy to, and run the thing.




// copy also source jars?
def alsoSources = false;
// copy also javadoc jars?
def alsojavaDocs = false;
// your maven repo path
def repo = "/home/moovida/.m2/repository/"
// path to which to copy them
def copyPath = "/home/moovida/development/gt-swt-port-hg/rcp-gt-swt/libs/"


def mvnCommand = "mvn dependency:tree";
def proc = mvnCommand.execute();
proc.waitFor();

def output = proc.in.text;
// clean out what we need
def lista=[];
def lines = output.split("\n");
def depsList = [];
def startIndex = -1;
def endIndex = -1;
for (int i = 0; i < lines.size(); i++){
def line = lines[i];
if(line.startsWith("[INFO] [dependency:tree]")){
startIndex = i + 1;
continue;
}
if(startIndex != -1 && line.startsWith("[INFO] ----------")){
endIndex = i - 1
break;
}
if(startIndex == -1){
continue;
}

lista << line;
}

println "Search for:"
lista.each{
println it
}

println "---------------------------------------"
println "---------------------------------------"

// find jars
def basedir = new File(repo)
def files = [];
basedir.eachDirRecurse () { dir ->
dir.eachFileMatch(~/.*.jar/) { file ->
files << file
}
}

// extract name pattern and version
def fileBeginList = []
def versionList = []
lista.each{
def split = it.split(":");
fileBeginList << split[1]
versionList << split[3]
println "${split[1]} --- ${split[3]}"
}

def finalList = [];
// extract right jars paths from list
for (it in files){
def name = it.getName()
def path = it.getAbsolutePath()

if(!alsoSources && name.matches(".*sources.*")){
continue;
}
if(!alsojavaDocs && name.matches(".*javadoc.*")){
continue;
}

for (int i = 0; i < fileBeginList.size(); i++){
def fBegin = fileBeginList.get(i);
def version = versionList.get(i);
if(name.startsWith(fBegin)){
if(name.matches(".*"+version+".*")){
finalList << it;
break;
}
}
}
}

println "---------------------------------------"
println "---------------------------------------"
println "Found:"
finalList.each{
println it
}


println "---------------------------------------"
println "---------------------------------------"
if(copyPath){
println "Copy deps jars to: ${copyPath}"
finalList.each{
def name = it.getName();
def path = it.getAbsolutePath();
def newPath = new File(copyPath, name).getAbsolutePath();
new AntBuilder().copy ( file : path , tofile : newPath )
}
}





Worked well for me.

1 comment:

moovida said...

Have to remember that on windows the mvn command needs to be:

def mvnCommand = "cmd /c mvn dependency:tree"