• Aucun résultat trouvé

Using Ant and FTP

Dans le document CHRIS ALLEN (Page 168-171)

Ant comes with an FTP task built in, but it’s considered an “optional” task. What this means is you have to download some extra files into your Ant distribution to make it work. If you read the documenta-tion on the Ant website at http://ant.apache.org/manual/OptionalTasks/ftp.html, you will see that it requires the following:

A networking library called commons-net. You can download this library from http://

commons.apache.org/net/.

A regular expression library named jakarta-oro. You can download this library fromhttp://

jakarta.apache.org/oro/.

Download both of those libraries, unzip them, and place the.jarfile in yourant/libdirectory. Once you’ve done that, you can begin using the Ant FTP task. An example follows:

<ftp verbose="yes" passive="yes"

remotedir="/www" server="www.yourserver.com"

userid="ftp_login" password="ftp_password">

<fileset dir="staging" />

</ftp>

The parameters to this command are as follows:

verbose="yes": This tells Ant to issue a higher level of logging so you can see errors, warnings, or messages about current progress.

passive="yes": This instructs Ant to use passive instead of active FTP transfers. Depending on the server you’re connecting to, one option or the other might not be available.

remotedir="/www": This tells Ant where to put the files on the remote server.

server="www.yourserver.com": Theserverattribute specifies the host name of the server to connect to.

userid="ftp_login" password="ftp_password": Theuseridandpasswordattributes specify your login credentials to the remote server.

<fileset dir="staging" />: Thefilesettag (or multiplefilesettags) specifies what files to copy from the local machine.

In this example, we specified both the username and password, but you could have also specified those as parameters and placed the values in an external properties file like so:

<property file="build.properties"/>

Then you could create a file calledbuild.propertiesand place the following content inside it:

ftp.username=myUsername ftp.password=myPassword

This gives you the benefit of not storing your username and password in thebuild.xmlfile, making it safe to share with others. But it still has the drawback of having your username and password in a file where anybody who uses your computer may find it.

If you don’t like the idea of storing your password in a file on your computer, you can use theinput task to cause Ant to prompt for it every time it’s run:

<input message="Enter your FTP password." addproperty="ftp.password" />

<ftp verbose="yes" passive="yes"

remotedir="/www" server="www.yourserver.com"

userid="myUsername" password="${ftp.password}">

<fileset dir="staging" />

</ftp>

All the previous examples upload the entire site to the staging server. Oftentimes, you want to trans-fer only the files that have changed. Theftp task has a property calleddependsthat will check the time stamp of the file on the server and compare it to the time stamp on your local copy and upload only the newer files. However, this method can be fragile, because the time of your local computer and the server might not be in sync. A better method is to use themodifiedsubtag of thefileset tag. The first time themodified tag runs, it creates a file listing the checksums of all your files. A checksum is a short string automatically generated based on the content of a file. The next time the modifiedtag is encountered, those checksums are compared to the current checksums of your files to determine whether they’ve been modified and need to be re-sent. An example follows:

<input message="Enter your FTP password." addproperty="ftp.password" />

<ftp verbose="yes" passive="yes"

When using themodifiedtag, make sure to use a different cache file for each location you upload to.

In this example, you want to upload to a staging and a production web server. If you were to use the same cache file for both and upload your files to the staging server, everything would work fine. But if you then tried to upload to the production server, no files would be transferred because the check-sums in your cache would match the checkcheck-sums of the file, making Ant think it had already sent those files. If you ever need to transmit all the files again, you can simply delete your.cachefile, which will cause Ant to “forget” which files it sent. A full example of thedeployStaginganddeployProduction targets follows:

<target name="deployStaging">

<input message="Enter your FTP password." addproperty="ftp.password"/>

<ftp verbose="yes" passive="yes"

<input message="Enter your FTP password." addproperty="ftp.password"/>

<ftp verbose="yes" passive="yes"

remotedir="/www" server="www.yourserver.com"

userid="myUsername" password="${ftp.password}">

<fileset dir="staging">

<modified>

<param name="cache.cachefile" value="production.cache"/>

More and more web hosting companies are starting to support connections to the web server over a secure SSH connection. By using this type of connection, you can use either SFTP or SCP to copy your website files to the server. Like FTP, using either SCP or SFTP with Ant requires you to download and install an extra library. The required library is called JSch, and you can download it from http://www.jcraft.com/jsch/index.html. To install it, just download thejsch.jarfile and place it in yourant/libdirectory.

To use either SCP or SFTP, you use thescpAnt tag. An example of SCP usage follows:

<scp trust="true" todir="username:password@yourServer.com:">

<fileset dir="staging" />

</scp>

To use SFTP instead, simply add ansftpparameter:

<scp sftp="true" trust="true"

todir="username:password@yourServer.com:">

<fileset dir="staging" />

</scp>

You can employ all the methods you used in the FTP example to pass a username and password. You can also use the modified tag to send only those files that have changed. A full example of deployStaginganddeployProductionusing SCP follows:

<target name="deployStaging">

<input message="Enter your SSH password." addproperty="ssh.password"/>

<scp trust="true"

SCP and SFTP are similar protocols and can be used interchangeably most of the time.

They both work over an encrypted SSH connection, and they both transmit files.

When choosing which to use, keep these factors in mind. Secure Copy (SCP) is more widely available and is best when transmitting a few small files. Secure File Transfer Protocol (SFTP) isn’t available as often but is optimized for big groups of larger files.

Dans le document CHRIS ALLEN (Page 168-171)