Ant: Passing attributes to Build.xml

Critter on a previous post asked "if you can pass in attributes to the build.xml file?" From the Terminal this is as easy as adding another argument "-D".

-D<property>=<value>
This will update a property in you build.xml file with the value you passed in

For example you would use something similar to


ant -buildfile ~/desktop/build.xml -DechoMsg=NewEchoMessage

Putting this into practice here is a sample build.xml which I am going to save to my Desktop


<?xml version="1.0" encoding="UTF-8"?>
<project name="myProject" default="runEcho" basedir=".">

    <property name="echoMsg" value="I am the original message" />

<target name="runEcho">
<!-- Simply echo's a message to the screen -->
<echo message="${echoMsg}"/>
</target>
</project>

From the Terminal no run


ant -buildfile ~/desktop/build.xml -DechoMsg='I overwrite the echo message'

In your terminal window you should see something like


Buildfile: [PATH TO BUILD FILE]build.xml

runEcho:
[echo] I overwrite the echo message

BUILD SUCCESSFUL
Total time: 0 seconds

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Critter's Gravatar Nice one mate.

I've taken the old Model-Glue Ant script you had and modified it a bit.. I've downloaded the latest and placed it on my computer.. made these modification: @modelglueapplicationtemplate@ to all the necessary files..

so now I can call my build.xml and pass in the directory name where I want M-G to be built..
# Posted By Critter | 6/29/07 12:00 PM
Nathan Mische's Gravatar I also use the Ant input task with the addproperty attribute quite a bit. (http://ant.apache.org/manual/CoreTasks/input.html) If you use Eclipse to run your build files you get a nice prompt box to enter your property value.
# Posted By Nathan Mische | 6/29/07 12:52 PM
Rob Wilkerson's Gravatar My preference has always been to create a build.properties file and include that in my build.xml file. I don't have a sample handy or I'd include some syntax, but I tend to use this approach for all of my deployment scripts .
# Posted By Rob Wilkerson | 7/6/07 4:13 PM
BlogCFC was created by Raymond Camden / Contact Blog Owner / mptooling.com / spicemerchants-portsmouth.co.uk / ipicture.it