Home » Php » php – Choosing properties in Phing (and Ant) build scripts

php – Choosing properties in Phing (and Ant) build scripts

Posted by: admin July 12, 2020 Leave a comment


I am working with databases in my build script. All the database details are in my properties file, but I was wondering how I could easily have the user choose which set of details to use? I am working with Phing, but because it is so similar to Ant I’ll also accept Ant answers.

Here’s my sample build.properties:

# Connection details for the dev database

# Connection details for the staging database

I would like to offer the user a simple build flag to choose which database to use. Suppose I have a build task to check a database schema. I would like to offer a build flag like so:

phing -Ddatabase=staging check-schema

That should use the db.staging.* properties for the database connection details. How can I achieve such a thing?

How to&Answers:

In Phing build files, you’re able to nest properties. Doing that would get the functionality that you’re looking for.

<?xml version="1.0"?>
<project name="test" default="init">
  <property name="database" value="staging" />
  <property name="db.dev.hostname" value="localhost" />
  <property name="db.staging.hostname" value="some.remote.server" />
  <property name="db.hostname" value="${db.${database}.hostname}" />

  <target name="init">
    <echo msg="${db.hostname}" />

You might also want to look into the input tag to make things easier for users instead of specifying the -D command line option:

  <input propertyname="database" validargs="dev,staging">Which database?</input>