views:

1299

answers:

2

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.
+3  A: 

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 file="pom.xml" append="true"><![CDATA[</version>
                            <classifier>stubjava</classifier>
                    </dependency>
            </dependencies>
   ]]> </echo>
Alexander Kjäll
Good catch on the ${} substitution
skaffman
+1  A: 

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