Choosing properties in Phing (and Ant) build scripts
Asked Answered
D

1

8

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
db.dev.hostname=localhost
db.dev.database=foo
db.dev.username=foo_user
db.dev.password=foo_password

# Connection details for the staging database
db.staging.hostname=some.remote.server
db.staging.database=bar
db.staging.username=bar_user
db.staging.password=bar_password

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?

Defect answered 1/8, 2011 at 8:58 Comment(0)
E
7

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}" />
  </target>
</project>

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>
Eggplant answered 1/8, 2011 at 14:56 Comment(1)
Awesome! I didn't know that nesting properties worked (and found a few reports that it didn't work, so I never tried). As for the input, I prefer -D flags because I can script that again from the outside (think about hooking Phing into yout Git pre-commmit for example).Defect

© 2022 - 2024 — McMap. All rights reserved.