我怎么可以遍历从文件属性? [英] How can I iterate over properties from a file?
问题描述
我所有的项目和它们的版本在一个属性文件中定义是这样的:
All my projects and their versions are defined in a properties file like this:
ProjectNameA=0.0.1
ProjectNameB=1.4.2
我想遍历所有的项目,并在Ant脚本中使用他们的名字和版本。
I'd like to iterate over all the projects, and use their names and versions in an Ant script.
在present我读使用属性任务的整个文件,然后遍历一个给定的列表中进行这样的循环:
At present I read the entire file using the property task, then iterate over a given list in a for loop like this:
<for list="ProjectNameA,ProjectNameB" param="project">
<sequential>
<echo message="@{project} has version ${@{project}}" />
</sequential>
</for>
我怎样才能避免项目名称的硬编码在for循环?
基本上遍历各行,因为我去提取名称和项目的版本。
How can I avoid the hard-coding of the project names in the for loop? Basically iterate over each line and extract the name and the version of a project as I go.
推荐答案
看到你已经在使用antcontrib 为
,怎么样利用的<一个href=\"http://ant-contrib.sourceforge.net/tasks/tasks/propertyselector.html\"><$c$c>propertyselector$c$c>任务:
Seeing as you're already using antcontrib for
, how about making use of the propertyselector
task:
<property file="properties.txt" prefix="projects."/>
<propertyselector property="projects" match="projects\.(.*)" select="\1"/>
<property file="properties.txt" />
<for list="${projects}" param="project">
...
</for>
这里的想法是与项目
preFIX曾经读过的属性,并使用所产生的属性集,建设项目用逗号分隔的列表在 propertyselector
任务。然后,属性是重读的没有的的preFIX,让您的for循环可以像以前一样继续进行。
The idea here is to read the properties once with the projects
prefix, and use the resulting set of properties to build a comma-separated list of projects with the propertyselector
task. Then the properties are re-read without the prefix, so that your for loop can proceed as before.
这篇关于我怎么可以遍历从文件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!