Ant, (over) write to 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> 

to a file named pom.xml in the current directory.

I tried 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 got an error message:

 echo doesn't support the nested "dependencies" element. 
+4
source share
3 answers

The ant parser reads the data you want to echo as an attempt to add invalid children to the parent <echo/> . If you want to track this information to pom.xml , you should use &lt; and &gt; to encode the output of your element:

 <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> 
+3
source

You should avoid content with a CDATA tag, which also means that it will not interpret variable substitution, so I would break it into three echo expressions.

  <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> 
+24
source

You have an 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> 
+13
source

All Articles