如何迭代文件的属性?
我的所有项目及其版本都在如下属性文件中定义:
ProjectNameA=0.0.1
ProjectNameB=1.4.2
我想迭代所有项目,并在 Ant 脚本中使用它们的名称和版本。
目前,我使用属性任务读取整个文件,然后在 for 循环中迭代给定列表,如下所示:
<for list="ProjectNameA,ProjectNameB" param="project">
<sequential>
<echo message="@{project} has version ${@{project}}" />
</sequential>
</for>
如何避免在 for 循环中对项目名称进行硬编码? 基本上迭代每一行并提取项目的名称和版本。
All my projects and their versions are defined in a properties file like this:
ProjectNameA=0.0.1
ProjectNameB=1.4.2
I'd like to iterate over all the projects, and use their names and versions in an Ant script.
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>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
既然您已经在使用 antcontrib
for
,那么如何利用propertyselector
任务:这里的想法是使用
projects
前缀读取属性一次,并使用生成的属性集构建逗号- 使用propertyselector
任务分隔的项目列表。然后,在没有前缀的情况下重新读取属性,以便 for 循环可以像以前一样继续进行。Seeing as you're already using antcontrib
for
, how about making use of thepropertyselector
task: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 thepropertyselector
task. Then the properties are re-read without the prefix, so that your for loop can proceed as before.如果您正在阅读其他 .property 文件(除了 build.properties 之外),您需要记住的一点是范围界定。如果您读取附加文件(通过属性 file="foo.property")标记,ant 将显示该文件已读取,并且属性已加载。然而,当你去引用它们时,它们却是未定义的。
Something you want to keep in mind, if you are reading additional .property files (besides build.properties) is scoping. If you read an additional file (via the property file="foo.property") tag, ant will show that the file was read, and the properties loaded. However, when you goto reference them, they come up un-defined.