09/08/2007: Extracting Dependancy Lists from Maven POM Files
Recently a client of mine wanted to know if they were including the same dependency versions in their subprojects. They had 12 modules feeding into one maven-based build so I was inefficient to check the dependencies by hand. After running this program, copy the output into a program to sort it.
package com.affy.pom; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; import java.util.TreeSet; 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 depVersionExpr = null; public static void main(String[] args) throws Exception { try { ListpomFiles = new ArrayList (); PomAnalysisDriver pad = new PomAnalysisDriver(); pad.init(); pad.searchForPomFiles("[ROOT_DIR]", pomFiles); pad.execute(pomFiles); } 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()"); depVersionExpr = xpath.compile("./version/text()"); } public void searchForPomFiles(final String dir, final List pomFiles) { File initialDir = new File(dir); if (initialDir.isDirectory() == false) { return; } else { String[] files = initialDir.list(); for (String filename : files) { String suffix = ""; if (dir.endsWith("\\") == false && dir.endsWith("/") == false) { suffix = "\\"; } File childDir = new File(dir + suffix + filename); if (childDir.isDirectory() == true) { searchForPomFiles(dir + suffix + filename, pomFiles); } else { if (filename.equals("pom.xml")) { pomFiles.add(childDir.getAbsolutePath()); } } } } } public void execute(final List pomFiles) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException { for (String pomFilename : pomFiles) { 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); List dependencies = new ArrayList (); 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 depVersion = (String) depVersionExpr.evaluate(dependency, XPathConstants.STRING); String depId = "/" + depGroupId + "/" + depArtifactId + "/" + depVersion + "/" + projectName; dependencies.add(depId); } for (String s : dependencies) { System.out.println(s); } } } }
08/07/2007: Super Simple JPA (also Super Simple EJB3)
Several months ago, I wrote a Super Simple Hibernate entry. Several people have asked me to write the same kind of entry to help them get started with JPA. This entry is the result.
The Super Simple JPA project allows you to ensure that your development environment is configured properly to run JPA applications – outside of any J2EE container.
If you don't want to download all of these support applications, wait a few days and I'll have a version that uses Maven to automate the process.
Follow these steps to get your Super Simple JPA (EJB3) project working:
- Download Hibernate 3.2 to C:\data\support\hibernate-3.2.
- Download Hibernate Annotations 3.3.0 GA to C:\data\support\hibernate-annotations-3.3.0.GA.
- Download Hibernate EntityManager 3.3.1 GA to C:\data\support\hibernate-entitymanager-3.3.1.GA.
- Download Commons Beanutils to C:\data\support\commons-beanutils-1.7.0.
- Download Commons Digester to C:\data\support\commons-digester-1.8.
- Download Commons Collections to C:\data\support\commons-collections-3.2.
- Download JUnit to C:\data\support\junit4.4.
- Download Hypersonic to C:\data\support\hsqldb.
- Extract the contents of SuperSimpleJPA.zip to c:\data\workspaces.
- Run the DatabasePlay utility. Its output, ignoring the debug messages, should be:
Hibernate: insert into Person (id, name) values (null, ?) Hibernate: call identity() person id: 1 person id: 0 person id: 1 Done.