Search in sources :

Example 1 with FileUtils

use of org.apache.tools.ant.util.FileUtils in project ant by apache.

the class ManifestClassPath method execute.

/**
 * Sets a property, which must not already exist, with a space
 * separated list of files and directories relative to the jar
 * file's parent directory.
 */
@Override
public void execute() {
    if (name == null) {
        throw new BuildException("Missing 'property' attribute!");
    }
    if (dir == null) {
        throw new BuildException("Missing 'jarfile' attribute!");
    }
    if (getProject().getProperty(name) != null) {
        throw new BuildException("Property '%s' already set!", name);
    }
    if (path == null) {
        throw new BuildException("Missing nested <classpath>!");
    }
    StringBuilder tooLongSb = new StringBuilder();
    for (int i = 0; i < maxParentLevels + 1; i++) {
        tooLongSb.append("../");
    }
    final String tooLongPrefix = tooLongSb.toString();
    // Normalize the reference directory (containing the jar)
    final FileUtils fileUtils = FileUtils.getFileUtils();
    dir = fileUtils.normalize(dir.getAbsolutePath());
    String[] elements = path.list();
    StringBuilder buffer = new StringBuilder();
    for (String element : elements) {
        // Normalize the current file
        File pathEntry = new File(element);
        String fullPath = pathEntry.getAbsolutePath();
        pathEntry = fileUtils.normalize(fullPath);
        String relPath = null;
        String canonicalPath = null;
        try {
            if (dir.equals(pathEntry)) {
                relPath = ".";
            } else {
                relPath = FileUtils.getRelativePath(dir, pathEntry);
            }
            canonicalPath = pathEntry.getCanonicalPath();
            // getRelativePath always uses '/' as separator, adapt
            if (File.separatorChar != '/') {
                canonicalPath = canonicalPath.replace(File.separatorChar, '/');
            }
        } catch (Exception e) {
            throw new BuildException("error trying to get the relative path" + " from " + dir + " to " + fullPath, e);
        }
        // No match, so bail out!
        if (relPath.equals(canonicalPath) || relPath.startsWith(tooLongPrefix)) {
            throw new BuildException("No suitable relative path from %s to %s", dir, fullPath);
        }
        if (pathEntry.isDirectory() && !relPath.endsWith("/")) {
            relPath = relPath + '/';
        }
        relPath = Locator.encodeURI(relPath);
        // Manifest's ClassPath: attribute always uses forward
        // slashes '/', and is space-separated. Ant will properly
        // format it on 72 columns with proper line continuation
        buffer.append(relPath);
        buffer.append(' ');
    }
    // Finally assign the property with the manifest classpath
    getProject().setNewProperty(name, buffer.toString().trim());
}
Also used : FileUtils(org.apache.tools.ant.util.FileUtils) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) BuildException(org.apache.tools.ant.BuildException)

Example 2 with FileUtils

use of org.apache.tools.ant.util.FileUtils in project ant by apache.

the class PropertyFileCLITest method testPropertyResolution.

@Test
public void testPropertyResolution() throws Exception {
    FileUtils fu = FileUtils.getFileUtils();
    File props = fu.createTempFile("propertyfilecli", ".properties", null, true, true);
    File build = fu.createTempFile("propertyfilecli", ".xml", null, true, true);
    File log = fu.createTempFile("propertyfilecli", ".log", null, true, true);
    FileWriter fw = null;
    FileReader fr = null;
    try {
        fw = new FileWriter(props);
        fw.write("w=world\nmessage=Hello, ${w}\n");
        fw.close();
        fw = new FileWriter(build);
        fw.write("<project><echo>${message}</echo></project>");
        fw.close();
        fw = null;
        Main m = new NoExitMain();
        m.startAnt(new String[] { "-propertyfile", props.getAbsolutePath(), "-f", build.getAbsolutePath(), "-l", log.getAbsolutePath() }, null, null);
        String l = FileUtils.safeReadFully(fr = new FileReader(log));
        assertContains("Hello, world", l);
    } finally {
        FileUtils.close(fw);
        FileUtils.close(fr);
    }
}
Also used : FileUtils(org.apache.tools.ant.util.FileUtils) FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

Example 3 with FileUtils

use of org.apache.tools.ant.util.FileUtils in project ant by apache.

the class Javac method collectFileListFromModulePath.

private void collectFileListFromModulePath() {
    final FileUtils fu = FileUtils.getFileUtils();
    for (String pathElement : moduleSourcepath.list()) {
        boolean valid = false;
        for (Map.Entry<String, Collection<File>> modules : resolveModuleSourcePathElement(getProject().getBaseDir(), pathElement).entrySet()) {
            final String moduleName = modules.getKey();
            for (File srcDir : modules.getValue()) {
                if (srcDir.exists()) {
                    valid = true;
                    final DirectoryScanner ds = getDirectoryScanner(srcDir);
                    final String[] files = ds.getIncludedFiles();
                    scanDir(srcDir, fu.resolveFile(destDir, moduleName), files);
                }
            }
        }
        if (!valid) {
            throw new BuildException("modulesourcepath \"" + pathElement + "\" does not exist!", getLocation());
        }
    }
}
Also used : FileUtils(org.apache.tools.ant.util.FileUtils) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) Collection(java.util.Collection) BuildException(org.apache.tools.ant.BuildException) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) File(java.io.File)

Example 4 with FileUtils

use of org.apache.tools.ant.util.FileUtils in project ant by apache.

the class MessageTest method tearDown.

@After
public void tearDown() {
    if (f.exists()) {
        FileUtils fu = FileUtils.getFileUtils();
        fu.tryHardToDelete(f);
    }
}
Also used : FileUtils(org.apache.tools.ant.util.FileUtils) After(org.junit.After)

Example 5 with FileUtils

use of org.apache.tools.ant.util.FileUtils in project ant by apache.

the class Javac method findModules.

/**
 * Finds modules in the expanded modulesourcepath entry.
 * @param root the project root
 * @param pathToModule the path to modules folder
 * @param pathInModule the path in module to source folder
 * @param collector the map to put modules into
 * @since 1.9.7
 */
private static void findModules(final File root, final String pathToModule, final String pathInModule, final Map<String, Collection<File>> collector) {
    final FileUtils fu = FileUtils.getFileUtils();
    final File f = fu.resolveFile(root, pathToModule);
    if (!f.isDirectory()) {
        return;
    }
    for (File module : f.listFiles(File::isDirectory)) {
        final String moduleName = module.getName();
        final File moduleSourceRoot = pathInModule == null ? module : new File(module, pathInModule);
        Collection<File> moduleRoots = collector.get(moduleName);
        if (moduleRoots == null) {
            moduleRoots = new ArrayList<>();
            collector.put(moduleName, moduleRoots);
        }
        moduleRoots.add(moduleSourceRoot);
    }
}
Also used : FileUtils(org.apache.tools.ant.util.FileUtils) File(java.io.File)

Aggregations

FileUtils (org.apache.tools.ant.util.FileUtils)6 File (java.io.File)5 BuildException (org.apache.tools.ant.BuildException)3 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 Resource (org.apache.tools.ant.types.Resource)1 FileResource (org.apache.tools.ant.types.resources.FileResource)1 After (org.junit.After)1 Test (org.junit.Test)1