17

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.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Herr K
  • 1,751
  • 3
  • 16
  • 24

2 Answers2

13

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.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • Added relation to a given bug, and the sequential alement to the for element to make it more obvious. – Gábor Lipták Oct 05 '11 at 07:38
  • Wanted to add an edit, but it was rejected. So I add it as a comment: the for task is not listed somehow in the antcontrib.properties in the latest jar. A workaround is to include it as an class with a name in a taskdef like it is stated in the bug report at http://sourceforge.net/tracker/?func=detail&aid=2838574&group_id=36177&atid=416920 – Gábor Lipták Oct 05 '11 at 08:50
  • Thanks for the post Martin. For others who did not have things setup for ant contrib, you may need the following line too: Reference: http://ant-contrib.sourceforge.net/tasks/index.html#intro – James Oravec Jul 24 '12 at 23:04
0

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.

eric manley
  • 99
  • 1
  • 2