原来我的POM中groupId是org.tinygroup,artifactId是对应的工程名,如parser。
后出有一个处理上的原因,而且便于进行分隔,还可以避免与别人的冲突,想把所有的artifactId前面增加个“org.tinygroup.”前缀,比如parser就变成org.tinygroup.parser。但是这样一来,所有的依赖信息也全都对不上了,也就是要对工程的artifactId及依赖中的artifactId都进行修改才行。
由于工程数比较多,一个一个手工改总是麻烦的,因此就想着写程序进行处理。
实现方案1:
public class ChangePom { public static void main(String[] args) throws Throwable { File file1 = new File("D:\\SVN\\tinyorg-code\\trunk\\Sources\\"); processFolder(file1); } private static void processFolder(File file1) throws Exception { File[] files = file1.listFiles(); for (File file : files) { if (file.isDirectory()) { processFolder(file); } if (file.getName().equals("pom.xml")) { processPomFile(file); } } } private static void processPomFile(File file) throws Exception { System.out.println("processing:" + file.getAbsolutePath()); XmlStringParser parser = new XmlStringParser(); XmlDocument doc = parser.parse(IOUtils.readFromInputStream(new FileInputStream(file), "utf-8")); XmlNode dependencies = doc.getRoot().getSubNode("dependencies"); XmlNode projectArtifactId = doc.getRoot().getSubNode("artifactId"); projectArtifactId.setContent("org.tinygroup" + projectArtifactId.getContent().trim()); if (dependencies != null) { ListdependencyList = dependencies.getSubNodes("dependency"); if (dependencyList != null) { for (XmlNode node : dependencyList) { XmlNode groupId = node.getSubNode("groupId"); if (groupId.getContent().trim().equals("org.tinygroup")) { XmlNode artifactId = node.getSubNode("artifactId"); artifactId.setContent("org.tinygroup." + artifactId.getContent().trim()); } } } } XmlFormater formater = new XmlFormater(); IOUtils.writeToOutputStream(new FileOutputStream(file), formater.format(doc), "UTF-8"); }}
呵呵,程序运行一会,马上搞定了。
当然,这个时候,可能感觉还是有点麻烦,OK,再换一种写法:
public class ChangePom1 { public static void main(String[] args) throws Throwable { FileObject fileObject= VFS.resolveFile("D:\\SVN\\tinyorg-code\\trunk\\Sources\\"); fileObject.foreach(new FileNameFileObjectFilter("pom\\.xml"),new FileObjectProcessor() { public void process(FileObject fileObject) throws Exception { System.out.println("processing:" + fileObject.getAbsolutePath()); XmlStringParser parser = new XmlStringParser(); XmlDocument doc = parser.parse(IOUtils.readFromInputStream(fileObject.getInputStream(), "utf-8")); XmlNode dependencies = doc.getRoot().getSubNode("dependencies"); XmlNode projectArtifactId = doc.getRoot().getSubNode("artifactId"); projectArtifactId.setContent("org.tinygroup" + projectArtifactId.getContent().trim()); if (dependencies != null) { ListdependencyList = dependencies.getSubNodes("dependency"); if (dependencyList != null) { for (XmlNode node : dependencyList) { XmlNode groupId = node.getSubNode("groupId"); if (groupId.getContent().trim().equals("org.tinygroup")) { XmlNode artifactId = node.getSubNode("artifactId"); artifactId.setContent("org.tinygroup." + artifactId.getContent().trim()); } } } } XmlFormater formater = new XmlFormater(); IOUtils.writeToOutputStream(fileObject.getOutputStream(), formater.format(doc), "UTF-8"); } }); }}
下面的xml格式化并输出到文件中,也可以写成:
formater.format(doc,fileObject.getOutputStream();
当然,上面只是临时进行一下处理,中间的Xml处理写得稍微丑陋一点。