Discussion:
Set a default value to property.
ritchie
2010-11-30 19:04:17 UTC
Permalink
My ant script takes a value for a property at runtime(-Denv=xxx), if the
argument is not passed i want the value to be set as a default arbitary
value. How to accomplish this?
--
View this message in context: http://ant.1045680.n5.nabble.com/Set-a-default-value-to-property-tp3286574p3286574.html
Sent from the Ant - Users mailing list archive at Nabble.com.
Scot P. Floess
2010-11-30 19:11:45 UTC
Permalink
Here is something I like to use... I macrodef'd it out so I can call it
for many properties that require default values...

<macrodef name="default">
<attribute name="property"/>
<attribute name="default"/>
<attribute name="description" default=""/>

<sequential>
<condition property="@{property}" value="@{default}">
<not>
<isset property="@{name}"/>
</not>
</condition>
</sequential>
</macrodef>

This works with stock Ant (meaning you don't need Ant-contrib or any third
party libraries)...

To use:

<default property="SomeProperty" default="Some Default Value"/>
Post by ritchie
My ant script takes a value for a property at runtime(-Denv=xxx), if the
argument is not passed i want the value to be set as a default arbitary
value. How to accomplish this?
--
Scot P. Floess


RHCT (Certificate Number 605010084735240)

Chief Architect FlossWare http://sourceforge.net/projects/flossware
http://flossware.sourceforge.net
https://github.com/organizations/FlossWare

Chief Architect JPlate http://sourceforge.net/projects/jplate
Chief Architect JavaPIM http://sourceforge.net/projects/javapim
Chief Architect Keros http://sourceforge.net/projects/keros
Scot P. Floess
2010-11-30 19:20:45 UTC
Permalink
Clearly my snippet is wrong:

<macrodef name="default">
<attribute name="property"/>
<attribute name="default"/>
<attribute name="description" default=""/>

<sequential>
<condition property="@{property}" value="@{default}">
<not>
<isset property="@{property}"/>
</not>
</condition>
</sequential>
</macrodef>

----- Call to <isset> was using the wrong attribute
Here is something I like to use... I macrodef'd it out so I can call it for
many properties that require default values...
<macrodef name="default">
<attribute name="property"/>
<attribute name="default"/>
<attribute name="description" default=""/>
<sequential>
<not>
</not>
</condition>
</sequential>
</macrodef>
This works with stock Ant (meaning you don't need Ant-contrib or any third
party libraries)...
<default property="SomeProperty" default="Some Default Value"/>
Post by ritchie
My ant script takes a value for a property at runtime(-Denv=xxx), if the
argument is not passed i want the value to be set as a default arbitary
value. How to accomplish this?
--
Scot P. Floess


RHCT (Certificate Number 605010084735240)

Chief Architect FlossWare http://sourceforge.net/projects/flossware
http://flossware.sourceforge.net
https://github.com/organizations/FlossWare

Chief Architect JPlate http://sourceforge.net/projects/jplate
Chief Architect JavaPIM http://sourceforge.net/projects/javapim
Chief Architect Keros http://sourceforge.net/projects/keros
Alexander Enrique Urieles Nieto
2010-11-30 19:24:53 UTC
Permalink
What may it be wrong about simply using:

<project name="Foo" default="foo" basedir=".">
<property name="bar" value="default"/>
<target name="foo" description="foo">
<echo message="${bar}" />
</target>
</project>

Since properties are immutable and if it was set in the command line
call it won't change?

Best regards,

Alexander.
   <macrodef name="default">
       <attribute name="property"/>
       <attribute name="default"/>
       <attribute name="description" default=""/>
       <sequential>
               <not>
               </not>
           </condition>
       </sequential>
   </macrodef>
----- Call to <isset> was using the wrong attribute
Here is something I like to use...  I macrodef'd it out so I can call it
for many properties that require default values...
   <macrodef name="default">
       <attribute name="property"/>
       <attribute name="default"/>
       <attribute name="description" default=""/>
       <sequential>
               <not>
               </not>
           </condition>
       </sequential>
   </macrodef>
This works with stock Ant (meaning you don't need Ant-contrib or any third
party libraries)...
<default property="SomeProperty" default="Some Default Value"/>
 My ant script takes a value for a property at runtime(-Denv=xxx), if the
 argument is not passed i want the value to be set as a default arbitary
 value. How to accomplish this?
--
Scot P. Floess
RHCT  (Certificate Number 605010084735240)
Chief Architect FlossWare  http://sourceforge.net/projects/flossware
                          http://flossware.sourceforge.net
                          https://github.com/organizations/FlossWare
Chief Architect JPlate     http://sourceforge.net/projects/jplate
Chief Architect JavaPIM    http://sourceforge.net/projects/javapim
Chief Architect Keros      http://sourceforge.net/projects/keros
---------------------------------------------------------------------
Niklas Matthies
2010-11-30 19:25:37 UTC
Permalink
How is this different from just

<property name="SomeProperty" value="Some Default Value"/>

?

-- Niklas Matthies
Post by Scot P. Floess
Here is something I like to use... I macrodef'd it out so I can call it
for many properties that require default values...
<macrodef name="default">
<attribute name="property"/>
<attribute name="default"/>
<attribute name="description" default=""/>
<sequential>
<not>
</not>
</condition>
</sequential>
</macrodef>
This works with stock Ant (meaning you don't need Ant-contrib or any
third party libraries)...
<default property="SomeProperty" default="Some Default Value"/>
Post by ritchie
My ant script takes a value for a property at runtime(-Denv=xxx), if the
argument is not passed i want the value to be set as a default arbitary
value. How to accomplish this?
--
Scot P. Floess
RHCT (Certificate Number 605010084735240)
Chief Architect FlossWare http://sourceforge.net/projects/flossware
http://flossware.sourceforge.net
https://github.com/organizations/FlossWare
Chief Architect JPlate http://sourceforge.net/projects/jplate
Chief Architect JavaPIM http://sourceforge.net/projects/javapim
Chief Architect Keros http://sourceforge.net/projects/keros
---------------------------------------------------------------------
Scot P. Floess
2010-11-30 19:30:53 UTC
Permalink
Actually, nothing at all... I swear at one time I thought a warning was
emitted when doing that... It was at that time I started using the
enclosed macrodef... Perhaps I am just remembering wrong...
Post by Niklas Matthies
How is this different from just
<property name="SomeProperty" value="Some Default Value"/>
?
-- Niklas Matthies
Post by Scot P. Floess
Here is something I like to use... I macrodef'd it out so I can call it
for many properties that require default values...
<macrodef name="default">
<attribute name="property"/>
<attribute name="default"/>
<attribute name="description" default=""/>
<sequential>
<not>
</not>
</condition>
</sequential>
</macrodef>
This works with stock Ant (meaning you don't need Ant-contrib or any
third party libraries)...
<default property="SomeProperty" default="Some Default Value"/>
Post by ritchie
My ant script takes a value for a property at runtime(-Denv=xxx), if the
argument is not passed i want the value to be set as a default arbitary
value. How to accomplish this?
--
Scot P. Floess
RHCT (Certificate Number 605010084735240)
Chief Architect FlossWare http://sourceforge.net/projects/flossware
http://flossware.sourceforge.net
https://github.com/organizations/FlossWare
Chief Architect JPlate http://sourceforge.net/projects/jplate
Chief Architect JavaPIM http://sourceforge.net/projects/javapim
Chief Architect Keros http://sourceforge.net/projects/keros
---------------------------------------------------------------------
---------------------------------------------------------------------
--
Scot P. Floess


RHCT (Certificate Number 605010084735240)

Chief Architect FlossWare http://sourceforge.net/projects/flossware
http://flossware.sourceforge.net
https://github.com/organizations/FlossWare

Chief Architect JPlate http://sourceforge.net/projects/jplate
Chief Architect JavaPIM http://sourceforge.net/projects/javapim
Chief Architect Keros http://sourceforge.net/projects/keros
Niklas Matthies
2010-11-30 20:00:06 UTC
Permalink
I don't think this (emitting a warning) was ever the case. Setting and
overriding defaults this way is one of the basic techniques in Ant. It
should probably be made more explicit in the manual.

-- Niklas Matthies
Post by Scot P. Floess
Actually, nothing at all... I swear at one time I thought a warning was
emitted when doing that... It was at that time I started using the
enclosed macrodef... Perhaps I am just remembering wrong...
Post by Niklas Matthies
How is this different from just
<property name="SomeProperty" value="Some Default Value"/>
?
-- Niklas Matthies
Post by Scot P. Floess
Here is something I like to use... I macrodef'd it out so I can call it
for many properties that require default values...
<macrodef name="default">
<attribute name="property"/>
<attribute name="default"/>
<attribute name="description" default=""/>
<sequential>
<not>
</not>
</condition>
</sequential>
</macrodef>
This works with stock Ant (meaning you don't need Ant-contrib or any
third party libraries)...
<default property="SomeProperty" default="Some Default Value"/>
Post by ritchie
My ant script takes a value for a property at runtime(-Denv=xxx), if the
argument is not passed i want the value to be set as a default arbitary
value. How to accomplish this?
Scot P. Floess
2010-11-30 20:05:47 UTC
Permalink
Honestly, its quite possible I am remembering wrong...

I'm looking over some of my old code...it looks like I wanted to emit a
warning if overriding - its me...again ;)
Post by Niklas Matthies
I don't think this (emitting a warning) was ever the case. Setting and
overriding defaults this way is one of the basic techniques in Ant. It
should probably be made more explicit in the manual.
-- Niklas Matthies
Post by Scot P. Floess
Actually, nothing at all... I swear at one time I thought a warning was
emitted when doing that... It was at that time I started using the
enclosed macrodef... Perhaps I am just remembering wrong...
Post by Niklas Matthies
How is this different from just
<property name="SomeProperty" value="Some Default Value"/>
?
-- Niklas Matthies
Post by Scot P. Floess
Here is something I like to use... I macrodef'd it out so I can call it
for many properties that require default values...
<macrodef name="default">
<attribute name="property"/>
<attribute name="default"/>
<attribute name="description" default=""/>
<sequential>
<not>
</not>
</condition>
</sequential>
</macrodef>
This works with stock Ant (meaning you don't need Ant-contrib or any
third party libraries)...
<default property="SomeProperty" default="Some Default Value"/>
Post by ritchie
My ant script takes a value for a property at runtime(-Denv=xxx), if the
argument is not passed i want the value to be set as a default arbitary
value. How to accomplish this?
---------------------------------------------------------------------
--
Scot P. Floess


RHCT (Certificate Number 605010084735240)

Chief Architect FlossWare http://sourceforge.net/projects/flossware
http://flossware.sourceforge.net
https://github.com/organizations/FlossWare

Chief Architect JPlate http://sourceforge.net/projects/jplate
Chief Architect JavaPIM http://sourceforge.net/projects/javapim
Chief Architect Keros http://sourceforge.net/projects/keros
ritchie
2010-11-30 21:04:51 UTC
Permalink
Thanks all for your replies, i tried some thing like this and it worked.

<property name = "version" value="23"/>
...
..
<replacefilter token="${XYZ}" value="${version}"/>

and when i ran the command ant -Dversion=25 it worked. Actually my ant task
is replacing a regular expression with the version value. When i dont pass
the arguments -Dversion=xxx it sets the token value as 23.
--
View this message in context: http://ant.1045680.n5.nabble.com/Set-a-default-value-to-property-tp3286574p3286768.html
Sent from the Ant - Users mailing list archive at Nabble.com.
David Weintraub
2010-11-30 19:44:36 UTC
Permalink
Post by ritchie
My ant script takes a value for a property at runtime(-Denv=xxx), if the
argument is not passed i want the value to be set as a default arbitary
value. How to accomplish this?
What do you mean "arbitrary" value? Do you mean some value that you
chose, or it should chose some value on its own, either from a list or
at random?

If you simply want a default property, just set a property in your build.xml.

There's a hierarchy of where property values are set. If you include a
properties file, the values there override whatever values you set in
the build.xml itself, and values from the command line overrided
values set in both the build.xml and property files.

I do this all the time:

<!-- Will be set if user didn't specify -Denv=xxx from the command line -->
<property name="env" value="some_value"/>
--
David Weintraub
***@gmail.com
Loading...