簡體   English   中英

Ant:我可以從xml文件中獲取特定屬性嗎?

[英]Ant: Can I get a specific property from a xml file?

我用ant來安裝服務器進行測試。 在屬性文件中,我有一個數據庫參數列表。 問題是我需要在更改數據庫時更改5-6個參數。 其中大多數取決於數據庫名稱。 所以我認為我創建了一個包含我們使用的所有不同數據庫的xml文件,只需在屬性文件中輸入名稱即可。 然后,當構建文件運行時,它將獲取所用數據庫的正確屬性。 我創建了一個像這樣的xml文件:

<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>

我在我的構建文件中調用該文件:

<xmlproperty file="databases.xml"  />

因此,當我的屬性文件聲明要使用“mssql_1”時,我希望加載匹配的屬性。

但無論我如何嘗試選擇正確的數據,我都會嘗試使用所有端口:“1433,1521”

<echo message="${databases.database.port}">

我已經搜索了不同的方法來做到這一點,但我找不到辦法做到這一點。 但它似乎應該是一件容易的事情....

您的屬性文件有兩個具有相同值的實體。 <xmlproperty>任務讀取整個XML文件,然后使用實體設置屬性名稱。 如果您碰巧有兩個具有相同屬性的實體,則該特定屬性的值將同時存在。

大多數人所做的是為每個數據庫都有一個單獨的屬性文件,然后使用帶有file參數的<property>任務來讀取該特定屬性文件。 所以你應該有兩個屬性文件:

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

然后,在build.xml文件中,根據數據庫讀入一個且只有一個屬性文件:

<property name="database"  value="mssql"/>   <!-- Default database -->
<property name="database.file   value="database.${database}.properties"/>

<fail message="No such database file &quot;${database.file}&quot;">
    <condition>
        <not>
            <available file="${database.file} type="file"/>
        </not>
    </condition>
 </fail>

<property file="${database.file}"/>

當有人運行Ant時,他們可以使用-D參數來設置數據庫:

$ ant -Ddatabase=oracle

這將覆蓋build.xml文件中的database屬性並使用Oracle數據庫。 如果未指定數據庫,則將使用默認數據庫(MS SQL)。

並且,如果另一個屬性文件指定屬性文件名,這也將起作用:

<property file="build.properties"> <!-- Contains database name -->
<property name="database.file"   value="database.${database}.properties"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM