GroovyのXmlSlurperを使用してMavenのpom.xmlを解析および変更しようとしています。私のpom.xmlは名前空間xsiを宣言しています。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a-group-id</groupId>
<artifactId>an-artifact-id</artifactId>
私のGroovyソースは次のとおりです。
import groovy.xml.XmlUtil
def pom = new XmlSlurper().parse('pom.xml')
.declareNamespace('': 'http://maven.apache.org/POM/4.0.0',
xsi: 'http://www.w3.org/2001/XMLSchema-instance')
//manipulate the pom
println XmlUtil.serialize(pom)
お気づきのとおり、最初の名前空間を空として宣言しました。ただし、出力ではtag0がどこにでも追加されます。
<?xml version="1.0" encoding="UTF-8"?>
<tag0:project xmlns:tag0="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<tag0:modelVersion>4.0.0</tag0:modelVersion>
<tag0:groupId>a-group-id</tag0:groupId>
<tag0:artifactId>an-artifact-id</tag0:artifactId>
それを回避する方法は?
今のところ、私の回避策はタグを手動で削除することです。
println XmlUtil.serialize(pom).replaceAll('tag0:', '').replaceAll(':tag0', '')