0
votes

I need to retrieve all the properties' names from a properties file before loading it (using Ant)

I'll go into detail to explain the whole process:

  1. A first properties file (let's name it as a.properties) is read and all its properties loaded as project's properties.

    #a.properties's contents
    myvar1=1
    myvar2=someTextHere
    
  2. A second file (let's say b.properties) has to be loaded on the project. Some already-set properties can also be contained in this second file, so what we have to do is to update such variables with the value found on it (by means of the ant-contrib's var target)

    #b.properties's contents
    myvar1=2  #updated value for a property that's is already set on the project
    myvar3=1,2,3,4,5,6
    
  3. So the expected subset (from a ANT project's properties perspective) of property/value pairs would be:

    myvar1=2
    myvar2=someTextHere
    myvar3=1,2,3,4,5,6
    

We cannot change the order in which those files are loaded on the project, which would be the easiest way of solving the issue (because of the behavior adopted by Ant when setting's properties)

Any feedback will be highly appreciated.

Regards

3
Please show the respective build script that you have. - Rao
I don't understand why you cannot change the order in which the properties files are loaded. You state that it's due Ant's property immutability, but you can simply use this to your advantage (this is a design feature of Ant, not a shortcoming). - CAustin

3 Answers

0
votes

I assume that you need to read properties from different files before you build your source code

<target name=-init-const-properties description="read all properties required">
  <propertyfile file="AbsolutePathToPropertyFile" comment="Write meaningfull 
    about the properties">
        <entry value="${myvar1}" key="VAR1"/>
        <entry value="${myvar2}" key="VAR2"/>
  </propertyfile>
</target>

Note: you need to add proper AbsolutePathToPropertyFileand comment if required

In the target -init-const-properties you can add as many files you want to read and use this target as dependent target in which you going to use these property values. hope this will answer your question

0
votes

I recommend having a standard file for build defaults called "build.properties". If you need to override any settings, then create an optional file called "build-local.properties".

My advice is to keep build logic simple. Using the ant-contrib extension to make properties act like variables is rarely needed in my experience.

Example

├── build-local.properties
├── build.properties
└── build.xml

Running the project produces the following output, where the value "two" is substituted:

$ ant
build:
     [echo] Testing one, dos, three

Delete the optional file and it goes back to default values:

$ rm build-local.properties
$ ant

build:
     [echo] Testing one, two, three

build.xml

The secret is the order in which the property files are loaded. If they don't exist then they don't create properties.

<project name="demo" default="build">

  <property file="build-local.properties"/>
  <property file="build.properties"/>

  <target name="build">
    <echo message="hello ${myvar1}, ${myvar2}, ${myvar3}"/>
  </target>

</project>

build.properties

myvar1=one
myvar2=two
myvar3=three

build-local.properties

myvar2=dos
0
votes

Finally, the approach I followed was to specify the second properties file (b.properties) from the command line:

ant <my_target> -propertyfile b.properties

So that's work fine to me...

Thanks all of you for your help.