Suneel Kumar
Suneel Kumar

Reputation: 11

using groovy script in ANT task to call another ant task and passing groovy variables to ANT target properties

I would like to know how to pass groovy script variables(here: compName, compPath) to the ant target (here : build.application) I would like to make the values of compName and compPath available to all ant targets in this build.xml

<target name="xmlreader"  description="Clean deployment directory">

        <groovy>
            import javax.xml.xpath.*
            import javax.xml.parsers.DocumentBuilder;
            import javax.xml.parsers.DocumentBuilderFactory;
            import org.w3c.dom.Document;
            import org.w3c.dom.Element;
            import org.w3c.dom.Node;
            import org.w3c.dom.NodeList;
            def ant = new AntBuilder()
            File buildfile = new File("d:/Users/sk/workspace/build.xml")
            fileContent = buildfile.getText() 
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(buildfile);
            XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();
            XPathExpression expr = xpath.compile("BuildConfig/Applications/ApplicationConfig");
            NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i &lt;  nl.getLength() ; i++) {
                    String compName = (String)nl.item(i).getElementsByTagName("Name").item(0).getChildNodes().item(0).getNodeValue();
                    String compPath = (String)nl.item(i).getElementsByTagName("SVN_Path").item(0).getChildNodes().item(0).getNodeValue();   
                    ant.echo "${compName}"
                    ant.echo "${compPath}" 

                    ant.ant( antfile: 'build.xml' ){
                             target(name: 'build.application')
                        }           
            }
        </groovy>
    </target>

Upvotes: 0

Views: 766

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

To answer your direct question, the ant task accepts property children to set properties in the new project used by the target you're calling:

ant.ant( antfile: 'build.xml', target: 'build.application') {
  property(name:'compName', value:compName)
  property(name:'compPath', value:compPath)
}

But you could also consider xmltask, whose "call" function can achieve the same thing without all the Groovy code.

<xmltask source="d:/Users/sk/workspace/build.xml">
  <call path="BuildConfig/Applications/ApplicationConfig" target="build.application">
    <param name="compName" path="Name" />
    <param name="compPath" path="SVN_Path" />
  </call>
</xmltask>

Upvotes: 1

Related Questions