修改后使用MavenXpp3Writer编写新的pom.xml会更改原始文件顺序或标签

时间:2019-05-23 14:16:39

标签: java pom.xml

我有此代码,可用于更改pom.xml值 我确实成功更改了原始文件,但是在新pom中,原始标签的顺序不同。 知道为什么吗?

File file = new File(pomxml1);
        if(file.getName().equalsIgnoreCase("pom.xml")) {
            System.out.println("file:" + file.getCanonicalPath());

            Model model = null;
            try {
                File pomFile = new File(file.getCanonicalPath());
                model = new MavenXpp3Reader().read(new FileInputStream(pomFile));
                boolean isModified = false;
                if(model.getVersion()!=null )
                {
                    model.setVersion(newVersion);
                    if(!isModified) {
                        isModified = true;
                    }
                }

                if(model.getParent()!=null && model.getParent().getVersion() != null )
                {
                    model.getParent().setVersion(newVersion);
                    if(!isModified) {
                        isModified = true;
                    }
                }
                if(model.getDependencyManagement()!=null && model.getDependencyManagement().getDependencies()!=null) {
                    for (Dependency dependency : model.getDependencyManagement().getDependencies()) {
                        if (dependency.getGroupId().equalsIgnoreCase("org.test")) {
                            System.out.println(dependency.getVersion());
                            if (dependency.getVersion() != null ) {
                                dependency.setVersion(newVersion);
                                if (!isModified) {
                                    isModified = true;
                                }
                            }
                        }
                    }
                }
                if(isModified) {
                    System.out.println(model.toString());
                    new MavenXpp3Writer().write(new FileWriter(pomFile), model);
                   // new MavenXpp3Writer().write(new FileOutputStream(pomFile), model);
                }

            }
            catch (XmlPullParserException | IOException e) {
                e.printStackTrace();
            }

        }

0 个答案:

没有答案