FileDocCategorySizeDatePackage
Patch.javaAPI DocApache Ant 1.705953Wed Dec 13 06:16:24 GMT 2006org.apache.tools.ant.taskdefs

Patch

public class Patch extends org.apache.tools.ant.Task
Patches a file by applying a 'diff' file to it; requires "patch" to be on the execution path.
since
Ant 1.1
ant.task
category="utility"

Fields Summary
private File
originalFile
private File
directory
private boolean
havePatchfile
private org.apache.tools.ant.types.Commandline
cmd
Constructors Summary
Methods Summary
public voidexecute()
execute patch

throws
BuildException when it all goes a bit pear shaped

        if (!havePatchfile) {
            throw new BuildException("patchfile argument is required",
                                     getLocation());
        }
        Commandline toExecute = (Commandline) cmd.clone();
        toExecute.setExecutable("patch");

        if (originalFile != null) {
            toExecute.createArgument().setFile(originalFile);
        }

        Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
                                                       Project.MSG_WARN),
                                  null);
        exe.setCommandline(toExecute.getCommandline());

        if (directory != null) {
            if (directory.exists() && directory.isDirectory()) {
                exe.setWorkingDirectory(directory);
            } else if (!directory.isDirectory()) {
                throw new BuildException(directory + " is not a directory.",
                                         getLocation());
            } else {
                throw new BuildException("directory " + directory
                                         + " doesn\'t exist", getLocation());
            }
        } else {
            exe.setWorkingDirectory(getProject().getBaseDir());
        }

        log(toExecute.describeCommand(), Project.MSG_VERBOSE);
        try {
            exe.execute();
        } catch (IOException e) {
            throw new BuildException(e, getLocation());
        }
    
public voidsetBackups(boolean backups)
flag to create backups; optional, default=false

param
backups if true create backups

        if (backups) {
            cmd.createArgument().setValue("-b");
        }
    
public voidsetDestfile(java.io.File file)
The name of a file to send the output to, instead of patching the file(s) in place; optional.

param
file the file to send the output to
since
Ant 1.6

        if (file != null) {
            cmd.createArgument().setValue("-o");
            cmd.createArgument().setFile(file);
        }
    
public voidsetDir(java.io.File directory)
The directory to run the patch command in, defaults to the project's base directory.

param
directory the directory to run the patch command in
since
Ant 1.5

        this.directory = directory;
    
public voidsetIgnorewhitespace(boolean ignore)
flag to ignore whitespace differences; default=false

param
ignore if true ignore whitespace differences

        if (ignore) {
            cmd.createArgument().setValue("-l");
        }
    
public voidsetOriginalfile(java.io.File file)
The file to patch; optional if it can be inferred from the diff file

param
file the file to patch


                             
        
        originalFile = file;
    
public voidsetPatchfile(java.io.File file)
The file containing the diff output; required.

param
file the file containing the diff output

        if (!file.exists()) {
            throw new BuildException("patchfile " + file + " doesn\'t exist",
                                     getLocation());
        }
        cmd.createArgument().setValue("-i");
        cmd.createArgument().setFile(file);
        havePatchfile = true;
    
public voidsetQuiet(boolean q)
Work silently unless an error occurs; optional, default=false

param
q if true suppress set the -s option on the patch command

        if (q) {
            cmd.createArgument().setValue("-s");
        }
    
public voidsetReverse(boolean r)
Assume patch was created with old and new files swapped; optional, default=false

param
r if true set the -R option on the patch command

        if (r) {
            cmd.createArgument().setValue("-R");
        }
    
public voidsetStrip(int num)
Strip the smallest prefix containing num leading slashes from filenames.

patch's -p option.

param
num number of lines to strip
exception
BuildException if num is < 0, or other errors

        if (num < 0) {
            throw new BuildException("strip has to be >= 0", getLocation());
        }
        cmd.createArgument().setValue("-p" + num);