Apache Ant: how to execute target inside condition with specific data from XmlProperty
<item>
<property name="someId" selected="true"></property>
<property name="someId2"></property>
</item>
this is my XmlProperty file, now my target need to imple开发者_StackOverflowmented only the property with the selected attribute, i don't know any way to access to property by index with ApacheAnt or run with foreach & index on XmlProperty.
thx:)
Would it help to restructure your XML properties definition so you can access the properties by name.
Restructured XML properties:
<item>
<someId selected="true"/>
<someId2/>
</item>
Build file:
<project default="conditionals">
<xmlproperty file="properties.xml" keepRoot="false" collapseAttributes="true"/>
<target name="conditionals" depends="ifSomeId, ifSomeId2"/>
<target name="ifSomeId" if="someId.selected">
<echo message="in ifSomeId"/>
</target>
<target name="ifSomeId2" if="someId2.selected">
<echo message="in ifSomeId2"/>
</target>
</project>
Output:
$ ant
Buildfile: build.xml
ifSomeId:
[echo] in ifSomeId
ifSomeId2:
conditionals:
BUILD SUCCESSFUL
Total time: 0 seconds
You could load the properties file in ant through a filter chain which excludes properties which do not have have the selected="true"
attribute.
the situation is difference in my case, i have one tag called settings and he contain 10 times the tag project, like array of items, now i'm not interested on checking every on of the projects, i want to slice or just access to specific one by index or something similar, this is my current solution right now.
<target name="GetSettings">
<echo message="Getting settings for compiling"></echo>
<loadfile srcfile="Settings.xml" property="settings">
<filterchain>
<linecontains>
<contains value="selected"></contains>
</linecontains>
</filterchain>
</loadfile>
<echo message="${settings}" file="temp.xml"></echo>
<xmlproperty file="temp.xml" collapseAttributes="true"></xmlproperty>
<delete file="temp.xml"></delete>
</target>
not perfect but work great.
For Ant scripts driven by a xmlproperty file you should use the power of Xpath. With the assistance of the xmltask (recommended for all xml related parts in your workflow) you may go like that =
your xmlpropertyfile somefile.xml =
<item>
<property name="someId" selected="true"></property>
<property name="someId2"></property>
</item>
examplescript with xmltask using xpath, for every match, the referenced target 'sometarget' is called =
<project default="main">
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
<target name="main">
<xmltask source="somefile.xml">
<call path="/item/property[@selected='true']" target="sometarget">
<param name="name" path="@name"/>
</call>
</xmltask>
</target>
<target name="sometarget">
<echo>${name}</echo>
</target>
</project>
output =
[echo] someId
some resources for xpath =
- XPath Tutorial : http://zvon.org/xxl/XPathTutorial/General/examples.html
- Eclipse Plugin for evaluating XPath expressions : XPath Developer
- Tool to evaluate xpath expressions : XPathExplorer
精彩评论