pindare
pindare

Reputation: 2470

Ant, (over)write into a file

How to write (or overwrite) the following contents:

            <dependencies>
            <dependency>
                <groupId>ged.eprom</groupId>
                <artifactId>epromx</artifactId>
                <version>${version.to.set}</version>
                <classifier>stubjava</classifier>
            </dependency>
        </dependencies>

into file called pom.xml in the current directory.

I have tried the ant script:

        <echo file="pom.xml">
        <dependencies>
            <dependency>
                <groupId>ged.eprom</groupId>
                <artifactId>epromx</artifactId>
                <version>${version.to.set}</version>
                <classifier>stubjava</classifier>
            </dependency>
        </dependencies>
    </echo>

But I obtained the error message:

echo doesn't support the nested "dependencies" element.

Upvotes: 4

Views: 11660

Answers (3)

Alexander Kj&#228;ll
Alexander Kj&#228;ll

Reputation: 4336

You must escape the content with a CDATA tag, that also means that it won't interpret the variable substitution, so I would break it up in three echo statements.

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

Upvotes: 24

Maxime Pacary
Maxime Pacary

Reputation: 23041

You have the echoxml task:

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>

Upvotes: 13

akf
akf

Reputation: 39485

The ant parser is reading the data that you want to echo as an attempt to add invalid child elements to an <echo/> parent. If you would like to echo that info out to pom.xml, you should use the &lt; and &gt; entities to encode your element output:

<echo file="pom.xml">
            &lt;dependencies&gt;
                    &lt;dependency&gt;
                            &lt;groupId&gt;ged.eprom&lt;/groupId&gt;
                            &lt;artifactId&gt;epromx&lt;/artifactId&gt;
                            &lt;version&gt;${version.to.set}&lt;/version&gt;
                            &lt;classifier&gt;stubjava&lt;/classifier&gt;
                    &lt;/dependency&gt;
            &lt;/dependencies&gt;
</echo>

Upvotes: 3

Related Questions