Peter West

"...the kingdom of heaven is like a merchant in search of fine pearls, who, 
finding one pearl of great value, sold all that he had and bought it."

Just define "well-known" properties which can be used from the command line, or 
set from within a master file, and define another "actual" property which will 
take precedence.  Add logic to called.xml and siblings to assign the "public" 
value to the "actual" outfile variable.  If the actual variable was set at a 
higher level, that setting will take precedence. Otherwise, the value from the 
"public" well-known property will become the value of the "actual" variable.

Just make sure to document it within the file, so that someone trying to work 
out why the command line option didn't "take" will be able to find it.

<project name="Main" default="run">
        
   <target name="run">
        <property name="outFileName" value="START"/>
        <echo>Main: outFileName: ${outFileName}</echo>
        
        <ant antfile="called.xml" target="run">
            <property name="realFileName" value="OTHER VALUE"/>
        </ant>
   </target>
        
</project>

<project name="called" default="run">

   <property name="realFileName" value="${outFileName}"/>
        
   <target name="run">
        <echo>called: outFileName: ${realFileName}</echo>
   </target>
        
</project>

Peter West

"...the kingdom of heaven is like a merchant in search of fine pearls, who, 
finding one pearl of great value, sold all that he had and bought it."


On 31 Jul 2014, at 10:02 pm, Al Le <al...@gmx.de> wrote:

> Hello.
> 
> Thank you for the quick reply.
> 
> I know that properties are immutable and should not be redefined. But my 
> situation is,
> that the caller (be it command line or an ant script) sets the property for 
> the called
> script.
> 
> If the property is defined not in the command line but in the script itself 
> (using the
> 'property' task) then the property is substituted in the called script. Bit 
> if it's set
> from the commnd line, it's not.
> 
> I looked into the ant source code. The reason seems to be that the properties 
> that are
> defined in the command line are set as *user* properties. Those properties 
> can't be
> redefined even through direct calls to project.setProperty().
> 
> How would you implement my goal (I hope it's clear what it is)?
> 
> If the main script is like this:
> 
> <project name="Main" default="run">
>       
>    <target name="run">
>         <property name="outFileName" value="START"/>
>       <echo>Main: outFileName: ${outFileName}</echo>
>       
>       <ant antfile="called.xml" target="run">
>           <property name="outFileName" value="OTHER VALUE"/>
>       </ant>
>    </target>
>       
> </project>
> 
> and I call it as "ant -f main.xml" (no "-D...") then the called script gets 
> the value
> "OTHER VALUE".
> 
> So why do the properties set from the command line get set as user properties 
> whereas
> properties set by the "property" task are set as usual properties?
> 
> I think they should be set in the same way.
> 
> Cheers
> Al
> 
> 
> 
>> Gesendet: Donnerstag, 31. Juli 2014 um 12:15 Uhr
>> Von: "Knuplesch, Jürgen" <juergen.knuple...@icongmbh.de>
>> An: "Ant Users List" <user@ant.apache.org>
>> Betreff: AW: How to override a property that was set in the command line via 
>> "-D..."
>> 
>> Hello,
>> 
>> because of the immutability of properties you have to redesign your scripts.
>> The first time you set a property is it. You are usually not able t change 
>> it later.
>> This is very important, because this is the only way to set properties from 
>> outside.
>> 
>> Why does your command line call then use "-D-DoutFileName=START", when you 
>> don’t want it?
>> 
>> Never do this, when you want to change the prop in your build.
>> If "START" is your default, then set it in the called target / Ant file, in 
>> your case "called.xml"
>> 
>> Its very important to understand why immutability is a feature and not a 
>> bug... (-;
>> 
>> Juergen
>> 
>> 
>> 
>> 
>> 
>> -----Ursprüngliche Nachricht-----
>> Von: Al Le [mailto:al...@gmx.de] 
>> Gesendet: Donnerstag, 31. Juli 2014 11:10
>> An: user@ant.apache.org
>> Betreff: How to override a property that was set in the command line via 
>> "-D..."
>> 
>> user@ant.apache.org
>> 
>> How to override a property that was set in the command line via "-D..."
>> 
>> Hello.
>> 
>> Could someone please help me with the following situation?
>> 
>> I have some ant scripts that are used in two ways:
>> 
>> 1. Standalone build, i.e. the script is executed directly from the command 
>> line 2. As a part of a larger build -- then the script is called via 'ant' 
>> from another
>>    script (here we have a main and a called scripts).
>> 
>> Each script uses a property 'outFileName' that specifies where to write some 
>> output to.
>> Both the main and the called script use this property. The script gets the 
>> value of the property passed from the caller.
>> 
>> The root script (i.e. the one called from the command line) gets passed the 
>> value of 'outFileName' via the "-DoutFileName=..." option.
>> 
>> When the main script calls a called script, it may specify another value for 
>> the property 'outFileName' (using the nested 'property' element). The called 
>> script should notice no difference how it was called (i.e. whether it is 
>> called from the command line or as a called script from another script).
>> 
>> Now the propblem: It turns out that the properties specified via "-D=..." 
>> are set as *user* properties. Hence it's not possible to redefine them via 
>> the nested elements. Ant not even using a script task with a call 
>> 'project.setProperty()'.
>> 
>> How would you solve this situation? I would not like introduce properties 
>> with a different name, since my goal is to have scripts that can be 
>> 'customized' by specifying a property with a well known name ('outFileName') 
>> -- be it on the command line or trough a nested element of an ant task. Thik 
>> of it as of script interface.
>> 
>> The only possible solution I can think of is to specify a custom property 
>> helper, but that's too much IMO and makes scripts not 'portable'.
>> 
>> Here's an example that I hope would explain the problem (I use ant 1.8.4 but 
>> I think the same problem would also occur with ant 1.9.x):
>> 
>> 
>> -- Main build file 'main.xml' --
>> 
>> <project name="Main" default="run">
>>      
>>    <target name="run">
>>      <echo>Main: outFileName: ${outFileName}</echo>
>>      
>>      <ant antfile="called.xml" target="run">
>>              <property name="outFileName" value="OTHER VALUE"/>
>>      </ant>
>>    </target>
>>      
>> </project>
>> 
>> 
>> -- Called build file 'called.xml' --
>> <project name="Called" default="run">
>>      
>>    <target name="run">
>>      <echo>Called: outFileName: ${outFileName}</echo>
>>    </target>
>> 
>> </project>
>> 
>> 
>> -- Command line --
>> ant -f main.xml -DoutFileName=START
>> 
>> 
>> -- Actual output --
>> run:
>>     [echo] Main: outFileName: START
>> run:
>>     [echo] Called: outFileName: START
>> 
>> 
>> -- Expected (desired) output --
>> run:
>>     [echo] Main: outFileName: START
>> run:
>>     [echo] Called: outFileName: OTHER VALUE
>> 
>> 
>> Any help will be much appreciated.
>> 
>> Al
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscr...@ant.apache.org For additional 
>> commands, e-mail: user-h...@ant.apache.org
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
>> For additional commands, e-mail: user-h...@ant.apache.org
>> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
> For additional commands, e-mail: user-h...@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@ant.apache.org
For additional commands, e-mail: user-h...@ant.apache.org

Reply via email to