Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何用ant-D密钥传递的属性文件值覆盖属性文件值?_Java_Ant - Fatal编程技术网

Java 如何用ant-D密钥传递的属性文件值覆盖属性文件值?

Java 如何用ant-D密钥传递的属性文件值覆盖属性文件值?,java,ant,Java,Ant,这是一个属性文件: test.url=https:\\url:port test.path=/home/folder test.location=Location test.version=1 以及以下ant任务: 我可以为一次任务运行传递临时值: ant -Dtest.path=new_path test_props 如何使用-D键将test.path值覆盖为通过的值?按照顺序,在相同的启动之后,test.path的值将更改为上面我通过的值 以下变体不起作用: <entry ke

这是一个属性文件:

test.url=https:\\url:port
test.path=/home/folder
test.location=Location
test.version=1
以及以下ant任务:

我可以为一次任务运行传递临时值:

ant -Dtest.path=new_path test_props
如何使用-D键将test.path值覆盖为通过的值?按照顺序,在相同的启动之后,test.path的值将更改为上面我通过的值

以下变体不起作用:

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


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

我会做以下几件事:

创建一个示例属性文件,如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
该操作将用属性的实际值替换@test_path@token,如-D参数中所述,然后将结果文件保存为default.properties。比如:

<copy file="default.properties.sample" toFile="default.properties" />
<replace file="default.properties" token="@test_path@" value="${test.path}" />
并运行以下测试:

默认运行:

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}
是的,它们是不可变的,但是使用propertyfile任务可以更改属性文件的条目。我想我也可以这样做,但是读取ant args。您可以使用${test.path}并随时用-D覆盖它。在本例中,您可以拥有第二个属性new.test.path,当通过-Dnew.test.path=通知时,它将通过任务永久替换文件中的${test.path},如我在回答中所述。它不起作用。。。或者我做错了什么:
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
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