2014-09-11 69 views
0

我想知道如何将groovy脚本变量(此处为compName,compPath)传递给ant目标(这里是:build.application ) 我想在这build.xml文件在ANT任务中使用groovy脚本来调用另一个ant任务并将groovy变量传递给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> 
+0

通过系统属性? – Opal 2014-09-11 09:34:29

回答

1

要回答你的问题直接和COMPNAME提供给所有蚂蚁的目标compPath的数值,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>