简体   繁体   English

如何用ant -D键传递的那一个覆盖属性文件的值?

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

Here is a property file: 这是一个属性文件:

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

And the following ant task: 以及以下蚂蚁任务:

I can pass temporary value for one run of a task: 我可以为任务的一次运行传递临时值:

ant -Dtest.path=new_path test_props

How can I overwrite test.path value with one I pass using -D key? 我如何使用-D键通过一个覆盖test.path的值? In order, after the same launch, the value of test.path would change to one I pass above? 为了顺序,在相同的启动之后,test.path的值将变为我上面通过的值?

The following variants don't work: 以下变体不起作用:

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

or 要么

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

If you want to permanently change a file, you could use the task . 如果要永久更改文件,可以使用task

I'd do the following: 我将执行以下操作:

Create a sample property file, like default.properties.sample. 创建一个示例属性文件,例如default.properties.sample。

Create a target that receives the given -D property, then, if it's been informed, does a replace on file default.properties.sample saving it into a default.properties file. 创建一个接收给定-D属性的目标,然后,如果被告知,则对文件default.properties.sample进行替换,将其保存到default.properties文件中。 The default.properties.sample would have these lines: default.properties.sample将具有以下行:

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

The action would replace the @test_path@ token with the real value of the property, as informed in the -D parameter, then save the resulting file as default.properties. 该操作将使用-D参数告知的属性的实际值替换@ test_path @标记,然后将结果文件另存为default.properties。 Something like: 就像是:

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

Some adjustments need to be made, like: only replace the property if the -D parameter is informed, or else the file would be replaced every time. 需要进行一些调整,例如:仅在通知-D参数时才替换属性,否则每次都将替换文件。

Paths and like should also be adjusted to your needs. 路径等也应根据您的需求进行调整。


I've tested the following scenario and it worked for me: 我已经测试了以下方案,并且对我有用:

I've created two files: a build.xml and a default.properties.sample. 我创建了两个文件:build.xml和default.properties.sample。 Their contents is as follows: 其内容如下:

build.xml 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: default.properties.sample:

test.property=@test_property@

And they run to the following tests: 他们运行以下测试:

Default run: 默认运行:

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

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

BUILD SUCCESSFUL
Total time: 0 seconds

Error control: 错误控制:

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

Replace of property: 替换财产:

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

Property file after replacement: 替换后的属性文件:

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

And in a subsequent runs the new value of the test.property is present: 在随后的运行中,将显示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

I think that is what you're looking for. 我认为这就是您要寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM