use of org.apache.tools.ant.types.FileSet in project tomcat70 by apache.
the class CheckEol method execute.
/**
* Perform the check
*
* @throws BuildException if an error occurs during execution of
* this task.
*/
@Override
public void execute() throws BuildException {
Mode mode = null;
if ("\n".equals(eoln)) {
mode = Mode.LF;
} else if ("\r\n".equals(eoln)) {
mode = Mode.CRLF;
} else {
log("Line ends check skipped, because OS line ends setting is neither LF nor CRLF.", Project.MSG_VERBOSE);
return;
}
int count = 0;
List<CheckFailure> errors = new ArrayList<CheckFailure>();
// Step through each file and check.
for (FileSet fs : filesets) {
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
File basedir = ds.getBasedir();
String[] files = ds.getIncludedFiles();
if (files.length > 0) {
log("Checking line ends in " + files.length + " file(s)");
for (int i = 0; i < files.length; i++) {
File file = new File(basedir, files[i]);
log("Checking file '" + file + "' for correct line ends", Project.MSG_DEBUG);
try {
check(file, errors, mode);
} catch (IOException e) {
throw new BuildException("Could not check file '" + file.getAbsolutePath() + "'", e);
}
count++;
}
}
}
if (count > 0) {
log("Done line ends check in " + count + " file(s), " + errors.size() + " error(s) found.");
}
if (errors.size() > 0) {
String message = "The following files have wrong line ends: " + errors;
// We need to explicitly write the message to the log, because
// long BuildException messages may be trimmed. E.g. I observed
// this problem with Eclipse IDE 3.7.
log(message, Project.MSG_ERR);
throw new BuildException(message);
}
}
use of org.apache.tools.ant.types.FileSet in project georocket by georocket.
the class ImportCommand method doRun.
@Override
public void doRun(String[] remainingArgs, InputReader in, PrintWriter out, Handler<Integer> handler) throws OptionParserException, IOException {
long start = System.currentTimeMillis();
// resolve file patterns
Queue<String> queue = new ArrayDeque<>();
for (String p : patterns) {
// convert Windows backslashes to slashes (necessary for Files.newDirectoryStream())
if (SystemUtils.IS_OS_WINDOWS) {
p = FilenameUtils.separatorsToUnix(p);
}
// collect paths and glob patterns
List<String> roots = new ArrayList<>();
List<String> globs = new ArrayList<>();
String[] parts = p.split("/");
boolean rootParsed = false;
for (String part : parts) {
if (!rootParsed) {
if (hasGlobCharacter(part)) {
globs.add(part);
rootParsed = true;
} else {
roots.add(part);
}
} else {
globs.add(part);
}
}
if (globs.isEmpty()) {
// string does not contain a glob pattern at all
queue.add(p);
} else {
// string contains a glob pattern
if (roots.isEmpty()) {
// there are not paths in the string. start from the current
// working directory
roots.add(".");
}
// add all files matching the pattern
String root = String.join("/", roots);
String glob = String.join("/", globs);
Project project = new Project();
FileSet fs = new FileSet();
fs.setDir(new File(root));
fs.setIncludes(glob);
DirectoryScanner ds = fs.getDirectoryScanner(project);
Arrays.stream(ds.getIncludedFiles()).map(path -> Paths.get(root, path).toString()).forEach(queue::add);
}
}
if (queue.isEmpty()) {
error("given pattern didn't match any files");
return;
}
Vertx vertx = new Vertx(this.vertx);
GeoRocketClient client = createClient();
int queueSize = queue.size();
doImport(queue, client, vertx, exitCode -> {
client.close();
if (exitCode == 0) {
String m = "file";
if (queueSize > 1) {
m += "s";
}
System.out.println("Successfully imported " + queueSize + " " + m + " in " + DurationFormat.formatUntilNow(start));
}
handler.handle(exitCode);
});
}
use of org.apache.tools.ant.types.FileSet in project dependency-check-plugin by jenkinsci.
the class DependencyCheckExecutor method executeDependencyCheck.
/**
* Executes the Dependency-Check on the dependent libraries.
*
* @return the Engine used to scan the dependencies.
*/
private Engine executeDependencyCheck() throws DatabaseException, UpdateException, ExceptionCollection {
populateSettings();
Engine engine;
if (classLoader != null) {
engine = new Engine(classLoader, settings);
} else {
engine = new Engine(settings);
}
if (options.isUpdateOnly()) {
log(Messages.Executor_Update_Only());
engine.doUpdates();
} else {
for (String scanPath : options.getScanPath()) {
if (new File(scanPath).exists()) {
log(Messages.Executor_Scanning() + " " + scanPath);
engine.scan(scanPath);
} else {
// Scan path does not exist. Check for Ant style pattern sets.
final File baseDir = new File(options.getWorkspace());
// Remove the workspace path from the scan path so FileSet can assume
// the specified path is a patternset that defines includes.
final String includes = scanPath.replace(options.getWorkspace() + File.separator, "");
final FileSet fileSet = Util.createFileSet(baseDir, includes, null);
final Iterator filePathIter = fileSet.iterator();
while (filePathIter.hasNext()) {
final FilePath foundFilePath = new FilePath(new FilePath(baseDir), filePathIter.next().toString());
log(Messages.Executor_Scanning() + " " + foundFilePath.getRemote());
engine.scan(foundFilePath.getRemote());
}
}
}
log(Messages.Executor_Analyzing_Dependencies());
engine.analyzeDependencies();
}
return engine;
}
use of org.apache.tools.ant.types.FileSet in project ant by apache.
the class Javadoc method addSourceFiles.
/**
* Add the files matched by the nested source files to the Vector
* as SourceFile instances.
*
* @since 1.7
*/
private void addSourceFiles(final List<SourceFile> sf) {
for (ResourceCollection rc : nestedSourceFiles) {
if (!rc.isFilesystemOnly()) {
throw new BuildException("only file system based resources are supported by javadoc");
}
if (rc instanceof FileSet) {
final FileSet fs = (FileSet) rc;
if (!fs.hasPatterns() && !fs.hasSelectors()) {
final FileSet fs2 = (FileSet) fs.clone();
fs2.createInclude().setName("**/*.java");
if (includeNoSourcePackages) {
fs2.createInclude().setName("**/package.html");
}
rc = fs2;
}
}
for (final Resource r : rc) {
sf.add(new SourceFile(r.as(FileProvider.class).getFile()));
}
}
}
use of org.apache.tools.ant.types.FileSet in project ant by apache.
the class Delete method execute.
/**
* Delete the file(s).
*
* @exception BuildException if an error occurs
*/
@Override
public void execute() throws BuildException {
if (usedMatchingTask) {
log("DEPRECATED - Use of the implicit FileSet is deprecated. Use a nested fileset element instead.", quiet ? Project.MSG_VERBOSE : verbosity);
}
if (file == null && dir == null && filesets.isEmpty() && rcs == null) {
throw new BuildException("At least one of the file or dir attributes, or a nested resource collection, must be set.");
}
if (quiet && failonerror) {
throw new BuildException("quiet and failonerror cannot both be set to true", getLocation());
}
// delete the single file
if (file != null) {
if (file.exists()) {
if (file.isDirectory()) {
log("Directory " + file.getAbsolutePath() + " cannot be removed using the file attribute. Use dir instead.", quiet ? Project.MSG_VERBOSE : verbosity);
} else {
log("Deleting: " + file.getAbsolutePath());
if (!delete(file)) {
handle("Unable to delete file " + file.getAbsolutePath());
}
}
} else if (isDanglingSymlink(file)) {
log("Trying to delete file " + file.getAbsolutePath() + " which looks like a broken symlink.", quiet ? Project.MSG_VERBOSE : verbosity);
if (!delete(file)) {
handle("Unable to delete file " + file.getAbsolutePath());
}
} else {
log("Could not find file " + file.getAbsolutePath() + " to delete.", quiet ? Project.MSG_VERBOSE : verbosity);
}
}
// delete the directory
if (dir != null && !usedMatchingTask) {
if (dir.exists() && dir.isDirectory()) {
/*
If verbosity is MSG_VERBOSE, that mean we are doing
regular logging (backwards as that sounds). In that
case, we want to print one message about deleting the
top of the directory tree. Otherwise, the removeDir
method will handle messages for _all_ directories.
*/
if (verbosity == Project.MSG_VERBOSE) {
log("Deleting directory " + dir.getAbsolutePath());
}
removeDir(dir);
} else if (isDanglingSymlink(dir)) {
log("Trying to delete directory " + dir.getAbsolutePath() + " which looks like a broken symlink.", quiet ? Project.MSG_VERBOSE : verbosity);
if (!delete(dir)) {
handle("Unable to delete directory " + dir.getAbsolutePath());
}
}
}
Resources resourcesToDelete = new Resources();
resourcesToDelete.setProject(getProject());
resourcesToDelete.setCache(true);
Resources filesetDirs = new Resources();
filesetDirs.setProject(getProject());
filesetDirs.setCache(true);
FileSet implicit = null;
if (usedMatchingTask && dir != null && dir.isDirectory()) {
// add the files from the default fileset:
implicit = getImplicitFileSet();
implicit.setProject(getProject());
filesets.add(implicit);
}
final int size = filesets.size();
for (FileSet fs : filesets) {
if (fs.getProject() == null) {
log("Deleting fileset with no project specified; assuming executing project", Project.MSG_VERBOSE);
fs = (FileSet) fs.clone();
fs.setProject(getProject());
}
final File fsDir = fs.getDir();
if (!fs.getErrorOnMissingDir() && (fsDir == null || !fsDir.exists())) {
continue;
}
if (fsDir == null) {
throw new BuildException("File or Resource without directory or file specified");
} else if (!fsDir.isDirectory()) {
handle("Directory does not exist: " + fsDir);
} else {
DirectoryScanner ds = fs.getDirectoryScanner();
// the previous line has already scanned the
// filesystem, in order to avoid a rescan when later
// iterating, capture the results now and store them
final String[] files = ds.getIncludedFiles();
resourcesToDelete.add(new ResourceCollection() {
@Override
public boolean isFilesystemOnly() {
return true;
}
@Override
public int size() {
return files.length;
}
@Override
public Iterator<Resource> iterator() {
return new FileResourceIterator(getProject(), fsDir, files);
}
});
if (includeEmpty) {
filesetDirs.add(new ReverseDirs(getProject(), fsDir, ds.getIncludedDirectories()));
}
if (removeNotFollowedSymlinks) {
String[] n = ds.getNotFollowedSymlinks();
if (n.length > 0) {
String[] links = new String[n.length];
System.arraycopy(n, 0, links, 0, n.length);
Arrays.sort(links, Comparator.reverseOrder());
for (String link : links) {
final Path filePath = Paths.get(link);
if (!Files.isSymbolicLink(filePath)) {
// it's not a symbolic link, so move on
continue;
}
// it's a symbolic link, so delete it
final boolean deleted = filePath.toFile().delete();
if (!deleted) {
handle("Could not delete symbolic link at " + filePath);
}
}
}
}
}
}
resourcesToDelete.add(filesetDirs);
if (rcs != null) {
// sort first to files, then dirs
Restrict exists = new Restrict();
exists.add(EXISTS);
exists.add(rcs);
Sort s = new Sort();
s.add(REVERSE_FILESYSTEM);
s.add(exists);
resourcesToDelete.add(s);
}
try {
if (resourcesToDelete.isFilesystemOnly()) {
for (Resource r : resourcesToDelete) {
// nonexistent resources could only occur if we already
// deleted something from a fileset:
File f = r.as(FileProvider.class).getFile();
if (!f.exists()) {
continue;
}
if (!f.isDirectory() || f.list().length == 0) {
log("Deleting " + f, verbosity);
if (!delete(f) && failonerror) {
handle("Unable to delete " + (f.isDirectory() ? "directory " : "file ") + f);
}
}
}
} else {
handle(getTaskName() + " handles only filesystem resources");
}
} catch (Exception e) {
handle(e);
} finally {
if (implicit != null) {
filesets.remove(implicit);
}
}
}
Aggregations