Ant task to chek duplicate file in destination dir

2019-07-19 19:03发布

问题:

I am copying list of files from source directory to destination directory, i need to check duplicate file name before copying..

Thanks..

回答1:

Just wondering, would this be sufficient?

<copy todir="../new/dir" overwrite="false" verbose="true">
    <fileset dir="src_dir"/>
</copy>

As manual says ( http://ant.apache.org/manual/Tasks/copy.html ):

verbose - Log the files that are being copied.

overwrite - Overwrite existing files even if the destination files are newer.

As this would be a low effort solution. Otherwise, I think you need to create your own ant task.


UPDATE:

OK, so I checked the sources of ant copy task, and I believe you can do the required by extending it in your subclass (new ant task). As I assume you're running multiple files copying instead of just one.

So you need to:

  • well this is not a must, but I believe it would help you: download source code of Ant (version you're using), let's assume you use the latest one: http://ant.apache.org/srcdownload.cgi
  • create your own ant task. For docs how to do it and use it, see: http://ant.apache.org/manual/develop.html (check example section)
  • make sure your class extends class: org.apache.tools.ant.taskdefs.Copy
  • override superclass method doFileOperations, as it's javadoc says:

    Actually does the file (and possibly empty directory) copies. This is a good method for subclasses to override.

Check the method contents in the sources you downloaded and your implenentation could look similar to this:

@Override
protected void doFileOperations() {
    if (fileCopyMap.size() > 0) {

    Enumeration e = fileCopyMap.keys();
    while (e.hasMoreElements()) {
        String fromFile = (String) e.nextElement();
        String[] toFiles = (String[]) fileCopyMap.get(fromFile);

        for (int i = 0; i < toFiles.length; i++) {
            String toFile = toFiles[i];

            if (fromFile.equals(toFile)) {
                log("Skipping self-copy of " + fromFile, verbosity);
                continue;
            }

            if (new File(toFile).exists) {
                log ("Warning: dest file already exists");
            }
        }
    }
            ...

    super.doFileOperations();
}


回答2:

Here is a more complete implementation:

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Copy;

public class CopyNonDuplicateTast extends Copy {

    public CopyNonDuplicateTast() {
        setOverwrite(true);
    }

    @Override
    public void setOverwrite(boolean overwrite) {
        super.setOverwrite(true);
    }

    @Override
    protected void doFileOperations() {

        Set<String> allToFiles = new HashSet<String>();

        for (final Map.Entry<String, String[]> e : fileCopyMap.entrySet()) {
            final String fromFile = e.getKey();
            final String[] toFiles = e.getValue();

            for (int i = 0; i < toFiles.length; i++) {
                final String toFile = toFiles[i];

                if (fromFile.equals(toFile)) {
                    continue;
                }

                File destFile = new File(toFile);
                if ((destFile.exists() && destFile.isFile()) || allToFiles.contains(toFile)) {
                    String msg = "Destination file \"" + toFile + "\" exists";
                    IOException ex = new IOException(msg);
                    if (failonerror) {
                        throw new BuildException(msg, ex, getLocation());
                    }
                    log(msg, Project.MSG_ERR);
                }
                allToFiles.add(toFile);
            }
        }

        super.doFileOperations();
    }

}

With this implementation, two filesets inside same copy task can fail:

<copyNonDuplicate todir="${deployment.dir}/languageFiles">
    <fileset dir="../MyComponent1/src">
        <include name="**/*.properties" />
    </fileset>
    <fileset dir="../MyComponent2/src">
        <include name="**/*.properties" />
    </fileset>
</copyNonDuplicate>


标签: ant