[英]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.