1
votes

So ... I have build.xml that loads property file from basedir.

Then, as the target I perform the following:

<var name="Var1" value="<property_from_**first**_loaded_property_file>" />
<var name="<property_from_**first**_loaded_property_file>" unset="true"/>
<property file="../<other directory>/<**second**_property_file>.properties" />
<var name="Var2" value="<property_from_**second**_loaded_property_file>"/>

The ceavat here is that both has same property name. It cannot be changed.

So, in the end, I should get the property like:

Var1=<property_from_**first**_loaded_property_file>

Var2=<property_from_**second**_loaded_property_file>

But instead - I am getting signs that property (Var1) from first properties file is not unset and then filled with new value from second properties file. The thing that ant-contribs unset should deal with :/ ... something like:

Var1 = Var2

Why I am not getting the expected result?

3
from the echoed results I am getting signs that value from pirts properties file is not unset and then filled with new value from second properties file. The thing that ant-contribs unset should deal with :/HX_unbanned
what do the property files containoers
identical property names with different values (Var1=1 in first propfile ; Var1=2 in second propfile). This would be most elegant solution for my task, if only it would work as supposed...HX_unbanned

3 Answers

1
votes

I think the issue is that even though you're loading the variable into an antcontrib var, it's still an ant property first, thus immutable.

I know you can't change the property files, but what kind of freedom do you have with the script itself? You can try to leverage the scoping rules and the antcallback task to scope where the variables get loaded.

For example, the following achieves - albeit somewhat messily - what I think you're after:

<?xml version="1.0" encoding="utf-8"?>
<project name="Test" basedir=".">

    <path id="ant.classpath">
        <fileset dir="${basedir}">
            <include name="ant-contrib_AP.jar"/>
        </fileset>
    </path>

    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="ant.classpath"/>

    <target name="test">

        <antcallback target="load-more-prop" return="Var2"/>

        <loadproperties>
            <file file="prop1.properties"/>
        </loadproperties>

        <property name="Var1" value="${var}" />

        <echo>${Var1}</echo>
        <echo>${Var2}</echo>

    </target>

    <target name="load-more-prop">

        <loadproperties>
            <file file="prop2.properties"/>
        </loadproperties>

        <property name="Var2" value="${var}" />
    </target>
</project>

In my console I see:

Buildfile: C:\Users\mfelzani\workspace-junk\junk\build.xml
test:
load-more-prop:
 [echo] 7
 [echo] 1
BUILD SUCCESSFUL
Total time: 905 milliseconds

Which matches the values i set in prop1.properties and prop2.properties, respectively, for the var property.

0
votes

You can't unset the value.

WRONG: <var name="<property_from_**first**_loaded_property_file>" unset="true"/>

You have to unset the variable

CORRECT: <var name="Var1" unset="true"/>
0
votes

If you need to overwrite some existing property or userproperty (those properties defined via ant commandline
parameter -Dkey=value), you might use Ant Plugin Flaka as alternative for antcontrib.
With Flaka's let task you either create a new property or overwrite any existing property straightforward :

<project xmlns:fl="antlib:it.haefelinger.flaka">

 <property name="foo" value="bar"/>

 <!-- create new property -->
 <fl:let>foo := 'baar'</fl:let>
 <echo>$${foo} => ${foo}</echo>

 <!--
  overwrite existing property
  notice the double '::' in foo ::= 'baz'
 -->
 <fl:let>foo ::= 'baz'</fl:let>
 <echo>$${foo} => ${foo}</echo>

</project>