Setting up your workstation for extension development (build with Phing)

From Joomla! Documentation

Revision as of 23:04, 22 January 2009 by Dextercowley (Talk | contribs)

Quill icon.png
Page Actively Being Edited!

This article is actively undergoing a major edit for a short while.
As a courtesy, please do not edit this page while this message is displayed. The user who added this notice will be listed in the page history. This message is intended to help reduce edit conflicts; please remove it between editing sessions to allow others to edit the page. If this page has not been edited for several hours, please remove this template, or replace it with {{underconstruction}} or {{incomplete}}.

This article explains one way in which you can set up your workstation for writing Joomla! extensions. It assumes that you are using Eclipse and that you are familiar with using Eclipse for working with Joomla! and Subversion. For more information about the basics of getting started with Eclipse for Joomla! development, see Setting up your workstation for Joomla! development.

Joomla! developers can and do use many different development tools, and there is no one way that is best for everyone. This article documents one way to use Eclipse and Phing to help streamline the process of writing and testing Joomla! extensions.



If you are writing a Joomla! extension, you have a challenge. On one hand, you need to keep the source code for the extension separate from the Joomla! source code. However, you also need to be able to run and perhaps debug the extension from within Joomla!. The problem is somewhat more difficult if you are using the Subversion (SVN) source code control system for the extension source code.

One simple way to do this is to create two separate projects in Eclipse. One project will be the "development" project. It will only contain the extension source code, and, if needed, can be controlled by SVN. The second project will be a "test" project. It will contain a full copy of Joomla! as well as a copy of your extension source code. Then we will create a Phing automatic build task that will allow us to keep the test copy of the extension current with the actual development copy.

Create the Development Project

The development project will contain the source code just for your Joomla! extension. If you are using SVN, then you will create this project in Eclipse by using File → Import → Checkout Projects from SVN. If you are not using SVN, you will just create a new PHP Project and create the appropriate folders and files for the extension. You should also have an install archive file so you can install the extension in Joomla! using the normal extension installer (Extensions → Install/Uninstall).

In our simple example, our extension is a plugin called "FJ Author Plugin". This has just two files called "authorunpublish.php" and "authorunpublish.xml". We also have created a zip archive file that contains these files, so we can install this extension on a Joomla! website.

Create the Test Project

Creating the test project is very easy. Just create a new PHP Project in Eclipse (in our case, called "Joomla_1.5.0"). Then unzip a full Joomla! zip archive into the top-level folder for this project (in our example, "C:\xampp\htdocs\joomla_development\Joomla_1.5.9"). Then navigate to the back end of this site and install Joomla!. (Note: If you already have a test environment set up you can just use this environment.)

Once the environment is set up, install the current version of the extension into the Joomla! test site in the normal way. In the back end of Joomla!, navigate to Extensions → Install/Uninstall, press Browse, find the archive file for your extension, and press "Upload File and Install".

  • Note that this step is needed so that the Joomla! installer can add the extension to the database. In this case, a row is added in the jos_plugins table to register this plugin in Joomla!.
  • Also note that, at this stage, the extension programs do not have to be working or finished. You just have the have a valid XML file for the installation and the appropriate files. The contents of the files, other than the XML file used for installation, does not matter. They can even be empty files.

Install Phing

Next, we need to add the Phing program to our PHP environment. Phing is an open-source project build system based on Apache Ant. In our example, we are going to use it to copy the program files from the development project to the test project every time we build the project.

Installing Phing is very easy.

  1. Go to the command line.
  2. Change directory to the Apache PHP folder. On my XAMPPP system, it is "C:\xampp\php".
  3. Enter the command pear channel-discover
  4. Then enter the command pear install phing/phing
  5. The system will download Phing. On Windows, the executable "BAT" file will be in your PHP folder (for example, "C:\xampp\php").

Create the "build.xml" File

Next, we need to create a file called "build.xml". This file will tell Phing what we want it to do during a project build -- in our case, just copy the two plugin files from the development project to the test project.

In Eclipse, select File → New → XML and create a file called "build.xml" in the root folder of your extension project. In our example, the file will contain the following code:

<?xml version="1.0" encoding="UTF-8"?>
<project name="FJ Author Plugin" default="copy_all" basedir=".">
        <property name="src"   value="."/>
        <property name="test"  value="../Joomla_1.5.9_test/plugins/content" />
        <target name="copy_all" description="Copies files to test project.">
                <echo message="Running build.xml. Copying files from dev to test..." />
                <copy file="${src}/authorunpublish.php" todir="${test}"/>
                <copy file="${src}/authorunpublish.xml" todir="${test}"/>

Line 3 tells Phing that the default action to take is called "copy_all" and that the base directory for this is the current directory. The next two lines just define the variables "src" (our current development folder) and "test" (where we want the files to be copied). Notice that "test" is set to copy the files to the "plugins/content" folder of the Joomla! test system. Of course, this will vary with different extension types. You can define as many variables as you need.

The next line creates the "target" element, in this case the default target called "copy_all". Inside the target element we have three lines. The first is just an echo command which will display the message in the Eclipse console when the build is run. The next two actually do the work. They copy the two files from the "src" folder to the "test" folder.

There are many things you can do inside a build file with Phing. See the Phing User Guide for more information.

Add Phing to the Eclipse Build

The last step is to set up the project in Eclipse so that our Phing build program is run automatically each time we build the project. To do this:

  1. Right-click on the project and select Properties. Then select "Builders" as shown below. [Image:Screenshot_eclipse_properties_20090122.png|frame|center]]
  2. Press New to display the Choose Configuration Type dialog shown below.
    Screenshot eclipse phing1 20090122.png
  3. Select Program and press OK to show the Edit Configuration dialog shown below.
    Screenshot eclipse phing2 20090122.png
    • In the Name field, enter a descriptive name for the program.
    • In the Location field, press "Browse File System" and browse to the location of the "phing.bat" file (for example, "c:\xampp\php").
    • In the Working Directory, press "Browse Workspace" and select the root folder of the current project.
    • Note that we are not entering anything into the Arguments field. This is because Phing by default will run the file called "build.xml". Also, we specified the "copy_all" as the default task inside the "build.xml" file. So this executes by default when Phing is run.
  4. Press OK and the Propertied dialog should now be as shown below.
    Screenshot eclipse phing3 20090122.png

Finally, let's test that this works. The idea here is that our Phing task should run whenever we build the Eclipse project. In the PHP Explorer, right-click on the project and select "Build". In the Eclipse console, you should see something similar to screen below.

Screenshot eclipse phing4 20090122.png

Note that, in this example, the source files in the development project were identical to the files in the test project, so we got the message "omitted, is up to date" for both. If you change one or both of these files and build the project again, you will see they have been copied.

More Phing Examples

Below are some more examples of things you can do with Phing. Please keep in mind that there are many advanced capabilities that you can use with Phing. See the Phing website for more information.

Run Phing File Outside of Build

In the previous example, we ran the Phing build.xml file automatically when the Eclipse project was built. We can also run any Phing file directly from Eclipse using the Eclipse External Tools Configuration. To do this:

  1. Select Run → External Tools → External Tools Configuration, as shown below.
    Screenshot eclipse external tools0 20090122.png
  2. Highlight the Program option and click on the New Launch Configuration button, as shown below.
    Screenshot eclipse external tools1 20090122.png
  3. Fill out the Externals Tools Configurations dialog box as shown below.
    Screenshot eclipse external tools2 20090122.png
    • Enter a descriptive name in the Name field.
    • In the Location field, again click on the Browse File System button and find the "phing.bat" file.
    • In the Working Directory field, click on the Browse Workspace button and select the root directory of the project.
    • In the Arguments field, enter "-f build.xml". Note that this is not strictly necessary, since "build.xml" is the default file name for Phing. However, this shows how you would run any Phing file.
  4. Press the Run button to actually run the "build.xml" file. You should see the same output as you did when you ran the project build.

To re-run this file, you can just select it from the list using the Run External Programs button, as shown below.

Screenshot eclipse external tools3 20090122.png

Alternatively, you can select Run → External Tools and select it from that list.

Copy From Test Project to Development Project

In some situations, it may be more convenient to write your code in the test environment and copy files from the test project to the development project. Again, this is very easy to do with Phing. In this example, we will create a Phing file in our development project called "copy_to_dev.xml" that will copy the files from our test project to the development project.

  1. Highlight the test project (in our example, "Joomla_1.5.9_test"), right-click, and select New → XML.
  2. Place the file in the project's root directory and name it "copy_to_dev.xml".
  3. In our example, the code for this file is the following:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Plugin Test" default="copy_to_dev" basedir=".">
        <property name="test"   value="./plugins/content"/>
        <property name="dev"  value="../FJ Author Plugin" />
        <target name="copy_to_dev" description="Copies files to dev project.">
                <echo message="Copying files from test to dev..." />
                <copy file="${test}/authorunpublish.php" todir="${dev}"/>
                <copy file="${test}/authorunpublish.xml" todir="${dev}"/>
This code is very similar to the "build.xml" discussed above, except that it copies in the other direction.

To run this, add a new External Tools Run Configuration by following the steps outlined in the previous section. When you get to the External Tools Configurations dialog, enter the following in the Arguments field: -f copy_to_dev.xml.

When you run this, it should copy the files from the test project to the development project.

Alternate Copy File Syntax

Create a Zip Archive In Phing

More Complex Phing Example