ref: http://affy.blogspot.com.br/2007/09/extracting-dependancy-lists-from-maven.html
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class PomAnalysisDriver {
private static XPathFactory xpathFactory = XPathFactory.newInstance();
XPathExpression artifactIdExpr = null;
XPathExpression dependencyExpr = null;
XPathExpression depGroupIdExpr = null;
XPathExpression depArtifactIdExpr = null;
XPathExpression depScopeExpr = null;
XPathExpression depVersionExpr = null;
public static List<String> pomFiles = new ArrayList<String>(1);
public static void main(String[] args) throws Exception {
try {
String pathPomFile_1;
String pathPomFile_2;
if (args.length == 2) {
pathPomFile_1 = args[0];
pathPomFile_2 = args[1];
} else {
System.out.println("Usage: java ComparePom FilePathPom FilePathAnotherPom");
return;
}
PomAnalysisDriver.pomFiles.add(pathPomFile_1);
PomAnalysisDriver.pomFiles.add(pathPomFile_2);
PomAnalysisDriver pad = new PomAnalysisDriver();
pad.init();
// pad.searchForPomFiles("[ROOT_DIR]", pomFiles);
pad.execute();
} finally {
System.out.println("Done.");
}
}
public void init() throws XPathExpressionException {
XPath xpath = xpathFactory.newXPath();
artifactIdExpr = xpath.compile("/project/artifactId/text()");
dependencyExpr = xpath.compile("/project/dependencies/dependency");
depGroupIdExpr = xpath.compile("./groupId/text()");
depArtifactIdExpr = xpath.compile("./artifactId/text()");
depScopeExpr = xpath.compile("./scope/text()");
depVersionExpr = xpath.compile("./version/text()");
}
public void execute() throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
Set<String>[] sets = new HashSet[2];
/* for (String pomFilename : PomAnalysisDriver.pomFiles) { */
for (int j = 1; j <= PomAnalysisDriver.pomFiles.size(); j++) {
String pomFilename = PomAnalysisDriver.pomFiles.get(j - 1);
sets[j - 1] = new HashSet<String>();
File f = new File(pomFilename);
Document document;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(f);
// Node projectNode = document.getDocumentElement();
// NodeList children = projectNode.getChildNodes();
String projectName = (String) artifactIdExpr.evaluate(document, XPathConstants.STRING);
NodeList nodes = (NodeList) dependencyExpr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Node dependency = nodes.item(i);
String depGroupId = (String) depGroupIdExpr.evaluate(dependency, XPathConstants.STRING);
String depArtifactId = (String) depArtifactIdExpr.evaluate(dependency, XPathConstants.STRING);
String depScope = (String) depScopeExpr.evaluate(dependency, XPathConstants.STRING);
String depVersion = (String) depVersionExpr.evaluate(dependency, XPathConstants.STRING);
String depId = "GroupId: " + depGroupId + "; ArtifactId: " + depArtifactId + "; Scope: " + depScope
+ "; Version: " + depVersion;
sets[j - 1].add((String) depId);
}
}
sets[0].removeAll(sets[1]);
/*
* for (Set<String> set : sets) { System.out.println(set); }
*/
for (String k : sets[0])
System.out.println(k);
}
}