Hi,

I wrote a new Ant task called ftp that will perform an FTP PUT of a file.
It uses the sun.net.ftp.FtpClient class in your JDK's classes.zip.

It would be great if someone chipped in and made this capable of sending an
entire tree, using includes, excludes and basedir.  It might become
acceptable to include in the official Ant at that point? <g>

Here is how it currently works:

        <ftp server="jakarta.apache.org"
             basedir="/pub/foozle/fliggle/fog"
             user="jsmith"
             password="hamsandwich"
             src="build.xml" />

Parameters:

    server - required: hostname of FTP server
    basedir  - optional: directory ON FTP SERVER to change to
    user - required: the FTP user name
    password - required: the FTP user password
    src - required: the local file to send
    dest - optional: the remote file name (defaults to src)
    mode - optional: transfer mode.  Choices are "ascii" and "binary";
default is binary

Don't forget to update your defaults.properties to include it as a built-in
task.

John

BEGIN:VCARD
VERSION:2.1
N:Cocula;John;W.
FN:John W. Cocula
ORG:Managed Objects;Development
TITLE:Founder and CTO
TEL;WORK;VOICE:+1 703-208-3330
TEL;CELL;VOICE:703-930-7843
TEL;WORK;FAX:+1 703-208-3331
TEL;HOME;FAX:703-208-3331
ADR;WORK:;;7925 Westpark Drive;McLean;VA;22102
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:7925 Westpark Drive=0D=0AMcLean, VA 22102
X-WAB-GENDER:2
URL:http://www.cocula.com
URL:http://www.ManagedObjects.com
EMAIL;PREF;INTERNET:[EMAIL PROTECTED]
REV:20000506T230250Z
END:VCARD
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights 
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:  
 *       "This product includes software developed by the 
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written 
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.*;
import java.io.*;
import java.net.*;
import sun.net.ftp.FtpClient;

/**
 * Put a particular source. 
 *
 * Syntax: <ftp server="fred.foo.com"
 *              userid="barney"
 *              password="wilma" 
 *              mode="binary"
 *              basedir="/pub/dist"
 *              src="foo.tar.gz"
 *              dest="foo-1.2.tar.gz" />
 *
 * @author [EMAIL PROTECTED]
 */
public class Ftp extends Task {
    private String server; // required
    private String user; // required
    private String password; // required
    private String basedir; // optional
    private String source; // required
    private String dest; // optional, defaults to source
    private String mode; // optional, defaults to binary
    
    /**
     * Does the work.
     *
     * @exception BuildException Thrown in unrecovrable error.
     */
    public void execute() throws BuildException {

        FtpClient client   = null;
        OutputStream os    = null;
        InputStream is     = null;

	try {
           if (server == null)   throw new BuildException( "server must be specified" );
           if (user == null)     throw new BuildException( "user must be specified" );
           if (password == null) throw new BuildException( "password must be specified" );
           if (source == null)   throw new BuildException( "src must be specified" );
           if (dest == null)     dest = source;

           client = new FtpClient( server );
           client.login( user, password );

           if (basedir != null)
           {
              client.cd( basedir );
              basedir += "/";
           }
           else
              basedir = "/";
           
           if (mode == null)
              mode = "binary";

           if (mode.equalsIgnoreCase( "ascii" ))   client.ascii();
           else                                    client.binary();
           
           os = new BufferedOutputStream( client.put( dest ), 8192 );
           is = new BufferedInputStream( new FileInputStream( source ), 8192 );

	   project.log( "Putting: " + source + " to ftp://"; + server + "/" + basedir + dest );

	   byte[] buffer = new byte[8192];
	   int length;
	   
	   while ((length = is.read( buffer )) >= 0)
	       os.write( buffer, 0, length );

	} catch (IOException ioe) {
	    project.log("Error performing ftp" );
	    throw new BuildException( ioe.toString() );
	}
        finally {
           if (is != null)       try { is.close(); } catch (IOException ex) {}
           if (os != null)       try { os.close(); } catch (IOException ex) {}
           if (client != null)   try { client.closeServer(); } catch (IOException ex) {}
        }
    }

    /**
     * Set the FTP server name
     *
     * @param name of the FTP server.
     */
    public void setServer(String d) {
	this.server=d;
    }

    /**
     * Set the FTP user name
     *
     * @param name of the FTP user.
     */
    public void setUser(String d) {
	this.user=d;
    }

    /**
     * Set the FTP user password
     *
     * @param password of the FTP user.
     */
    public void setPassword(String d) {
	this.password=d;
    }

    /**
     * Set the FTP server base directory
     *
     * @param name of the FTP server.
     */
    public void setBasedir(String d) {
	this.basedir=d;
    }

    /**
     * Set the FTP transfer mode
     *
     * @param mode of the FTP transfer.
     */
    public void setMode(String d) {
	this.mode=d;
    }

    /**
     * Set the source file
     *
     * @param name of the source file.
     */
    public void setSrc(String d) {
	this.source=d;
    }

    /**
     * Where to copy the source file.
     *
     * @param dest Path to file on FTP server.
     */
    public void setDest(String dest) {
	this.dest = dest;
    }
}

Reply via email to