如何迭代文件的属性?

发布于 2024-09-05 14:22:36 字数 446 浏览 3 评论 0原文

我的所有项目及其版本都在如下属性文件中定义:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-09-12 14:22:36

既然您已经在使用 antcontrib for,那么如何利用 propertyselector 任务:

<property file="properties.txt" prefix="projects."/>
<propertyselector property="projects" match="projects\.(.*)" select="\1"/>

<property file="properties.txt" />
<for list="${projects}" param="project">
    ...
</for>

这里的想法是使用 projects 前缀读取属性一次,并使用生成的属性集构建逗号- 使用 propertyselector 任务分隔的项目列表。然后,在没有前缀的情况下重新读取属性,以便 for 循环可以像以前一样继续进行。

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>

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.

橪书 2024-09-12 14:22:36

如果您正在阅读其他 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文