Ant,(over)写入文件

pin*_*are 4 ant scripting file

如何编写(或覆盖)以下内容:

            <dependencies>
            <dependency>
                <groupId>ged.eprom</groupId>
                <artifactId>epromx</artifactId>
                <version>${version.to.set}</version>
                <classifier>stubjava</classifier>
            </dependency>
        </dependencies>
Run Code Online (Sandbox Code Playgroud)

到当前目录中名为pom.xml的文件中.

我试过蚂蚁脚本:

        <echo file="pom.xml">
        <dependencies>
            <dependency>
                <groupId>ged.eprom</groupId>
                <artifactId>epromx</artifactId>
                <version>${version.to.set}</version>
                <classifier>stubjava</classifier>
            </dependency>
        </dependencies>
    </echo>
Run Code Online (Sandbox Code Playgroud)

但我收到了错误消息:

echo doesn't support the nested "dependencies" element.
Run Code Online (Sandbox Code Playgroud)

Ale*_*äll 24

您必须使用CDATA标记来转义内容,这也意味着它不会解释变量替换,因此我会在三个echo语句中将其拆分.

    <echo file="pom.xml"><![CDATA[
            <dependencies>
                    <dependency>
                            <groupId>ged.eprom</groupId>
                            <artifactId>epromx</artifactId>
                            <version>]]></echo>
    <echo file="pom.xml" append="true">${version.to.set}</echo>
    <echo file="pom.xml" append="true"><![CDATA[</version>
                            <classifier>stubjava</classifier>
                    </dependency>
            </dependencies>
   ]]> </echo>
Run Code Online (Sandbox Code Playgroud)


Fro*_*y Z 13

你有echoxml任务:

http://ant.apache.org/manual/Tasks/echoxml.html

<echoxml file="pom.xml">
  <dependencies>
    <dependency>
      <groupId>ged.eprom</groupId>
      <artifactId>epromx</artifactId>
      <version>${version.to.set}</version>
      <classifier>stubjava</classifier>
    </dependency>
  </dependencies>
</echoxml>
Run Code Online (Sandbox Code Playgroud)

  • 虽然这个特定的XML片段无关紧要,但应该注意的是`echoxml`任务并不一定要写出你逐字提供的XML.例如,至少在Windows上使用Ant 1.8.2(以及其他平台上的其他版本)时,使用`echoxml`任务似乎可以按字母顺序切换我包含的任何属性节点的顺序:`<echoxml file = "blah.xml"> <el b ="x"a ="x"/> </ echoxml>`将写出属性顺序切换为字母顺序,即`<el a ="x"b ="x "/>` (2认同)