在ANT任务中使用groovy脚本来调用另一个ant任务并将groovy变量传递给ANT目标属性

时间:2014-09-11 08:40:32

标签: ant groovy

我想知道如何将groovy脚本变量(这里:compName,compPath)传递给ant目标(这里:build.application)     我想使compName和compPath的值可用于此build.xml中的所有ant目标

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

1 个答案:

答案 0 :(得分:1)

要回答您的直接问题,ant任务接受property个孩子在您正在呼叫的目标所使用的新项目中设置属性:

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

但您也可以考虑xmltask,其“调用”函数可以在没有所有Groovy代码的情况下实现相同的功能。

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