use of org.apache.tools.ant.types.FilterSetCollection in project ant by apache.
the class Move method copyFile.
/**
* Copy fromFile to toFile.
* @param fromFile File
* @param toFile File
* @param filtering boolean
* @param overwrite boolean
*/
private void copyFile(File fromFile, File toFile, boolean filtering, boolean overwrite) {
try {
log("Copying " + fromFile + " to " + toFile, verbosity);
FilterSetCollection executionFilters = new FilterSetCollection();
if (filtering) {
executionFilters.addFilterSet(getProject().getGlobalFilterSet());
}
getFilterSets().forEach(executionFilters::addFilterSet);
getFileUtils().copyFile(fromFile, toFile, executionFilters, getFilterChains(), forceOverwrite, getPreserveLastModified(), /* append: */
false, getEncoding(), getOutputEncoding(), getProject(), getForce());
} catch (IOException ioe) {
throw new BuildException("Failed to copy " + fromFile + " to " + toFile + " due to " + ioe.getMessage(), ioe, getLocation());
}
}
use of org.apache.tools.ant.types.FilterSetCollection in project ant by apache.
the class Copy method doResourceOperations.
/**
* Actually does the resource copies.
* This is a good method for subclasses to override.
* @param map a map of source resource to array of destination files.
* @since Ant 1.7
*/
protected void doResourceOperations(final Map<Resource, String[]> map) {
if (!map.isEmpty()) {
log("Copying " + map.size() + " resource" + (map.size() == 1 ? "" : "s") + " to " + destDir.getAbsolutePath());
for (final Map.Entry<Resource, String[]> e : map.entrySet()) {
final Resource fromResource = e.getKey();
for (final String toFile : e.getValue()) {
try {
log("Copying " + fromResource + " to " + toFile, verbosity);
final FilterSetCollection executionFilters = new FilterSetCollection();
if (filtering) {
executionFilters.addFilterSet(getProject().getGlobalFilterSet());
}
for (final FilterSet filterSet : filterSets) {
executionFilters.addFilterSet(filterSet);
}
ResourceUtils.copyResource(fromResource, new FileResource(destDir, toFile), executionFilters, filterChains, forceOverwrite, preserveLastModified, /* append: */
false, inputEncoding, outputEncoding, getProject(), getForce());
} catch (final IOException ioe) {
String msg = "Failed to copy " + fromResource + " to " + toFile + " due to " + getDueTo(ioe);
final File targetFile = new File(toFile);
if (!(ioe instanceof ResourceUtils.ReadOnlyTargetFileException) && targetFile.exists() && !targetFile.delete()) {
msg += " and I couldn't delete the corrupt " + toFile;
}
if (failonerror) {
throw new BuildException(msg, ioe, getLocation());
}
log(msg, Project.MSG_ERR);
}
}
}
}
}
use of org.apache.tools.ant.types.FilterSetCollection in project ant by apache.
the class Rmic method moveGeneratedFile.
/**
* Move the generated source file(s) to the base directory
*
* @throws BuildException When error
* copying/removing files.
*/
private void moveGeneratedFile(File baseDir, File sourceBaseFile, String classname, RmicAdapter adapter) throws BuildException {
String classFileName = classname.replace('.', File.separatorChar) + ".class";
String[] generatedFiles = adapter.getMapper().mapFileName(classFileName);
if (generatedFiles == null) {
return;
}
for (String generatedFile : generatedFiles) {
if (!generatedFile.endsWith(".class")) {
// have a corresponding Java source for example.
continue;
}
String sourceFileName = StringUtils.removeSuffix(generatedFile, ".class") + ".java";
File oldFile = new File(baseDir, sourceFileName);
if (!oldFile.exists()) {
// no source file generated, nothing to move
continue;
}
File newFile = new File(sourceBaseFile, sourceFileName);
try {
if (filtering) {
FILE_UTILS.copyFile(oldFile, newFile, new FilterSetCollection(getProject().getGlobalFilterSet()));
} else {
FILE_UTILS.copyFile(oldFile, newFile);
}
oldFile.delete();
} catch (IOException ioe) {
throw new BuildException("Failed to copy " + oldFile + " to " + newFile + " due to " + ioe.getMessage(), ioe, getLocation());
}
}
}
use of org.apache.tools.ant.types.FilterSetCollection in project ant by apache.
the class Copy method doFileOperations.
/**
* Actually does the file (and possibly empty directory) copies.
* This is a good method for subclasses to override.
*/
protected void doFileOperations() {
if (!fileCopyMap.isEmpty()) {
log("Copying " + fileCopyMap.size() + " file" + (fileCopyMap.size() == 1 ? "" : "s") + " to " + destDir.getAbsolutePath());
for (final Map.Entry<String, String[]> e : fileCopyMap.entrySet()) {
final String fromFile = e.getKey();
for (final String toFile : e.getValue()) {
if (fromFile.equals(toFile)) {
log("Skipping self-copy of " + fromFile, verbosity);
continue;
}
try {
log("Copying " + fromFile + " to " + toFile, verbosity);
final FilterSetCollection executionFilters = new FilterSetCollection();
if (filtering) {
executionFilters.addFilterSet(getProject().getGlobalFilterSet());
}
for (final FilterSet filterSet : filterSets) {
executionFilters.addFilterSet(filterSet);
}
fileUtils.copyFile(new File(fromFile), new File(toFile), executionFilters, filterChains, forceOverwrite, preserveLastModified, /* append: */
false, inputEncoding, outputEncoding, getProject(), getForce());
} catch (final IOException ioe) {
String msg = "Failed to copy " + fromFile + " to " + toFile + " due to " + getDueTo(ioe);
final File targetFile = new File(toFile);
if (!(ioe instanceof ResourceUtils.ReadOnlyTargetFileException) && targetFile.exists() && !targetFile.delete()) {
msg += " and I couldn't delete the corrupt " + toFile;
}
if (failonerror) {
throw new BuildException(msg, ioe, getLocation());
}
log(msg, Project.MSG_ERR);
}
}
}
}
if (includeEmpty) {
int createCount = 0;
for (final String[] dirs : dirCopyMap.values()) {
for (String dir : dirs) {
final File d = new File(dir);
if (!d.exists()) {
if (!d.mkdirs() && !d.isDirectory()) {
log("Unable to create directory " + d.getAbsolutePath(), Project.MSG_ERR);
} else {
createCount++;
}
}
}
}
if (createCount > 0) {
log("Copied " + dirCopyMap.size() + " empty director" + (dirCopyMap.size() == 1 ? "y" : "ies") + " to " + createCount + " empty director" + (createCount == 1 ? "y" : "ies") + " under " + destDir.getAbsolutePath());
}
}
}
Aggregations