繁体   English   中英

如何在 Windows 上通过 POM 文件在 XML 中添加自定义标签?

[英]How to add custom tag in XML via POM file on Windows?

我正在使用maven-dependency-plugin unpack目标从 maven 存储库中下载并提取一个工件,如下面的 POM.xml 所示

<build>
    <directory>${project.basedir}/target</directory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <id>prod-artifacts</id>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>org.one.two</groupId>
                                <artifactId>three</artifactId>
                                <version>4.3.2</version>
                                <type>zip</type>
                                <overWrite>false</overWrite>
                                <outputDirectory>c:\myDir</outputDirectory>
                                <excludes>**/*.sh,**/*.tar.gz</excludes>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            <executions>
        </plugin>
    </plugins>
</build>

提取的工件有一个 XML 文件,我想在其中编辑以添加一个名为MyTag的新标签(如果尚不存在)。 这必须作为同一 POM.xml 的一部分完成

我怎样才能做到这一点? 我在 Windows 上工作。

我要在其中添加名为MyTag的新 TAG 的 XML 文件具有以下结构

<?xml version="1.0" encoding="UTF-8"?>
<Root>

    <!-- I want to add below tag here like this
    <MyTag className="org.customClass" />
    -->

    <Tag1>dir1/first.xml</Tag1>
    <Tag1>dir2/second.xml</Tag1>
    <Tag2>${dir.base}/three.xml</Tag2>

</Root>

添加新标签的 xml 编辑逻辑必须是解压 zip 的同一 POM.xml 的一部分。

谁能指导或分享有关如何在 Windows 上实现此目的的示例? 如果共享一些示例代码或共享具有建议方法的任何插件信息,那将是很棒的。

您可以像这样使用简单的 XSLT 转换:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="Root">
    <xsl:copy>
      <MyTag className="org.customClass" />
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>

然后您可以使用 PowerShell 脚本执行它:

$inputFile = Path to your input file
$XSLTfile = Path to your XSLT file
$outputFile = Path to your output file

$xsltsettings = New-Object System.Xml.Xsl.XsltSettings
$xmlresolver = New-Object System.Xml.XmlUrlResolver
$transformation = New-Object System.Xml.Xsl.XslCompiledTransform
$transformation.Load($XSLTfile, $xsltsettings, $xmlresolver)

$arguments = New-Object System.Xml.Xsl.XsltArgumentList

$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true
$xmlsettings.IndentChars = "    "

$writer = [System.XML.XmlWriter]::Create($outputFile, $xmlsettings)

$transformation.Transform($inputFile, $arguments, $writer)
$writer.Flush()
$writer.Close()

(我只保留了基础知识,您可以在脚本中添加错误检查。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM