在Groovy中修改现有的xml文件。

时间:2022-01-13 07:45:38

I have a pom.xml file like:

我有一个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>

I need to add some more nodes in the above xml file using groovy xml slurper. For example: wants to add the below configuration inside the existing xml file.

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

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

Similarly, I want to add new dependency element inside dependencies element:

同样,我想在dependencies元素中添加新的dependency元素:

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

How can I achieve this? I looked at some examples & written code like:

我如何做到这一点?我看了一些例子和编写的代码:

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"
            }
        }
}

But the above code replaces the whole file with only new text.

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

1 个解决方案

#1


4  

Here You go:

给你:

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)

#1


4  

Here You go:

给你:

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)