XML And Phing
A valid Phing build file consists of the following parts:
1. preface to the document
2. Unique root element <project>
3. Some Phing type elements (such as <property>, <fileset>, and <patternset>)
4. One or more <target> elements. Each target contains built-in or custom Phing task elements (for example, <install>,
<Bcc> ).
Writing A Simple Buildfile
The Foobar project installs some PHP files from the source directory to the target directory and packages these files.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Project name = "FooBar" default = "dist">
<! -- ===================================================== =====-->
<! -- Target: prepare -->
<! -- ===================================================== =====-->
<Target name = "prepare">
<Echo msg = "Making directory./build"/>
<Mkdir dir = "./build"/>
</Target>
<! -- ===================================================== =====-->
<! -- Target: build -->
<! -- ===================================================== =====-->
<Target name = "build" depends = "prepare">
<Echo msg = "Copying files to build directory..."/>
<Echo msg = "Copying./about. php to./build directory..."/>
<Copy file = "./about. php" tofile = "./build/about. php"/>
<Echo msg = "Copying./browsers. php to./build directory..."/>
<Copy file = "./browsers. php" tofile = "./build/browsers. php"/>
<Echo msg = "Copying./contact. php to./build directory..."/>
<Copy file = "./contact. php" tofile = "./build/contact. php"/>
</Target>
<! -- ===================================================== =====-->
<! -- (DEFAULT) Target: dist -->
<! -- ===================================================== =====-->
<Target name = "dist" depends = "build">
<Echo msg = "Creating archive..."/>
<Tar destfile = "./build/build.tar.gz" compression = "gzip">
<Fileset dir = "./build">
<Include name = "*"/>
</Fileset>
</Tar>
<Echo msg = "Files copied and compressed in build directory OK! "/>
</Target>
</Project>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Project name = "FooBar" default = "dist">
<! -- ===================================================== =====-->
<! -- Target: prepare -->
<! -- ===================================================== =====-->
<Target name = "prepare">
<Echo msg = "Making directory./build"/>
<Mkdir dir = "./build"/>
</Target>
<! -- ===================================================== =====-->
<! -- Target: build -->
<! -- ===================================================== =====-->
<Target name = "build" depends = "prepare">
<Echo msg = "Copying files to build directory..."/>
<Echo msg = "Copying./about. php to./build directory..."/>
<Copy file = "./about. php" tofile = "./build/about. php"/>
<Echo msg = "Copying./browsers. php to./build directory..."/>
<Copy file = "./browsers. php" tofile = "./build/browsers. php"/>
<Echo msg = "Copying./contact. php to./build directory..."/>
<Copy file = "./contact. php" tofile = "./build/contact. php"/>
</Target>
<! -- ===================================================== =====-->
<! -- (DEFAULT) Target: dist -->
<! -- ===================================================== =====-->
<Target name = "dist" depends = "build">
<Echo msg = "Creating archive..."/>
<Tar destfile = "./build/build.tar.gz" compression = "gzip">
<Fileset dir = "./build">
<Include name = "*"/>
</Fileset>
</Tar>
<Echo msg = "Files copied and compressed in build directory OK! "/>
</Target>
</Project> A phing build file is usually named after build. xml. If no file name is specified, phing uses build. xml as the default file.
Execute the default target in the above Build File and run phing directly.
This will execute the target named dist. When executing a task in the build file, some information will be output, showing the affected files.
To execute other targets, you only need to specify the target name in the command line. For example, to execute a target named build, you only need to execute phing build.
For other command line parameters, see Appendix A (Fact Sheet ).
Project Element
The first element after the preface is the root element <project>. Other elements must be included in <project>. It has the following attributes:
Attribute meaning required
Name project name no
Basedir indicates the start directory of the current project. "." indicates the current directory.
Note: If this parameter is not specified, the parent directory of the build file is set to the default value. No
Default specifies the default target. If the target is not specified when the current file is called,
The default target is executed. Yes
Description project description no
Target Element
A target can depend on other targets. Phing processes the dependencies between them.
Note: The depend attribute of Phing can only specify the execution sequence of the target, and the dependency target cannot be confirmed to be executed. Phing does not execute the target that is depended on when it is not necessary.
Phing executes the target specified in the depends attribute from left to right. Note that a depended target may have been executed as early as before due to the existence of another dependency, and will not be executed again.
The following example illustrates this:
[Html]
<Target name = "A"/>
<Target name = "B" depends = "A"/>
<Target name = "C" depends = "B"/>
<Target name = "D" depends = "C, B, A"/>
<Target name = "A"/>
<Target name = "B" depends = "A"/>
<Target name = "C" depends = "B"/>
<Target name = "D" depends = "C, B, A"/> suppose we want to execute target D. Based on its depends attribute, you may think that the execution sequence is C, B,. Error! C depends on B and B depends on A, so A will execute first, then B, then C, and finally D.
A target is executed only once, even if many taget dependencies exist.
The description attribute is used to describe this target. In command line mode, you can print it out using the-projecthelp option.
Task Elements
A task is a piece of php code that can be executed. This code completes a specific function (such as the installation file ). It is defined in the build file and called by Phing.
The basic structure of a task is as follows:
[Html]
<Name attribute1 = "value1" attribute2 = "value2".../>
<Name attribute1 = "value1" attribute2 = "value2".../> name is the task name, attributeN is the attribute name, And valueN is the attribute value.
There are a series of core Tasks (see Appendix B, Core Tasks) and some optional Tasks. You can also easily define your own tasks (see Chapter 6th, Extending Phing ).
Task can be assigned an id attribute:
[Html]
<Taskname id = "taskID".../>
<Taskname id = "taskID".../> You can reference this task by id in other tasks.
Property Element
Property is an important variable in the build file. You can set the property through PropertyTask or by using commands (the property value set in the command line overwrites the property value of the same name in the build file ). A property can only have one name and one value. Property can be the attribute value of a task. You only need to place the property name between "$ {" and. For example, if we define a property called BC_BUILD_DIR and its value is 'build', we can use it like this: $ {BC_BUILD_DIR}/en. This statement is parsed as build/en.
If you use a property that is not defined through the property task, phing will find whether there is a property with the same name from the system environment variable. For example, if you use $ {BCHOME} But you have not defined it in the command line or build file, this value exists in the environment variable, then phing will use the BCHOME value in the environment variable.
Built-in Properties
Phing provides a method to access system attributes, which is the same as that defined by <property> task. For example, $ {OS. name} represents the name of the operating system. For more information, see Built-In Properties In Appendix.
More Complex Buildfile
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Project name = "testsite" basedir = "." default = "main">
<Property file = "./build. properties"/>
<Property name = "package" value = "$ {phing. project. name}" override = "true"/>
<Property name = "builddir" value = "./build/testsite" override = "true"/>
<Property name = "srcdir" value = "$ {project. basedir}" override = "true"/>
<! -- Fileset for all files -->
<Fileset dir = "." id = "allfiles">
<Include name = "**"/>
</Fileset>
<! -- ===================================================== =====-->
<! -- (DEFAULT) Target: main -->
<! -- ===================================================== =====-->
<Target name = "main" description = "main target">
<Copy todir = "$ {builddir}">
<Fileset refid = "allfiles"/>
</Copy>
</Target>
<! -- ===================================================== =====-->
<! -- Target: Rebuild -->
<! -- ===================================================== =====-->
<Target name = "rebuild" description = "rebuilds this package">
<Delete dir = "$ {builddir}"/>
<Phingcall target = "main"/>
</Target>
</Project>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Project name = "testsite" basedir = "." default = "main">
<Property file = "./build. properties"/>
<Property name = "package" value = "$ {phing. project. name}" override = "true"/>
<Property name = "builddir" value = "./build/testsite" override = "true"/>
<Property name = "srcdir" value = "$ {project. basedir}" override = "true"/>
<! -- Fileset for all files -->
<Fileset dir = "." id = "allfiles">
<Include name = "**"/>
</Fileset>
<! -- ===================================================== =====-->
<! -- (DEFAULT) Target: main -->
<! -- ===================================================== =====-->
<Target name = "main" description = "main target">
<Copy todir = "$ {builddir}">
<Fileset refid = "allfiles"/>
</Copy>
</Target>
<! -- ===================================================== =====-->
<! -- Target: Rebuild -->
<! -- ===================================================== =====-->
<Target name = "rebuild" description = "rebuilds this package">
<Delete dir = "$ {builddir}"/>
<Phingcall target = "main"/>
</Target>
</Project>
This build file first defines some properties. A fileset and two targets are defined. Next let's take a quick look at this document.
The first five lines in the project tag are used to define the property. They show two usage forms of the property Tag:
1. The second property tag contains only one file attribute. This value must correspond to a property File and can be an absolute or relative path (for the File format, see appendix I, File Formats ).
2. The tag contains the name and value attributes. After execution, you can get the value through $ {name.
Next, let's look at the <fileset> label. It defines a collection of files. You can use the inlude and exclude labels to add or exclude files. If the id attribute is set for fileset, You can reference this fileset by id.
Next we will talk about the Double Star expression "**". This specific regular expression indicates all files in all subdirectories. A star "*" only indicates all files in the directory.
Example:
[Html]
**/*. Phps
**/*. Phps indicates the files with the suffix phps in all subdirectories under the current directory.
The first task is a <copy>. Note that the fileset in the copy label does not contain the include or exclude element. Instead, it references a previously created fileset through refid. In this way, you can define a fileset and use it in the Build File multiple times.
The only note in the second target is the <phingcall> tag (for more information, see PhingCallTask ). This task runs another target in the same file as itself. The second target deletes the build directory and calls main to re-build the project.
If you use the-D parameter in the command line to assign a value to the property, the value of the property with the same name in the build file will be overwritten. For example, if you execute
[Plain] view plaincopyprint? Phing-Dbuilddir =/tmp/system-test
In the example of phing-Dbuilddir =/tmp/system-test, the builddir value is no longer./build/testsite, but/tmp/system-test.