posts - 27,  comments - 0,  trackbacks - 0
          下面是一個具體的案例,即對整個目錄中的POM樹進行批量處理 。
          原來我的POM中groupId是org.tinygroup,artifactId是對應的工程名,如parser。
          后出有一個處理上的原因,而且便于進行分隔,還可以避免與別人的沖突,想把所有的artifactId前面增加個“org.tinygroup.”前綴,比如parser就變成org.tinygroup.parser。但是這樣一來,所有的依賴信息也全都對不上了,也就是要對工程的artifactId及依賴中的artifactId都進行修改才行。
          由于工程數比較多,一個一個手工改總是麻煩的,因此就想著寫程序進行處理。
           1public class ChangePom {
           2    public static void main(String[] args) throws Throwable {
           3        File file1 = new File("D:\\SVN\\tinyorg-code\\trunk\\Sources\\");
           4        processFolder(file1);
           5    }

           6
           7    private static void processFolder(File file1) throws Exception {
           8        File[] files = file1.listFiles();
           9        for (File file : files) {
          10            if (file.isDirectory()) {
          11                processFolder(file);
          12            }

          13            if (file.getName().equals("pom.xml")) {
          14                processPomFile(file);
          15            }

          16        }

          17    }

          18
          19    private static void processPomFile(File file) throws Exception {
          20        System.out.println("processing:" + file.getAbsolutePath());
          21        XmlStringParser parser = new XmlStringParser();
          22        XmlDocument doc = parser.parse(IOUtils.readFromInputStream(new FileInputStream(file), "utf-8"));
          23        XmlNode dependencies = doc.getRoot().getSubNode("dependencies");
          24        XmlNode projectArtifactId = doc.getRoot().getSubNode("artifactId");
          25        projectArtifactId.setContent("org.tinygroup" + projectArtifactId.getContent().trim());
          26        if (dependencies != null{
          27            List<XmlNode> dependencyList = dependencies.getSubNodes("dependency");
          28            if (dependencyList != null{
          29                for (XmlNode node : dependencyList) {
          30                    XmlNode groupId = node.getSubNode("groupId");
          31                    if (groupId.getContent().trim().equals("org.tinygroup")) {
          32                        XmlNode artifactId = node.getSubNode("artifactId");
          33                        artifactId.setContent("org.tinygroup." + artifactId.getContent().trim());
          34                    }

          35                }

          36            }

          37        }

          38
          39        XmlFormater formater = new XmlFormater();
          40        IOUtils.writeToOutputStream(new FileOutputStream(file), formater.format(doc), "UTF-8");
          41    }

          42}

          實現方案1:

            呵呵,程序運行一會,馬上搞定了。
            當然,這個時候,可能感覺還是有點麻煩,OK,再換一種寫法:
             1public class ChangePom1 {
             2    public static void main(String[] args) throws Throwable {
             3        FileObject fileObject= VFS.resolveFile("D:\\SVN\\tinyorg-code\\trunk\\Sources\\");
             4        fileObject.foreach(new FileNameFileObjectFilter("pom\\.xml"),new FileObjectProcessor() {
             5            public void process(FileObject fileObject) throws Exception {
             6                System.out.println("processing:" + fileObject.getAbsolutePath());
             7                XmlStringParser parser = new XmlStringParser();
             8                XmlDocument doc = parser.parse(IOUtils.readFromInputStream(fileObject.getInputStream(), "utf-8"));
             9                XmlNode dependencies = doc.getRoot().getSubNode("dependencies");
            10                XmlNode projectArtifactId = doc.getRoot().getSubNode("artifactId");
            11                projectArtifactId.setContent("org.tinygroup" + projectArtifactId.getContent().trim());
            12                if (dependencies != null{
            13                    List<XmlNode> dependencyList = dependencies.getSubNodes("dependency");
            14                    if (dependencyList != null{
            15                        for (XmlNode node : dependencyList) {
            16                            XmlNode groupId = node.getSubNode("groupId");
            17                            if (groupId.getContent().trim().equals("org.tinygroup")) {
            18                                XmlNode artifactId = node.getSubNode("artifactId");
            19                                artifactId.setContent("org.tinygroup." + artifactId.getContent().trim());
            20                            }

            21                        }

            22                    }

            23                }

            24
            25                XmlFormater formater = new XmlFormater();
            26                IOUtils.writeToOutputStream(fileObject.getOutputStream(), formater.format(doc), "UTF-8");
            27            }

            28        }
            );
            29    }

            30}
          下面的xml格式化并輸出到文件中,也可以寫成:
            1formater.format(doc,fileObject.getOutputStream();
          當然,上面只是臨時進行一下處理,中間的Xml處理寫得稍微丑陋一點。


          歡迎訪問框架論壇:http://web.j2ee.top。本例涉及的代碼和框架資料,將會在論壇分享。《自己動手寫框架》成員QQ群:228977971,讓我們一起動手,了解框架的奧秘! 

          posted on 2015-06-08 23:12 柏然 閱讀(73) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2015年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 普兰店市| 忻州市| 保靖县| 勐海县| 怀集县| 永清县| 鸡泽县| 榆树市| 游戏| 客服| 霍城县| 肥乡县| 赞皇县| 右玉县| 广西| 镇雄县| 会同县| 黄山市| 丽水市| 桃园县| 云龙县| 常宁市| 平山县| 兰坪| 嘉禾县| 枣强县| 莒南县| 邢台县| 洪湖市| 姚安县| 满城县| 海城市| 友谊县| 寿光市| 常德市| 彭泽县| 平南县| 慈利县| 吉安县| 吴桥县| 阳原县|