修改Groovy中的现有xml文件

时间:2014-08-19 14:41:57

标签: xml groovy

我有一个pom.xml文件,如:

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test.plugins</groupId>
    <artifactId>testplugin</artifactId>
    <packaging>grails-plugin</packaging>
    <version>1.0</version>

    <name>testplugin</name>
    <description>testplugin</description>

    <dependencies>
        <dependency>
            <groupId>org.grails</groupId>
            <artifactId>grails-plugin-async</artifactId>
            <version>${grails.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>grails</id>
            <name>grails</name>
            <url>http://repo.grails.org/grails/core</url>
        </repository>
    </repositories>
</project>

我需要使用groovy xml slurper在上面的xml文件中添加更多节点。例如:想要在现有的xml文件中添加以下配置。

<pluginRepositories>
    <pluginRepository>
        <id>synergian-repo</id>
        <url>https://raw.github.com/synergian/wagon-git/releases</url>
    </pluginRepository>
</pluginRepositories>

同样,我想在依赖元素中添加新的依赖元素:

<dependency>
    <groupId>org.grails.plugins</groupId>
    <artifactId>tomcat</artifactId>
    <version>7.0.42</version>
    <type>zip</type>
    <scope>provided</scope>
</dependency>

我怎样才能做到这一点?我看了一些例子&amp;书面代码如:

new File("pom.xml").withWriter('UTF-8') { w ->
    def xml = new MarkupBuilder(w)
        xml.pluginRepositories {
            pluginRepository {
                id "synergian-repo"
                url "https://raw.github.com/synergian/wagon-git/releases"
            }
        }
}

但是上面的代码只用新文本替换整个文件。

1 个答案:

答案 0 :(得分:4)

你走了:

import groovy.xml.XmlUtil

def pom = '''
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test.plugins</groupId>
    <artifactId>testplugin</artifactId>
    <packaging>grails-plugin</packaging>
    <version>1.0</version>

    <name>testplugin</name>
    <description>testplugin</description>

    <dependencies>
        <dependency>
            <groupId>org.grails</groupId>
            <artifactId>grails-plugin-async</artifactId>
            <version>${grails.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>grails</id>
            <name>grails</name>
            <url>http://repo.grails.org/grails/core</url>
        </repository>
    </repositories>
</project>
'''

def xml = new XmlSlurper(false,false).parseText(pom)
xml.appendNode {
    pluginRepositories {
        pluginRepository {
            id 'synergian-repo'
            url 'https://raw.github.com/synergian/wagon-git/releases'
        }
    }
}
xml.dependencies.appendNode {
    dependency {
        groupId 'org.grails.plugins'
        artifactId 'tomcat'
        version '7.0.42'
        type 'zip'
        scope 'provided'
    }
}
XmlUtil.serialize(xml)