簡體   English   中英

如何用ant -D鍵傳遞的那一個覆蓋屬性文件的值?

[英]How to overwrite property file value with the one passed with ant -D key?

這是一個屬性文件:

test.url=https:\\url:port
test.path=/home/folder
test.location=Location
test.version=1

以及以下螞蟻任務:

我可以為任務的一次運行傳遞臨時值:

ant -Dtest.path=new_path test_props

我如何使用-D鍵通過一個覆蓋test.path的值? 為了順序,在相同的啟動之后,test.path的值將變為我上面通過的值?

以下變體不起作用:

<entry key="test.path" value="${test.path}"/>

要么

<propertycopy name="test.path" from="${test_path}"/>

如果要永久更改文件,可以使用task

我將執行以下操作:

創建一個示例屬性文件,例如default.properties.sample。

創建一個接收給定-D屬性的目標,然后,如果被告知,則對文件default.properties.sample進行替換,將其保存到default.properties文件中。 default.properties.sample將具有以下行:

test.url=https:\\url:port
test.path=@test_path@
test.location=Location
test.version=1

該操作將使用-D參數告知的屬性的實際值替換@ test_path @標記,然后將結果文件另存為default.properties。 就像是:

<copy file="default.properties.sample" toFile="default.properties" />
<replace file="default.properties" token="@test_path@" value="${test.path}" />

需要進行一些調整,例如:僅在通知-D參數時才替換屬性,否則每次都將替換文件。

路徑等也應根據您的需求進行調整。


我已經測試了以下方案,並且對我有用:

我創建了兩個文件:build.xml和default.properties.sample。 其內容如下:

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="BuildTest" default="showProperties" basedir=".">
    <property file="default.properties"/>

    <target name="showProperties">
        <echo message="test.property=${test.property}"/>
    </target>

    <target name="replace">
        <fail unless="new.test.property" message="Property new.test.property should be informed via -D parameter"/>
        <copy file="default.properties.sample" toFile="default.properties"/>
        <replace file="default.properties" token="@test_property@" value="${new.test.property}"/>
    </target>
</project>

default.properties.sample:

test.property=@test_property@

他們運行以下測試:

默認運行:

C:\Filipe\Projects\BuildTest>ant
Buildfile: C:\Filipe\Projects\BuildTest\build.xml

showProperties:
     [echo] test.property=${test.property}

BUILD SUCCESSFUL
Total time: 0 seconds

錯誤控制:

C:\Filipe\Projects\BuildTest>ant replace
Buildfile: C:\Filipe\Projects\BuildTest\build.xml

replace:

BUILD FAILED
C:\Filipe\Projects\BuildTest\build.xml:10: Property new.test.property should be      informed via -D parameter
Total time: 0 seconds

替換財產:

C:\Filipe\Projects\BuildTest>ant replace -Dnew.test.property="This is a New Value"
Buildfile: C:\Filipe\Projects\BuildTest\build.xml

replace:
     [copy] Copying 1 file to C:\Filipe\Projects\BuildTest

BUILD SUCCESSFUL
Total time: 0 seconds

替換后的屬性文件:

C:\Filipe\Projects\BuildTest>type default.properties
test.property=This is a New Value

在隨后的運行中,將顯示test.property的新值:

C:\Filipe\Projects\BuildTest>ant
Buildfile: C:\Filipe\Projects\BuildTest\build.xml

showProperties:
     [echo] test.property=This is a New Value

BUILD SUCCESSFUL
Total time: 0 seconds

我認為這就是您要尋找的。

暫無
暫無

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

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