You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Pa...@b-source.ch on 2008/06/11 12:22:23 UTC

how to check that a property exist

Hi all,

I'm trying to find out a solution for the below issue.

I've a properties file like that:

...
defaultPollerManagedServer=A
TestEvent.pollerManagedServer=B
DummyEvent.pollerManagedServer=C
...

I run my build.xml providing a parameter:

ant -DIPEvent=TestEvent

And I would like to set the value of a property named 'pollerEventValue'
according to these rules:

1) if exists a property like 

    <property name="pollerEvent"
value="${IPEvent}.pollerManagedServer"/>

then the requested value is:

    <property name="pollerEventValue" value="${${pollerEvent}}"/>
?????? but I think the syntax is wrong

2) else take as value

    <property name="pollerEventValue"
value="${defaultPollerManagedServer}"/>

So if I run:

 ant -DIPEvent=TestEvent   the expected value is B
 ant -DIPEvent=other       the expected value is A (the default one)

Could anyone help me to solve that ?

Thanks in advance for any help. 
ferp

IMPORTANT: 
This e-mail transmission is intended for the named 
addressee(s)only. 
Its contents are private, confidential and protected 
from disclosure and should not be read, copied or
disclosed by any other person. 
If you are not the intended recipient, we kindly ask
you to notify the sender immediately by telephone 
(+41 (0)58 806 50 00), to redirect the message to the 
account "info@b-source.ch" and to delete this e-mail.
E-mail transmissions may be intercepted, altered or 
read by unauthorized persons and may contain viruses.
Therefore, it is recommended that you use regular mail
or courier services for any information intended to be 
confidential. However, by sending us messages through
e-mail, you authorize and instruct us to correspond by 
e-mail in the relevant matter. 
Thank you.


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


Re: how to check that a property exist

Posted by Gilbert Rebhan <an...@schillbaer.de>.
Patrizio.Ferlito@b-source.ch schrieb:

> but I don't know how to use it.
> My ant version is: Apache Ant version 1.6.5 compiled on June 2 2005 and
> I read that script task depends on external libraries not included in
> the Ant distribution.

it's only the BeanScriptingFramework = bsf.jar and
the language specific jar, f.e. jruby.jar ...

> So I choose the "lowlevel" approach:
> 
> <echo file="tmp.properties">pollerValue=${pollerValue}</echo>
> 
> The properties file is created:
> 
> /tmp> cat tmp.properties 
> pollerValue=bnk01alm01
> 
> but then
> 
> <property name="pollerValue"   value="${pollerValue}"
> file="tmp.properties"/>
> 
> doesn't work. I'm trying to understand why ...

the property task has not attribute value

<property file="tmp.properties"/>

loads your propertyfile, then

<echo>$${pollerValue} == ${pollerValue}</echo>

will work.

See also =
http://ant.apache.org/manual/CoreTasks/property.html
http://ant.apache.org/manual/CoreTasks/loadfile.html
http://ant.apache.org/manual/CoreTasks/loadproperties.html


Regards, Gilbert

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


RE: how to check that a property exist

Posted by Pa...@b-source.ch.
Thanks again !!!

I'm looking at your last hint:

<script>
check your existing properties etc. ...
set new properties you need for further processing via use of ant api
<script/>

but I don't know how to use it.
My ant version is: Apache Ant version 1.6.5 compiled on June 2 2005 and
I read that script task depends on external libraries not included in
the Ant distribution.

So I choose the "lowlevel" approach:

<echo file="tmp.properties">pollerValue=${pollerValue}</echo>

The properties file is created:

/tmp> cat tmp.properties 
pollerValue=bnk01alm01

but then

<property name="pollerValue"   value="${pollerValue}"
file="tmp.properties"/>

doesn't work. I'm trying to understand why ...

Regards
Patrizio


-----Original Message-----
From: Patrizio.Ferlito@b-source.ch [mailto:Patrizio.Ferlito@b-source.ch]

Sent: Thursday, June 12, 2008 1:42 PM
To: user@ant.apache.org
Subject: RE: how to check that a property exist

/*
Hi Gilbert,

issue ii) solved using macrodef:

  <target name="pollerEventIsSet-if" depends="check-pollerEventIsSet"
if="pollerEventIsSet">
    <macrodef name="propertycopy">
      <attribute name="pollerEvent"/>
        <sequential>
          <property name="pollerValue" value="${@{pollerEvent}}"/>
        </sequential>
    </macrodef>
    <propertycopy pollerEvent="${IPEvent}.pollerManagedServer"/>
    <echo>pollerEventIsSet, pollerValue: ${pollerValue}</echo>
  </target>

output:

pollerEventIsSet-if:
     [echo] pollerEventIsSet, pollerValue: bnk01alm01


Any idea about issue i) ?

*/

issue ii) =
i believe that's solution which is also listed in the ant faq
as far as i remember

issue i)
the problem is you're using antcall which opens a new project scope
means after antcall is through the project scope is gone and you
don't get back anything to the calling target.

therefore tasks like AntCallBack, AntFetch exist in antcontrib ;-)

possible solutions =

very lowlevel = echo value to a file and load it in the calling target

/*
thanks a lot for your hint but I would like to find a way without using
antcontrib...
*/
go with antcontrib - i see you're already using it, as propertycopy
is part of the antcontrib task suite ;-)))  and adapt my snippet to your
needs

or finally - recommended -  use
<script>
check your existing properties etc. ...
set new properties you need for further processing via use of ant api
<script/>

Regards, Gilbert

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


IMPORTANT: 
This e-mail transmission is intended for the named 
addressee(s)only. 
Its contents are private, confidential and protected 
from disclosure and should not be read, copied or
disclosed by any other person. 
If you are not the intended recipient, we kindly ask
you to notify the sender immediately by telephone 
(+41 (0)58 806 50 00), to redirect the message to the 
account "info@b-source.ch" and to delete this e-mail.
E-mail transmissions may be intercepted, altered or 
read by unauthorized persons and may contain viruses.
Therefore, it is recommended that you use regular mail
or courier services for any information intended to be 
confidential. However, by sending us messages through
e-mail, you authorize and instruct us to correspond by 
e-mail in the relevant matter. 
Thank you.


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


RE: how to check that a property exist

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 

-----Original Message-----
From: Patrizio.Ferlito@b-source.ch [mailto:Patrizio.Ferlito@b-source.ch]

Sent: Thursday, June 12, 2008 1:42 PM
To: user@ant.apache.org
Subject: RE: how to check that a property exist

/*
Hi Gilbert,

issue ii) solved using macrodef:

  <target name="pollerEventIsSet-if" depends="check-pollerEventIsSet"
if="pollerEventIsSet">
    <macrodef name="propertycopy">
      <attribute name="pollerEvent"/>
        <sequential>
          <property name="pollerValue" value="${@{pollerEvent}}"/>
        </sequential>
    </macrodef>
    <propertycopy pollerEvent="${IPEvent}.pollerManagedServer"/>
    <echo>pollerEventIsSet, pollerValue: ${pollerValue}</echo>
  </target>

output:

pollerEventIsSet-if:
     [echo] pollerEventIsSet, pollerValue: bnk01alm01


Any idea about issue i) ?

*/

issue ii) =
i believe that's solution which is also listed in the ant faq
as far as i remember

issue i)
the problem is you're using antcall which opens a new project scope
means after antcall is through the project scope is gone and you
don't get back anything to the calling target.

therefore tasks like AntCallBack, AntFetch exist in antcontrib ;-)

possible solutions =

very lowlevel = echo value to a file and load it in the calling target

/*
thanks a lot for your hint but I would like to find a way without using
antcontrib...
*/
go with antcontrib - i see you're already using it, as propertycopy
is part of the antcontrib task suite ;-)))  and adapt my snippet to your
needs

or finally - recommended -  use
<script>
check your existing properties etc. ...
set new properties you need for further processing via use of ant api
<script/>

Regards, Gilbert

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


RE: how to check that a property exist

Posted by Pa...@b-source.ch.
Hi Gilbert,

issue ii) solved using macrodef:

  <target name="pollerEventIsSet-if" depends="check-pollerEventIsSet" if="pollerEventIsSet">
    <macrodef name="propertycopy">
      <attribute name="pollerEvent"/>
        <sequential>
          <property name="pollerValue" value="${@{pollerEvent}}"/>
        </sequential>
    </macrodef>
    <propertycopy pollerEvent="${IPEvent}.pollerManagedServer"/>
    <echo>pollerEventIsSet, pollerValue: ${pollerValue}</echo>
  </target>

output:

pollerEventIsSet-if:
     [echo] pollerEventIsSet, pollerValue: bnk01alm01


Any idea about issue i) ?

Regards
Patrizio



-----Original Message-----
From: Ferlito Patrizio (B-Source - Lugano) 
Sent: giovedì, 12. giugno 2008 13:11
To: 'Ant Users List'
Subject: RE: how to check that a property exist



Hi Gilbert,

thanks a lot for your hint but I would like to find a way without using antcontrib...

After some tries here what I've found but I've two issues:

i) a property defined in a target is not visible in another. How could I set, let's say, a 'global property' ?
ii) a way to get 'your' $${${IPEvent}.pollerManagedServer} as value in a property

My tries ....


<target name="test">
    <antcall target="retrievePollerValue"/>
    <echo>pollerValue        : ${pollerValue}</echo>

  <target name="retrievePollerValue" depends="pollerEventIsSet-if,pollerEventIsSet-else"/>
  <target name="check-pollerEventIsSet">
    <condition property="pollerEventIsSet">
      <isset property="${IPEvent}.pollerManagedServer"/>
    </condition>
  </target>
  <target name="pollerEventIsSet-if" depends="check-pollerEventIsSet" if="pollerEventIsSet">
    <property name="pollerValue" value="$${${IPEvent}.pollerManagedServer}"/>
    <echo>pollerEventIsSet, pollerValue: ${pollerValue}</echo>
  </target>
  <target name="pollerEventIsSet-else" depends="check-pollerEventIsSet" unless="pollerEventIsSet">
    <property name="pollerValue" value="${defaultPollerManagedServer}"/>
    <echo>pollerEventIsNotSet, pollerValue: ${pollerValue}</echo>
  </target>

where

<property name="defaultPollerManagedServer" value="A"/>
<property name="TestEvent.pollerManagedServer" value="B"/>
<property name="DummyEvent.pollerManagedServer" value="C"/>

that works more or less ...

I see

1) with a not defined <something>.pollerManagedServer property: 

1.a) OK: pollerEventIsSet-else is called and 'locally' the value is correct

check-pollerEventIsSet:
pollerEventIsSet-if:
pollerEventIsSet-else:
     [echo] pollerEventIsNotSet, pollerValue: bnk01alm01

1.b) but at 'test' target level the value is not visible

test:
     [echo] pollerValue        : ${pollerValue}

2. with a defined <something>.pollerManagedServer property:

2.a) pollerEventIsSet-if is called

check-pollerEventIsSet:
pollerEventIsSet-if:
     [echo] pollerEventIsSet, pollerValue: ${DummyEvent.pollerManagedServer}
pollerEventIsSet-else:

but as you can see it seems that the

 <property name="pollerValue" value="$${${IPEvent}.pollerManagedServer}"/>

is not the propery way to set the value !!!!

So, as you can see your 'if' is achieved in a different way but I'm not able to solve my i) & ii) issues.

Do you have any idea ?

Regards
Patrizio





-----Original Message-----
From: Rebhan, Gilbert [mailto:Gilbert.Rebhan@huk-coburg.de] 
Sent: mercoledì, 11. giugno 2008 13:45
To: Ant Users List
Subject: RE: how to check that a property exist



 
-----Original Message-----
From: Patrizio.Ferlito@b-source.ch [mailto:Patrizio.Ferlito@b-source.ch]

Sent: Wednesday, June 11, 2008 12:22 PM
To: user@ant.apache.org
Subject: how to check that a property exist

/*
I've a properties file like that:

...
defaultPollerManagedServer=A
TestEvent.pollerManagedServer=B
DummyEvent.pollerManagedServer=C
...

I run my build.xml providing a parameter:

ant -DIPEvent=TestEvent

And I would like to set the value of a property named 'pollerEventValue'
according to these rules:

1) if exists a property like 

    <property name="pollerEvent"
value="${IPEvent}.pollerManagedServer"/>

then the requested value is:

    <property name="pollerEventValue" value="${${pollerEvent}}"/>
?????? but I think the syntax is wrong

2) else take as value

    <property name="pollerEventValue"
value="${defaultPollerManagedServer}"/>

So if I run:

 ant -DIPEvent=TestEvent   the expected value is B
 ant -DIPEvent=other       the expected value is A (the default one)

Could anyone help me to solve that ?
*/


that's a tricky one, i once had a similar problem and discovered, that
it works with the <var> task of antcontrib, so the solution in your case
would be =

ant -DIPEvent=TestEvent -f yourbuild.xml ...

and

<!-- // Taskdefs -->
<!-- Import AntContrib -->
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<!-- Taskdefs // -->

<!-- // Properties -->
<property name="defaultPollerManagedServer" value="A"/>
<property name="TestEvent.pollerManagedServer" value="B"/>
<property name="DummyEvent.pollerManagedServer" value="C"/>
<!-- Properties // -->

<target name="depends"> 
 <if>
  <isset property="${IPEvent}.pollerManagedServer"/>
   <then>
     <var name="pollerEventValue"
value="$${${IPEvent}.pollerManagedServer}"/>
   </then>
   <else>
     <property name="pollerEventValue"
value="${defaultPollerManagedServer}"/>
   </else>
 </if>
</target>

<target name="main" depends="depends">
  <echo>$${pollerEventValue} == ${pollerEventValue}</echo>
</target>
</project>


you need antcontrib =
http://sourceforge.net/project/showfiles.php?group_id=36177


Regards, Gilbert

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


IMPORTANT: 
This e-mail transmission is intended for the named 
addressee(s)only. 
Its contents are private, confidential and protected 
from disclosure and should not be read, copied or
disclosed by any other person. 
If you are not the intended recipient, we kindly ask
you to notify the sender immediately by telephone 
(+41 (0)58 806 50 00), to redirect the message to the 
account "info@b-source.ch" and to delete this e-mail.
E-mail transmissions may be intercepted, altered or 
read by unauthorized persons and may contain viruses.
Therefore, it is recommended that you use regular mail
or courier services for any information intended to be 
confidential. However, by sending us messages through
e-mail, you authorize and instruct us to correspond by 
e-mail in the relevant matter. 
Thank you.


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


RE: how to check that a property exist

Posted by Pa...@b-source.ch.
Hi Gilbert,

thanks a lot for your hint but I would like to find a way without using antcontrib...

After some tries here what I've found but I've two issues:

i) a property defined in a target is not visible in another. How could I set, let's say, a 'global property' ?
ii) a way to get 'your' $${${IPEvent}.pollerManagedServer} as value in a property

My tries ....


<target name="test">
    <antcall target="retrievePollerValue"/>
    <echo>pollerValue        : ${pollerValue}</echo>

  <target name="retrievePollerValue" depends="pollerEventIsSet-if,pollerEventIsSet-else"/>
  <target name="check-pollerEventIsSet">
    <condition property="pollerEventIsSet">
      <isset property="${IPEvent}.pollerManagedServer"/>
    </condition>
  </target>
  <target name="pollerEventIsSet-if" depends="check-pollerEventIsSet" if="pollerEventIsSet">
    <property name="pollerValue" value="$${${IPEvent}.pollerManagedServer}"/>
    <echo>pollerEventIsSet, pollerValue: ${pollerValue}</echo>
  </target>
  <target name="pollerEventIsSet-else" depends="check-pollerEventIsSet" unless="pollerEventIsSet">
    <property name="pollerValue" value="${defaultPollerManagedServer}"/>
    <echo>pollerEventIsNotSet, pollerValue: ${pollerValue}</echo>
  </target>

where

<property name="defaultPollerManagedServer" value="A"/>
<property name="TestEvent.pollerManagedServer" value="B"/>
<property name="DummyEvent.pollerManagedServer" value="C"/>

that works more or less ...

I see

1) with a not defined <something>.pollerManagedServer property: 

1.a) OK: pollerEventIsSet-else is called and 'locally' the value is correct

check-pollerEventIsSet:
pollerEventIsSet-if:
pollerEventIsSet-else:
     [echo] pollerEventIsNotSet, pollerValue: bnk01alm01

1.b) but at 'test' target level the value is not visible

test:
     [echo] pollerValue        : ${pollerValue}

2. with a defined <something>.pollerManagedServer property:

2.a) pollerEventIsSet-if is called

check-pollerEventIsSet:
pollerEventIsSet-if:
     [echo] pollerEventIsSet, pollerValue: ${DummyEvent.pollerManagedServer}
pollerEventIsSet-else:

but as you can see it seems that the

 <property name="pollerValue" value="$${${IPEvent}.pollerManagedServer}"/>

is not the propery way to set the value !!!!

So, as you can see your 'if' is achieved in a different way but I'm not able to solve my i) & ii) issues.

Do you have any idea ?

Regards
Patrizio





-----Original Message-----
From: Rebhan, Gilbert [mailto:Gilbert.Rebhan@huk-coburg.de] 
Sent: mercoledì, 11. giugno 2008 13:45
To: Ant Users List
Subject: RE: how to check that a property exist



 
-----Original Message-----
From: Patrizio.Ferlito@b-source.ch [mailto:Patrizio.Ferlito@b-source.ch]

Sent: Wednesday, June 11, 2008 12:22 PM
To: user@ant.apache.org
Subject: how to check that a property exist

/*
I've a properties file like that:

...
defaultPollerManagedServer=A
TestEvent.pollerManagedServer=B
DummyEvent.pollerManagedServer=C
...

I run my build.xml providing a parameter:

ant -DIPEvent=TestEvent

And I would like to set the value of a property named 'pollerEventValue'
according to these rules:

1) if exists a property like 

    <property name="pollerEvent"
value="${IPEvent}.pollerManagedServer"/>

then the requested value is:

    <property name="pollerEventValue" value="${${pollerEvent}}"/>
?????? but I think the syntax is wrong

2) else take as value

    <property name="pollerEventValue"
value="${defaultPollerManagedServer}"/>

So if I run:

 ant -DIPEvent=TestEvent   the expected value is B
 ant -DIPEvent=other       the expected value is A (the default one)

Could anyone help me to solve that ?
*/


that's a tricky one, i once had a similar problem and discovered, that
it works with the <var> task of antcontrib, so the solution in your case
would be =

ant -DIPEvent=TestEvent -f yourbuild.xml ...

and

<!-- // Taskdefs -->
<!-- Import AntContrib -->
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<!-- Taskdefs // -->

<!-- // Properties -->
<property name="defaultPollerManagedServer" value="A"/>
<property name="TestEvent.pollerManagedServer" value="B"/>
<property name="DummyEvent.pollerManagedServer" value="C"/>
<!-- Properties // -->

<target name="depends"> 
 <if>
  <isset property="${IPEvent}.pollerManagedServer"/>
   <then>
     <var name="pollerEventValue"
value="$${${IPEvent}.pollerManagedServer}"/>
   </then>
   <else>
     <property name="pollerEventValue"
value="${defaultPollerManagedServer}"/>
   </else>
 </if>
</target>

<target name="main" depends="depends">
  <echo>$${pollerEventValue} == ${pollerEventValue}</echo>
</target>
</project>


you need antcontrib =
http://sourceforge.net/project/showfiles.php?group_id=36177


Regards, Gilbert

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


IMPORTANT: 
This e-mail transmission is intended for the named 
addressee(s)only. 
Its contents are private, confidential and protected 
from disclosure and should not be read, copied or
disclosed by any other person. 
If you are not the intended recipient, we kindly ask
you to notify the sender immediately by telephone 
(+41 (0)58 806 50 00), to redirect the message to the 
account "info@b-source.ch" and to delete this e-mail.
E-mail transmissions may be intercepted, altered or 
read by unauthorized persons and may contain viruses.
Therefore, it is recommended that you use regular mail
or courier services for any information intended to be 
confidential. However, by sending us messages through
e-mail, you authorize and instruct us to correspond by 
e-mail in the relevant matter. 
Thank you.


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


RE: how to check that a property exist

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 
-----Original Message-----
From: Patrizio.Ferlito@b-source.ch [mailto:Patrizio.Ferlito@b-source.ch]

Sent: Wednesday, June 11, 2008 12:22 PM
To: user@ant.apache.org
Subject: how to check that a property exist

/*
I've a properties file like that:

...
defaultPollerManagedServer=A
TestEvent.pollerManagedServer=B
DummyEvent.pollerManagedServer=C
...

I run my build.xml providing a parameter:

ant -DIPEvent=TestEvent

And I would like to set the value of a property named 'pollerEventValue'
according to these rules:

1) if exists a property like 

    <property name="pollerEvent"
value="${IPEvent}.pollerManagedServer"/>

then the requested value is:

    <property name="pollerEventValue" value="${${pollerEvent}}"/>
?????? but I think the syntax is wrong

2) else take as value

    <property name="pollerEventValue"
value="${defaultPollerManagedServer}"/>

So if I run:

 ant -DIPEvent=TestEvent   the expected value is B
 ant -DIPEvent=other       the expected value is A (the default one)

Could anyone help me to solve that ?
*/


that's a tricky one, i once had a similar problem and discovered, that
it works with the <var> task of antcontrib, so the solution in your case
would be =

ant -DIPEvent=TestEvent -f yourbuild.xml ...

and

<!-- // Taskdefs -->
<!-- Import AntContrib -->
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<!-- Taskdefs // -->

<!-- // Properties -->
<property name="defaultPollerManagedServer" value="A"/>
<property name="TestEvent.pollerManagedServer" value="B"/>
<property name="DummyEvent.pollerManagedServer" value="C"/>
<!-- Properties // -->

<target name="depends"> 
 <if>
  <isset property="${IPEvent}.pollerManagedServer"/>
   <then>
     <var name="pollerEventValue"
value="$${${IPEvent}.pollerManagedServer}"/>
   </then>
   <else>
     <property name="pollerEventValue"
value="${defaultPollerManagedServer}"/>
   </else>
 </if>
</target>

<target name="main" depends="depends">
  <echo>$${pollerEventValue} == ${pollerEventValue}</echo>
</target>
</project>


you need antcontrib =
http://sourceforge.net/project/showfiles.php?group_id=36177


Regards, Gilbert

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