2015-02-26 85 views
0

組合名單我有這樣的XML:循環槽的XML文件在ANT

<?xml version="1.0" encoding="UTF-8"?> 
<projects> 
    <project action="D">Project1</project> 
    <project action="M">Project2</project> 
</projects> 

我想用螞蟻循環槽項目和時動作d執行一段代碼,和另一件的代碼時動作不D.

我迄今取得的進展是:

<target name="test"> 
    <xmlproperty file="changeList.xml"/> 
    <for list="${projects.project}" param="project"> 
    <sequential> 
     <echo>The project name is @{project} Action @{project.action}</echo> 
    </sequential> 
    </for> 
</target> 

結果現在是:

[echo] Project name is Project1 Action is @{project.action} 
[echo] Project name is Project2 Action is @{project.action} 

據我所知,我有2個單獨的列表,我沒有得到結果中的行動。我需要循環通過我的xml文件,這兩個屬性都可用(項目名稱和操作)作爲參數傳遞給下一個目標。

回答

1

隨着ant插件任務xmltask你有兩種可能性。

xmltask具有嵌套動作 =>運行包含在動作任務容器的xpath表達式 的每場比賽的所有任務(action屬性= d在本實施例中):

<project> 
<!-- Import XMLTask --> 
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/> 

<!-- loop over all projects with attribute action=D --> 
<xmltask source="changeList.xml"> 
    <call path="//projects/project[@action='D']"> 
    <param path="text()" name="projname"/> 
    <!-- define other params if needed .. --> 
    <param value="bar" name="foo"/> 
    <!-- inside action adress params with @{..} syntax ! --> 
    <actions> 
     <echo>Action D => @{projname}${line.separator}Param @@{foo} => @{foo}</echo> 
    </actions> 
    </call> 
</xmltask> 

</project> 

xmltask主叫ant目標爲xpath表達式的每個匹配(在此示例中屬性操作!= D )。
在這種情況下,您必須使用來自目標(此代碼段中的目標主體)的xmltask,否則您將得到BuildFailed => xmltask task at the top level must not invoke its own build file

<project default="main"> 
<!-- Import XMLTask --> 
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/> 

<target name="main"> 
<xmltask source="changeList.xml"> 
    <call path="//projects/project[@action != 'D']" target="foo"> 
    <param path="text()" name="projname"/> 
    <param value="bar" name="foo"/> 
    </call> 
</xmltask> 
</target> 

<!-- you have to use the propertysyntax ${...} in the called target 
    for the params ! --> 

<target name="foo"> 
<echo> 
${projname} => Action != D 
Param $${foo} is ${foo} 
</echo> 
</target> 

</project> 
+0

我可以很容易地調整這我的需要,謝謝! –