Search in sources :

Example 16 with FileSet

use of org.apache.tools.ant.types.FileSet in project gate-core by GateNLP.

the class PackageGappTask method copyDirectories.

/**
 * Copy directories as specified by the given map.
 *
 * @param copyMap map specifying the directories to copy and the
 *          target locations to which they should be copied.
 * @param minimalPlugin if true, treat the directory as a GATE plugin
 *          and copy just the minimal files needed for the plugin to
 *          work (creole.xml and any referenced jars).
 */
private void copyDirectories(Map<URL, URL> copyMap, boolean minimalPlugin) {
    for (Map.Entry<URL, URL> copyEntry : copyMap.entrySet()) {
        File source = Files.fileFromURL(copyEntry.getKey());
        if (!source.exists()) {
            return;
        }
        File dest = Files.fileFromURL(copyEntry.getValue());
        // set up a copy task to do the copying
        Copy copyTask = new Copy();
        copyTask.setProject(getProject());
        copyTask.setLocation(getLocation());
        copyTask.setTaskName(getTaskName());
        copyTask.setTodir(dest);
        // ensure the target directory exists
        dest.mkdirs();
        FileSet fileSet = new FileSet();
        copyTask.addFileset(fileSet);
        fileSet.setDir(source);
        if (minimalPlugin) {
            // just copy creole.xml and JARs
            NameEntry include = fileSet.createInclude();
            include.setName("creole.xml");
            URL creoleXml;
            try {
                creoleXml = new URL(copyEntry.getKey().toExternalForm() + "/creole.xml");
            } catch (MalformedURLException e) {
                throw new BuildException("Error creating URL for creole.xml in plugin " + copyEntry.getKey());
            }
            for (String jarString : getJars(creoleXml)) {
                NameEntry jarInclude = fileSet.createInclude();
                jarInclude.setName(jarString);
            }
        }
        // do the copying
        copyTask.init();
        copyTask.perform();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) FileSet(org.apache.tools.ant.types.FileSet) Copy(org.apache.tools.ant.taskdefs.Copy) NameEntry(org.apache.tools.ant.types.PatternSet.NameEntry) BuildException(org.apache.tools.ant.BuildException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) File(java.io.File) URL(java.net.URL)

Example 17 with FileSet

use of org.apache.tools.ant.types.FileSet in project jenkin-qtest-plugin by QASymphony.

the class AutoScanParser method parse.

/**
 * @param request request
 * @return list of {@link AutomationTestResult}
 * @throws Exception Exception
 */
@Override
public List<AutomationTestResult> parse(ParseRequest request) throws Exception {
    PrintStream logger = request.getListener().getLogger();
    LoggerUtils.formatInfo(logger, "Auto scan JUnit test results files.");
    Run<?, ?> build = request.getBuild();
    // otherwise auto scan test result
    String basedDir = request.getWorkSpace().toURI().getPath();
    List<String> resultFolders = CommonParsingUtils.scanJunitTestResultFolder(basedDir);
    LOG.info("Scanning junit test result in dir:" + basedDir + String.format(".Found: %s dirs, %s", resultFolders.size(), resultFolders));
    List<AutomationTestResult> result = new LinkedList<>();
    if (request.isMavenProject() && resultFolders.size() <= 1) {
        // if pom file is located at workspace, we do not scan to detect junit result
        FileSet fs = Util.createFileSet(new File(basedDir), AutoScanParser.TEST_RESULT_LOCATIONS);
        DirectoryScanner ds = fs.getDirectoryScanner();
        if (ds.getIncludedFiles().length > 0)
            return parse(request, TEST_RESULT_LOCATIONS);
    } else if (request.isMavenProject()) {
        // if is maven project, we only scan surefire report folder
        for (String res : resultFolders) {
            if (res.contains(AutoScanParser.SUREFIRE_REPORT)) {
                try {
                    result.addAll(parse(request, res + Constants.JUNIT_SUFFIX));
                } catch (Exception e) {
                    LoggerUtils.formatWarn(logger, "Try to scan test result in: %s, error: %s", res, e.getMessage());
                }
            }
        }
        return result;
    }
    for (String res : resultFolders) {
        try {
            result.addAll(parse(request, res + Constants.JUNIT_SUFFIX));
        } catch (Exception e) {
            LoggerUtils.formatWarn(logger, "Try to scan test result in: %s, error: %s", res, e.getMessage());
        }
    }
    return result;
}
Also used : PrintStream(java.io.PrintStream) AutomationTestResult(com.qasymphony.ci.plugin.model.AutomationTestResult) FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) File(java.io.File) LinkedList(java.util.LinkedList)

Example 18 with FileSet

use of org.apache.tools.ant.types.FileSet in project ant by apache.

the class MakeUrl method filesetsToURL.

/**
 * convert the filesets to urls.
 *
 * @return null for no files
 */
private String filesetsToURL() {
    if (filesets.isEmpty()) {
        return "";
    }
    int count = 0;
    StringBuilder urls = new StringBuilder();
    for (FileSet fs : filesets) {
        DirectoryScanner scanner = fs.getDirectoryScanner(getProject());
        for (String file : scanner.getIncludedFiles()) {
            File f = new File(scanner.getBasedir(), file);
            validateFile(f);
            String asUrl = toURL(f);
            urls.append(asUrl);
            log(asUrl, Project.MSG_DEBUG);
            urls.append(separator);
            count++;
        }
    }
    // at this point there is one trailing space to remove, if the list is not empty.
    return stripTrailingSeparator(urls, count);
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) File(java.io.File)

Example 19 with FileSet

use of org.apache.tools.ant.types.FileSet in project ant by apache.

the class Move method doFileOperations.

// ************************************************************************
// protected and private methods
// ************************************************************************
/**
 * Override copy's doFileOperations to move the files instead of copying them.
 */
@Override
protected void doFileOperations() {
    // Attempt complete directory renames, if any, first.
    if (completeDirMap.size() > 0) {
        for (Map.Entry<File, File> entry : completeDirMap.entrySet()) {
            File fromDir = entry.getKey();
            File toDir = entry.getValue();
            boolean renamed = false;
            try {
                log("Attempting to rename dir: " + fromDir + " to " + toDir, verbosity);
                renamed = renameFile(fromDir, toDir, filtering, forceOverwrite);
            } catch (IOException ioe) {
                String msg = "Failed to rename dir " + fromDir + " to " + toDir + " due to " + ioe.getMessage();
                throw new BuildException(msg, ioe, getLocation());
            }
            if (!renamed) {
                FileSet fs = new FileSet();
                fs.setProject(getProject());
                fs.setDir(fromDir);
                addFileset(fs);
                DirectoryScanner ds = fs.getDirectoryScanner(getProject());
                String[] files = ds.getIncludedFiles();
                String[] dirs = ds.getIncludedDirectories();
                scan(fromDir, toDir, files, dirs);
            }
        }
    }
    int moveCount = fileCopyMap.size();
    if (moveCount > 0) {
        // files to move
        log("Moving " + moveCount + " file" + ((moveCount == 1) ? "" : "s") + " to " + destDir.getAbsolutePath());
        for (Map.Entry<String, String[]> entry : fileCopyMap.entrySet()) {
            String fromFile = entry.getKey();
            File f = new File(fromFile);
            boolean selfMove = false;
            if (f.exists()) {
                // Is this file still available to be moved?
                String[] toFiles = entry.getValue();
                for (int i = 0; i < toFiles.length; i++) {
                    String toFile = toFiles[i];
                    if (fromFile.equals(toFile)) {
                        log("Skipping self-move of " + fromFile, verbosity);
                        selfMove = true;
                        // move will not occur, but that's what we want
                        continue;
                    }
                    File d = new File(toFile);
                    if ((i + 1) == toFiles.length && !selfMove) {
                        // Only try to move if this is the last mapped file
                        // and one of the mappings isn't to itself
                        moveFile(f, d, filtering, forceOverwrite);
                    } else {
                        copyFile(f, d, filtering, forceOverwrite);
                    }
                }
            }
        }
    }
    if (includeEmpty) {
        int createCount = 0;
        for (Map.Entry<String, String[]> entry : dirCopyMap.entrySet()) {
            String fromDirName = entry.getKey();
            boolean selfMove = false;
            for (String toDirName : entry.getValue()) {
                if (fromDirName.equals(toDirName)) {
                    log("Skipping self-move of " + fromDirName, verbosity);
                    selfMove = true;
                    continue;
                }
                File d = new File(toDirName);
                if (!d.exists()) {
                    if (!d.mkdirs() && !d.exists()) {
                        log("Unable to create directory " + d.getAbsolutePath(), Project.MSG_ERR);
                    } else {
                        createCount++;
                    }
                }
            }
            File fromDir = new File(fromDirName);
            if (!selfMove && okToDelete(fromDir)) {
                deleteDir(fromDir);
            }
        }
        if (createCount > 0) {
            log("Moved " + dirCopyMap.size() + " empty director" + (dirCopyMap.size() == 1 ? "y" : "ies") + " to " + createCount + " empty director" + (createCount == 1 ? "y" : "ies") + " under " + destDir.getAbsolutePath());
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) Map(java.util.Map) File(java.io.File)

Example 20 with FileSet

use of org.apache.tools.ant.types.FileSet 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();
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) HashMap(java.util.HashMap) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) ArrayList(java.util.ArrayList) FileResource(org.apache.tools.ant.types.resources.FileResource) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileProvider(org.apache.tools.ant.types.resources.FileProvider) ArrayList(java.util.ArrayList) List(java.util.List) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) HashSet(java.util.HashSet)

Aggregations

FileSet (org.apache.tools.ant.types.FileSet)165 File (java.io.File)124 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)83 BuildException (org.apache.tools.ant.BuildException)49 Test (org.junit.Test)41 IOException (java.io.IOException)36 ArrayList (java.util.ArrayList)29 Project (org.apache.tools.ant.Project)22 Resource (org.apache.tools.ant.types.Resource)12 FileResource (org.apache.tools.ant.types.resources.FileResource)10 URL (java.net.URL)6 Hashtable (java.util.Hashtable)6 ArchiveFileSet (org.apache.tools.ant.types.ArchiveFileSet)6 Path (org.apache.tools.ant.types.Path)6 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)6 PrintStream (java.io.PrintStream)5 HashMap (java.util.HashMap)5 Iterator (java.util.Iterator)5 List (java.util.List)5 ZipFileSet (org.apache.tools.ant.types.ZipFileSet)5