Eclipse simplifies the process of deploying your site to a web server. You can even use Ant, traditionally thought of as a Java tool, to help you in the deployment. This article, the second of two parts, explains how. It is excerpted from PHPEclipse: A User Guide, written by Shu-Wai Chow (Packt Publishing, 2006; ISBN: 1904811442).
Deploying your Site with PHPEclipse, continued (Page 1 of 4 )
Adding Files to the Ant Classpath
By naming these files in the Ant classpath, we are telling Eclipse of their existence, and that critical JARs may be found in them. To add them to the classpath, go to the Window | Preferences | Ant | Runtime menu option. Click on Ant Home Entries (Default) in the list under Classpath. Click on the Add External Jars… button.
Select the two new JARs that you just downloaded. Click on the OK button. Our installation of Ant is now FTP enabled.
Creating Our Sample Ant Build File
In this demonstration, we will make a script to actually do what we have described. Our script will log into CVS, pull files from the Head of our project, put the files locally in a temporary directory, FTP the files into a web server, and finally do some cleaning up by deleting our temporary directory.
Generally, Ant scripts are composed of targets. Targets are basically groupings of commands called tasks. In order to be functional, each target contains at least one Ant task. These tasks are listed and explained in the official Ant documentation. A target might contain the task of compiling a Java program. At the beginning of the Ant file, we ordain one target as the special default target. Normally, when you run an Ant file, you pass it the target that you want to execute. If it doesn't have one, it will execute the default target.
We are going to create an Ant file with four targets. The first will check out the project from CVS into our local machine. The next will upload the files to a web server. The third target will delete the copy of the application that we downloaded from CVS. Our default target sits above these three. Its job will be to call the previous three targets in the correct order.
First, create a file in our project called build.xml. This is the standard name for Ant build files. This script has already been included in the sample code, so you do not have to type everything in manually. However, we will walk through the build file in its entirety and look at it section by section. You may want to pull up the complete file to follow along.
This is the beginning of the Ant file. We include in a proper XML declaration here for good form. Ant files begin and end with a <project> tag. The important part of this tag is the default attribute. The default attribute tells the Ant engine which target to run if one is not specified when the build file is executed from the command line. You must declare this target later on in the file. While not officially required, Eclipse needs it to run build files because it automatically fires build files as-is. In other words, Eclipse does not prompt you for a target. Further, it is good coding practice to include a default.
The code snippet shown above is our first target, named startPublish. We separate our deployment process into three separate tasks—check the files out from CVS into our temporary area, FTP them to our server, and finally clean up our temporary area. The startPublish target merely calls the other tasks in the correct order. Calling other targets is done by the antcall tag, which includes a mandatory target to call defined by the target attribute.
This is our second target, getFilesFromCVS. There is only one task, cvs. This task pulls files from a CVS repository. The cvsRoot attribute defines the repository based on the CVS connection string. We specify a package to check out in the package attribute, and the dest attribute specifies the local directory where we want to place the checked out files.
We briefly talked about CVS connection strings in the previous chapter. Here, we use the local connection method to connect to our local CVS server. This method is simple and easy, but only works on a CVS repository that is on our machine. Consult your CVS administrator or CVS documentation on how to construct a proper connection string for your server setup.
The interaction with the FTP server is contained in the third target. First, we enter our connection settings in the appropriate attributes within the ftp tag. The attributes server and userid are your account credentials on the FTP server. While the FTP and SFTP export plug-ins in Eclipse do not work with local machines, Ant uses its own FTP client, and thus, can send files to a local FTP server. remotedir is the attribute that tells Ant the location of the remote directory on which we will be performing our actions. The action attribute tells Ant what to do on that directory. All our standard FTP commands, for example, get/put, are available via the action attribute.
<fileset dir="/tmp/ShelterSite" id="id">
Nested in the ftp element is the fileset tag. The dir attribute specifies the local root directory we wish to use. Combined with the previous ftp element's action and remotedir attributes, Ant will send this local directory to the remote directory.
You may have created this build file underneath the project directory and checked it into CVS. This is not a bad thing because you now have a history of a file that is a critical piece of the project. However, it is a bad thing if it gets FTPed into the production web server, especially if you have FTP server passwords! We should also prevent Eclipse from uploading its own .project file to the web server.
To exclude the build and .project files from the upload, we use exclude elements for these two files nested within fileset. There is a corresponding include element to specify the inclusion of files. For both elements we name our file using the name attribute. Ant includes a powerful pattern-matching engine for use in include and exclude. In our example, the first exclude tells Ant to exclude anything named build.xml from the upload process. The second tells Ant to exclude anything ending with .project from the upload process. Be aware that the order of include and exclude tags is important in a fileset element, with the lower include/exclude taking precedence over previous ones in case of conflict. For example, if you tell Ant to include a directory, you can name specific files to exclude within that directory by placing the exclude tags after the directory include tag.
</fileset> </ftp> </target>
Finally, we close out the fileset, ftp, and target elements in this block.
At the end of the file, we clean up our temporary directory. We do this with our fourth target. The only task in here is a delete task. It identifies which local directory we want to delete with a dir attribute. Lastly, we close our project element. Our build file is complete. It's time to run our file.