如果你想永久改变一个文件,你可以使用 task。
我会做到以下几点:
创建示例属性文件,像default.properties.sample。
创建一个接收给定-D属性的目标,然后,如果已通知它,则执行文件default.properties.sample中的替换操作,将其保存到default.properties文件中。该default.properties.sample会有这些行:
test.url=https:\\url:port
[email protected][email protected]
test.location=Location
test.version=1
操作将与该财产的实际价值取代@ test_path @令牌,如-D参数通知,然后保存生成的文件默认的.properties。要进行
<copy file="default.properties.sample" toFile="default.properties" />
<replace file="default.properties" token="@[email protected]" 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="@[email protected]" value="${new.test.property}"/>
</target>
</project>
default.properties。示例:
[email protected][email protected]
而且它们运行以下测试:
默认运行:
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
我认为这是你在找什么。
您无法覆盖这些属性..它们是不可变的,但您使用-D选项传递的属性将优先。你只需要在你想要的地方使用它,比如'$ {test.path}' – 2012-08-16 13:50:05
是的,它们是不可变的,但是使用propertyfile任务可以改变属性文件的条目。我以为我可以用同样的方式做到这一点,但是阅读蚂蚁的参数。 – Dragon 2012-08-16 13:53:46
如果您喜欢,可以使用$ {test.path}并用-D覆盖它。在这种情况下,您可以使用第二个属性new.test.path,当通过-Dnew.test.path =通知时,将通过任务永久替换该文件中的$ {test.path},如我的回答。 –
2012-08-16 13:57:03