Reputation: 21
Is there an elegant way of determining a value for a property? For example, I'd like to allow a property to be read from a file, but also to be grabbed from an environment variable. If the environment variable is present, I'd like to prioritize it over the file's value.
Here's what I have (with the example property deploy.destination
):
<property environment="env" />
<if>
<isset property="env.DEPLOY_DEST" />
<then>
<property name="deploy.destination" refid="env.DEPLOY_DEST"/>
</then>
</if>
<property file="build.properties"/>
However, it's fairly bulky (especially for more than more property).
Is there a way I can do something like this?
<property environment="env" />
<property name="deploy.destination" refid="env.DEPLOY_DEST"/>
<property file="build.properties"/>
With this example, it fails if env.DEPLOY_DEST
is not set.
Thanks!
Upvotes: 1
Views: 1178
Reputation: 4598
Environment variables are widely used in CI/CD platforms, if this is your case maybe the following will be useful.
First I show you my buildfile build.xml:
<project name="Demo" default="deployment">
<target name="deployment">
<property file="build.properties.ini"/>
<echo message="Your host is: ${destination}"/>
</target>
</project>
My propertyfile build.properties.ini:
destination = localhost
If we execute the only target in our buildfile, $ phing deployment
, the output will be the expected one:
$ phing deployment
[echo] Your host is: localhost
Now execute the following $ phing deployment -Ddestination=example.com
, the output is:
$ phing deployment -Ddestination=example.com
[echo] Your host is: example.com
As you can see my buildfile and propertyfile remain unchanged. however I was capable change the value of destination
property, this is because when properties are set as a command line argument they have higher priority than properties declared in a property file.
To conclude:
// Use the following in development
$ phing deployment
// And override "destination" in your CI/CD config
$ phing deployment -Ddestination=$DEPLOY_DEST
Source: https://www.phing.info/phing/guide/en/output/chunkhtml/ch04s03.html
Upvotes: 1
Reputation: 39
Given your statement:
If the environment variable is present, I'd like to prioritize it over the file's value.
you can do
<property environment="env" />
<if>
<available file="custom.properties"/>
<then>
<property file="custom.properties" override="false"/>
</then>
</if>
The key shall be override="false" to allow env property values to stay.
Upvotes: 2