Ant: Can I get a specific property from a xml file?
I use ant to install servers for test. In the properties file I have a list of parameters for the database. The problem is that I need to change 5-6 parameters when a database is changed. Most of these are depending on the database name. So I thoug开发者_运维技巧ht that I create a xml file with all the different databases we use and just enter the name in the properties file. Then when the build file is running it gets the correct properties for the database used. I created a xml-file like this:
<databases>
<database>
<server>mssql_1</server>
<port>1433</port>
<sid_instance>foobar</sid_instance>
<path></path>
<hostuser>sa</hostuser>
<hostpwd>password</hostpwd>
</database>
<database>
<server>oracle_1</server>
<port>1521</port>
<sid_instance>foobar</sid_instance>
<path>C:\\oracle\\oradata\\foobar</path>
<hostuser>system</hostuser>
<hostpwd>password</hostpwd>
</database>
</databases>
I call the file in my build file with:
<xmlproperty file="databases.xml" />
So when my property file states that "mssql_1" are to be used, I want the matching properties loaded.
But no matter how I try to select the correct data, I get all ports like: "1433, 1521" when I try with
<echo message="${databases.database.port}">
I have searched for different ways to do this but I can't find a way to do this. But it seems like it should be an easy thing to do....
Your property file has two entities with the same value. The <xmlproperty>
task reads through the entire XML file, and then uses the entities to set the property name. If you happen to have two entities with the same property, the value of that particular property will be both of them together.
What most people do is have a separate property file for each database, then use the <property>
task with the file
parameter to read in that particular property file. So you should have two property files:
database.mssql.properties
server=mssql_1
port=1433
sid_instance=foobar
path=
hostuser=sa
hostpwd=password
database.oracle.properties
server=oracle_1
port=1521
sid_instance=foobar
path=C:/oracle/oradata/foobar
hostuser=ssytem
hostpwd=password
Then, in your build.xml
file, you read in one and only one of the properties file based upon the database:
<property name="database" value="mssql"/> <!-- Default database -->
<property name="database.file value="database.${database}.properties"/>
<fail message="No such database file "${database.file}"">
<condition>
<not>
<available file="${database.file} type="file"/>
</not>
</condition>
</fail>
<property file="${database.file}"/>
When someone runs Ant, they can use the -D
parameter to set the database:
$ ant -Ddatabase=oracle
This will override the database
property inside the build.xml
file and use the Oracle database. If you don't specify a database, it will use the default database (the MS SQL one).
And, this will also work if another property file specifies the property file name:
<property file="build.properties"> <!-- Contains database name -->
<property name="database.file" value="database.${database}.properties"/>
精彩评论