I am copying list of files from source directory to destination directory, i need to check duplicate file name before copying..
Thanks..
I am copying list of files from source directory to destination directory, i need to check duplicate file name before copying..
Thanks..
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:
org.apache.tools.ant.taskdefs.Copy
Actually does the file (and possibly empty directory) copies. This is a good method for subclasses to override.
@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();
}
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>