Using Custom Properties In Phing

8th January 2009

Phing allows you to set up certain parameters that can be used throughout the rest of the script. These might be used to define a non standard build directory, or to store database connection details that can be written to the connection file during the build.

Properties are defined using the property element, which you should place at the top of your build.xml file in order to make it easy for other developers to see what is going on. The following example defines a property and the uses the main target to print the property out.

  1.  
  2.  
  3.  
  4. property1: ${property1}
  5.  

Running this build.xml file will product the following result.

  1. C:\myProject>phing
  2. Buildfile: C:\myProject\build.xml
  3.  
  4. myProject > main:
  5. [echo] property1: value1
  6.  
  7. BUILD FINISHED
  8.  
  9. Total time: 0.2119 seconds

It is possible to define properties using the command line. The following build.xml file is similar to the first but it tries to print out a second property that is not defined. The main target now has an attribute called if, which is used to see if a property is defined. If the property2 property is not defined then the target will not run, but it will also not product any errors.

  1.  
  2.  
  3.  
  4. property1: ${property1}
  5. property2: ${property2}
  6.  

To run this script with property2 present use the -D flag along with phing.

phing -Dpropertyq2 value2

This can also be written as:

phing -Dpropertyq2=value2

The opposite of the if attribute is the unless attribute. If the property in the unless attribute is defined then the target element is not run. This provides a convenient way of turning on and off targets.

  1. <?xml version="1.0"??>
  2. <!-- build xml -->
  3. <project name="myProject" default="main">
  4. <property name="property1" value="value1"></property>
  5.  
  6. <target name="main" unless="property2">
  7. <echo>property1: ${property1}</echo>
  8. </target>
  9. </project>

The main target can be turned off in by using the following command.

phing -Dproperty2 value2

When a property is set on the command line it overrides any value that has been set for that property within the build.xml file.

You can also override properties from within the same build.xml file by using the override attribute of a second property element.

  1. <?xml version="1.0"??>
  2. <!-- build xml -->
  3. <project name="myProject" default="main">
  4. <property name="property1" value="value1"></property>
  5.  
  6. <target name="main">
  7. <property name="property1" value="wibble" override="yes"></property>
  8. <echo>property1: ${property1}</echo>
  9. </target>
  10. </project>

The value of the property1 property will now be wibble.

Automated Build With Phing

Comments

Permalink
Thanks for showing me the way to set Properties from Command Line. I have one question: I set a property, which is defined in the project root, in a target with override="true" and want to use it in another target. But: I always get the initial value of that property. Is there a workaround for that or is it not possible ?

Wolfgang (Mon, 05/26/2014 - 09:32)

Add new comment

The content of this field is kept private and will not be shown publicly.