use of org.apache.tools.ant.types.resources.FileResource in project lombok by rzwitserloot.
the class WebUpToDate method eval.
/**
* Evaluate (all) target and source file(s) to
* see if the target(s) is/are up-to-date.
* @return true if the target(s) is/are up-to-date
*/
public boolean eval() {
if (sourceFileSets.size() == 0 && sourceResources.size() == 0 && sourceFile == null) {
throw new BuildException("At least one srcfile or a nested <srcfiles> or <srcresources> element must be set.");
}
if ((sourceFileSets.size() > 0 || sourceResources.size() > 0) && sourceFile != null) {
throw new BuildException("Cannot specify both the srcfile attribute and a nested <srcfiles> or <srcresources> element.");
}
if (urlbase == null) {
throw new BuildException("The urlbase attribute must be set.");
}
// if the source file isn't there, throw an exception
if (sourceFile != null && !sourceFile.exists()) {
throw new BuildException(sourceFile.getAbsolutePath() + " not found.");
}
boolean upToDate = true;
if (sourceFile != null) {
Resource fileResource = new FileResource(sourceFile);
upToDate = isUpToDate(fileResource);
}
if (upToDate) {
Enumeration e = sourceFileSets.elements();
while (upToDate && e.hasMoreElements()) {
FileSet fs = (FileSet) e.nextElement();
Iterator it = fs.iterator();
while (upToDate && it.hasNext()) {
Resource r = (Resource) it.next();
upToDate = isUpToDate(r);
}
}
}
if (upToDate) {
Resource[] r = sourceResources.listResources();
for (int i = 0; upToDate && i < r.length; i++) {
upToDate = isUpToDate(r[i]);
}
}
return upToDate;
}
use of org.apache.tools.ant.types.resources.FileResource in project processing by processing.
the class AppBundlerTask method copyClassPathRefEntries.
private void copyClassPathRefEntries(File javaDirectory) throws IOException {
if (classPathRef != null) {
org.apache.tools.ant.types.Path classpath = (org.apache.tools.ant.types.Path) classPathRef.getReferencedObject(getProject());
Iterator<?> iter = classpath.iterator();
while (iter.hasNext()) {
FileResource resource = (FileResource) iter.next();
File source = resource.getFile();
File destination = new File(javaDirectory, source.getName());
copy(source, destination);
}
}
}
use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.
the class Copy method execute.
/**
* Perform the copy operation.
* @exception BuildException if an error occurs.
*/
@Override
public void execute() throws BuildException {
// may be altered in validateAttributes
final File savedFile = file;
final File savedDestFile = destFile;
final File savedDestDir = destDir;
ResourceCollection savedRc = null;
if (file == null && destFile != null && rcs.size() == 1) {
// will be removed in validateAttributes
savedRc = rcs.elementAt(0);
}
try {
// make sure we don't have an illegal set of options
try {
validateAttributes();
} catch (final BuildException e) {
if (failonerror || !getMessage(e).equals(MSG_WHEN_COPYING_EMPTY_RC_TO_FILE)) {
throw e;
} else {
log("Warning: " + getMessage(e), Project.MSG_ERR);
return;
}
}
// deal with the single file
copySingleFile();
// deal with the ResourceCollections
/* for historical and performance reasons we have to do
things in a rather complex way.
(1) Move is optimized to move directories if a fileset
has been included completely, therefore FileSets need a
special treatment. This is also required to support
the failOnError semantic (skip filesets with broken
basedir but handle the remaining collections).
(2) We carry around a few protected methods that work
on basedirs and arrays of names. To optimize stuff, all
resources with the same basedir get collected in
separate lists and then each list is handled in one go.
*/
final Map<File, List<String>> filesByBasedir = new HashMap<>();
final Map<File, List<String>> dirsByBasedir = new HashMap<>();
final Set<File> baseDirs = new HashSet<>();
final List<Resource> nonFileResources = new ArrayList<>();
for (ResourceCollection rc : rcs) {
// Step (1) - beware of the ZipFileSet
if (rc instanceof FileSet && rc.isFilesystemOnly()) {
final FileSet fs = (FileSet) rc;
DirectoryScanner ds;
try {
ds = fs.getDirectoryScanner(getProject());
} catch (final BuildException e) {
if (failonerror || !getMessage(e).endsWith(DirectoryScanner.DOES_NOT_EXIST_POSTFIX)) {
throw e;
}
if (!quiet) {
log("Warning: " + getMessage(e), Project.MSG_ERR);
}
continue;
}
final File fromDir = fs.getDir(getProject());
final String[] srcFiles = ds.getIncludedFiles();
final String[] srcDirs = ds.getIncludedDirectories();
if (!flatten && mapperElement == null && ds.isEverythingIncluded() && !fs.hasPatterns()) {
completeDirMap.put(fromDir, destDir);
}
add(fromDir, srcFiles, filesByBasedir);
add(fromDir, srcDirs, dirsByBasedir);
baseDirs.add(fromDir);
} else {
if (!rc.isFilesystemOnly() && !supportsNonFileResources()) {
throw new BuildException("Only FileSystem resources are supported.");
}
for (final Resource r : rc) {
if (!r.isExists()) {
final String message = "Warning: Could not find resource " + r.toLongString() + " to copy.";
if (!failonerror) {
if (!quiet) {
log(message, Project.MSG_ERR);
}
} else {
throw new BuildException(message);
}
continue;
}
File baseDir = NULL_FILE_PLACEHOLDER;
String name = r.getName();
final FileProvider fp = r.as(FileProvider.class);
if (fp != null) {
final FileResource fr = ResourceUtils.asFileResource(fp);
baseDir = getKeyFile(fr.getBaseDir());
if (fr.getBaseDir() == null) {
name = fr.getFile().getAbsolutePath();
}
}
// files.
if (r.isDirectory() || fp != null) {
add(baseDir, name, r.isDirectory() ? dirsByBasedir : filesByBasedir);
baseDirs.add(baseDir);
} else {
// a not-directory file resource
// needs special treatment
nonFileResources.add(r);
}
}
}
}
iterateOverBaseDirs(baseDirs, dirsByBasedir, filesByBasedir);
// do all the copy operations now...
try {
doFileOperations();
} catch (final BuildException e) {
if (!failonerror) {
if (!quiet) {
log("Warning: " + getMessage(e), Project.MSG_ERR);
}
} else {
throw e;
}
}
if (!nonFileResources.isEmpty() || singleResource != null) {
final Resource[] nonFiles = nonFileResources.toArray(new Resource[nonFileResources.size()]);
// restrict to out-of-date resources
final Map<Resource, String[]> map = scan(nonFiles, destDir);
if (singleResource != null) {
map.put(singleResource, new String[] { destFile.getAbsolutePath() });
}
try {
doResourceOperations(map);
} catch (final BuildException e) {
if (!failonerror) {
if (!quiet) {
log("Warning: " + getMessage(e), Project.MSG_ERR);
}
} else {
throw e;
}
}
}
} finally {
// clean up again, so this instance can be used a second
// time
singleResource = null;
file = savedFile;
destFile = savedDestFile;
destDir = savedDestDir;
if (savedRc != null) {
rcs.insertElementAt(savedRc, 0);
}
fileCopyMap.clear();
dirCopyMap.clear();
completeDirMap.clear();
}
}
use of org.apache.tools.ant.types.resources.FileResource 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.resources.FileResource in project ant by apache.
the class Copy method buildMap.
/**
* Create a map of resources to copy.
*
* @param fromResources The source resources.
* @param toDir the destination directory.
* @param mapper a <code>FileNameMapper</code> value.
* @return a map of source resource to array of destination files.
* @since Ant 1.7
*/
protected Map<Resource, String[]> buildMap(final Resource[] fromResources, final File toDir, final FileNameMapper mapper) {
final Map<Resource, String[]> map = new HashMap<>();
Resource[] toCopy;
if (forceOverwrite) {
final List<Resource> v = new ArrayList<>();
for (Resource rc : fromResources) {
if (mapper.mapFileName(rc.getName()) != null) {
v.add(rc);
}
}
toCopy = v.toArray(new Resource[v.size()]);
} else {
toCopy = ResourceUtils.selectOutOfDateSources(this, fromResources, mapper, name -> new FileResource(toDir, name), granularity);
}
for (Resource rc : toCopy) {
final String[] mappedFiles = mapper.mapFileName(rc.getName());
if (mappedFiles == null || mappedFiles.length == 0) {
throw new BuildException("Can't copy a resource without a" + " name if the mapper doesn't" + " provide one.");
}
if (!enableMultipleMappings) {
map.put(rc, new String[] { new File(toDir, mappedFiles[0]).getAbsolutePath() });
} else {
// reuse the array created by the mapper
for (int k = 0; k < mappedFiles.length; k++) {
mappedFiles[k] = new File(toDir, mappedFiles[k]).getAbsolutePath();
}
map.put(rc, mappedFiles);
}
}
return map;
}
Aggregations