Is it possible to alter Ant properties after they've been initialized?

Discussion in 'Java' started by vturner, Sep 20, 2004.

  1. vturner

    vturner Guest

    If I define an Ant property and assign it a value, is it possible to change
    it later. Something like:

    <property name="abc" value="hello"/>
    <alter property="abc" newvalue="goodbye"/>
    <echo message="${abc}"/> # will print "hello", but want "goodbye"

    In general, treat a propertly like a variable. Or are ant properties like
    "variables" in XSL -- namely they're really constants?

    I know I can write a Taskdef handler (Java program) that does something
    like:

    this.getProject().setProperty("abc", "goodbye");

    However, it seems like there ought to be a built in feature of Ant that
    allows this. The closest I can find is the <copyproperty> tag from
    ant-conrib. This allows you to copy the value of a property into another,
    but only if the "new" property has never been defined. You cannot use it
    to override a previously defined property.

    My basic problem is I'm calling a sub-project from my main project and I
    want to be able to return properties from the sub-project back to the main
    project:

    <ant antfile="build_child.xml" dir="." target="do-something"/>
    ....access properties from build_child.xml here

    From my experimentation, it appears any properties defined in the
    sub-project are not available to the parent project. Properties in the
    parent projct *are* available to the sub-project, but since they're already
    defined I can't alter them with new values.

    So I guess my real question is: how do you pass properties from a subproject
    back to the calling project?
     
    vturner, Sep 20, 2004
    #1
    1. Advertising

  2. vturner

    vturner Guest

    vturner wrote:

    > If I define an Ant property and assign it a value, is it possible to
    > change
    > it later. Something like:
    >
    > <property name="abc" value="hello"/>
    > <alter property="abc" newvalue="goodbye"/>
    > <echo message="${abc}"/> # will print "hello", but want "goodbye"
    >
    > In general, treat a propertly like a variable. Or are ant properties like
    > "variables" in XSL -- namely they're really constants?
    >
    > I know I can write a Taskdef handler (Java program) that does something
    > like:
    >
    > this.getProject().setProperty("abc", "goodbye");
    >
    > However, it seems like there ought to be a built in feature of Ant that
    > allows this. The closest I can find is the <copyproperty> tag from
    > ant-conrib. This allows you to copy the value of a property into another,
    > but only if the "new" property has never been defined. You cannot use it
    > to override a previously defined property.
    >
    > My basic problem is I'm calling a sub-project from my main project and I
    > want to be able to return properties from the sub-project back to the main
    > project:
    >
    > <ant antfile="build_child.xml" dir="." target="do-something"/>
    > ...access properties from build_child.xml here
    >
    > From my experimentation, it appears any properties defined in the
    > sub-project are not available to the parent project. Properties in the
    > parent projct *are* available to the sub-project, but since they're
    > already defined I can't alter them with new values.
    >
    > So I guess my real question is: how do you pass properties from a
    > subproject back to the calling project?


    While my original questions still remain, I was able to achieve my
    objectives by simply importing the remote target using the the <import>
    statement. When you do this, the imported targets are a part of the
    importing project. Therefore, they're in the same namespace. Which simply
    means any properties I set with the imported target are availabe to the
    importing target. Case closed, although I'm still curious if it's possible
    to alter Ant variables.
     
    vturner, Sep 21, 2004
    #2
    1. Advertising

  3. vturner

    vturner Guest

    vturner wrote:

    > While my original questions still remain, I was able to achieve my
    > objectives by simply importing the remote target using the the <import>
    > statement. When you do this, the imported targets are a part of the
    > importing project. Therefore, they're in the same namespace. Which
    > simply means any properties I set with the imported target are availabe to
    > the
    > importing target. Case closed, although I'm still curious if it's
    > possible to alter Ant variables.


    For posterity, I suppose I should document that this in fact did *not* work.
    I jumped the gun and deluded myself into thinking it worked. In the end I
    finally used a macrodef to fix the problem.

    In the remote file (to be shared among several build files) I have:

    <macrodef name="create-work-files">
    <attribute name="service"/>
    <sequential>
    <tstamp>
    <format property="DATE-TIME" pattern="yyyyMMdd_kkmmss"
    unit="second"/>
    </tstamp>
    <property name="work-file-a"
    value="/tmp/build/ups_@{service}_work_${DATE-TIME}_${req.CustomerRefNo}_a"/>
    <property name="work-file-b"
    value="/tmp/build/ups_@{service}_work_${DATE-TIME}_${req.CustomerRefNo}_b"/>
    <property name="work-file-c"
    value="/tmp/build/ups_@{service}_work_${DATE-TIME}_${req.CustomerRefNo}_c"/>
    </sequential>
    </macrodef>

    In the importing file I have:

    <import file="../../common/create_work_files.xml" />
    ....
    <create-work-files service="rates"/> # call macro here

    Now, in the importing file I can finally reference the properties defined in
    the remote file.
     
    vturner, Sep 21, 2004
    #3
  4. vturner

    baltrunas Guest

    I had simple problem: to write "buildAll" task which would use same
    build.xml with different properties.

    In project there is defined <property name="series_code" value="Series30" />

    <target name="buildAll">
    <antcall target="build" inheritall="false">
    <param name="series_code" value="Series30"/>
    </antcall>
    <antcall target="build" inheritall="false">
    <param name="series_code" value="Series40"/>
    </antcall>
    </target>

    I should mention that there must be set inheritall="false". In other way
    there will be used earlier defined property.

    maybe this will be useful for someone.

    --
    Linas
     
    baltrunas, Sep 21, 2004
    #4
  5. "vturner" <> wrote in message
    news:hGI3d.64$...
    > If I define an Ant property and assign it a value, is it possible to
    > change
    > it later.


    No; property values, once set, cannot be changed. Attempts to do so will
    fail silently. Where in a language like Java, one might write:

    int i = 5; // set to default value
    if (unusualConditionIsTrue())
    i = 10; // set to other value

    In ant, you need to set the unusual value first, something like:

    <target name="setProp" if="unusual.condition">
    <property name="i" value="10"/>
    </target>

    <target name="usual" depends="setProp">
    <property name="i" value="5/>
    </target>

    Note that the setting of "prop" to 5 will fail when "unusual.condition " is
    true, since "prop" will already have been set to 10.
     
    Mike Schilling, Sep 21, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MattB
    Replies:
    3
    Views:
    54,696
    nada111
    Oct 4, 2007
  2. CJM
    Replies:
    2
    Views:
    4,507
  3. Noozer
    Replies:
    3
    Views:
    6,822
    Jeffrey Silverman
    Dec 6, 2004
  4. Marc Heiler
    Replies:
    1
    Views:
    137
    Stefan Lang
    Jan 26, 2008
  5. -berlin.de
    Replies:
    0
    Views:
    144
    -berlin.de
    Jun 30, 2007
Loading...

Share This Page