Reputation: 2481
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? In order, after the same launch, the value of test.path would change to one I pass above?
The following variants don't work:
<entry key="test.path" value="${test.path}"/>
or
<propertycopy name="test.path" from="${test_path}"/>
Upvotes: 0
Views: 3179
Reputation: 2540
If you want to permanently change a file, you could use the task.
I'd do the following:
Create a sample property file, like 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. The default.properties.sample would have these lines:
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. 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.
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. Their contents is as follows:
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@
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:
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.
Upvotes: 1