Hey guys, I am using Apache Ant and I am wondering how would I edit a variable in a file (e.g. database host, username, password) and pass an argument (e.g. for a subversion commit message)
Basically I want the Apache Ant task to change my database variables and then execute the subversion commit command on my current directory.
This is on Windows 7.
Thanks
— It’s hard to know what you are asking for in detail, so the answer changes —
If you want to copy a new value into a portion of a file (instead of allowing a user to set a variable in ant), then you use ant’s
copytask coupled with afilterset.The copy will produce the output file from the input file, and the filterset will indicate which “tokens” need to be replaced with some value. Often this value is something that was stored in a property, but it could be composed by other means.
copies all files from
srctobuild/srcreplacing@BUILD_NUMBER@with the value of the propertybuild.number.— Original post follows —
Create a properties file, then reference it from the build.xml.
An example of a properties file
database.propertiesAn example of referencing the file from a
build.xmlfileThis way you can isolate your configuration changes to a specific file. Ant doesn’t have variables, it has properties which are “set once”, so to provide default values, you need to do something like
to ensure that the database.user is always set. If a database user property is defined in the
database.propertiesfile, then the second attempt to assign the property will be ignored. If a user removesdatabase.user=bobfrom the properties file, then the second attempt to set it in thebuild.xmlfile will succeed.In the rare event that you actually want a person to type in some value (perhaps a password), use the
inputtask; however, it again can only set a property. Ant doesn’t have variables.