Search in sources :

Example 11 with FileNameMapper

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

the class SignJar method execute.

/**
 * sign the jar(s)
 *
 * @throws BuildException on errors
 */
@Override
public void execute() throws BuildException {
    // validation logic
    final boolean hasJar = jar != null;
    final boolean hasSignedJar = signedjar != null;
    final boolean hasDestDir = destDir != null;
    final boolean hasMapper = mapper != null;
    if (!hasJar && !hasResources()) {
        throw new BuildException(ERROR_NO_SOURCE);
    }
    if (null == alias) {
        throw new BuildException(ERROR_NO_ALIAS);
    }
    if (null == storepass) {
        throw new BuildException(ERROR_NO_STOREPASS);
    }
    if (hasDestDir && hasSignedJar) {
        throw new BuildException(ERROR_TODIR_AND_SIGNEDJAR);
    }
    if (hasResources() && hasSignedJar) {
        throw new BuildException(ERROR_SIGNEDJAR_AND_PATHS);
    }
    // we can change implementation details later
    if (!hasDestDir && hasMapper) {
        throw new BuildException(ERROR_MAPPER_WITHOUT_DEST);
    }
    beginExecution();
    try {
        // special case single jar handling with signedjar attribute set
        if (hasJar && hasSignedJar) {
            // single jar processing
            signOneJar(jar, signedjar);
            // return here.
            return;
        }
        // the rest of the method treats single jar like
        // a nested path with one file
        Path sources = createUnifiedSourcePath();
        // set up our mapping policy
        FileNameMapper destMapper = hasMapper ? mapper : new IdentityMapper();
        // deal with the paths
        for (Resource r : sources) {
            FileResource fr = ResourceUtils.asFileResource(r.as(FileProvider.class));
            // calculate our destination directory; it is either the destDir
            // attribute, or the base dir of the fileset (for in situ updates)
            File toDir = hasDestDir ? destDir : fr.getBaseDir();
            // determine the destination filename via the mapper
            String[] destFilenames = destMapper.mapFileName(fr.getName());
            if (destFilenames == null || destFilenames.length != 1) {
                // we only like simple mappers.
                throw new BuildException(ERROR_BAD_MAP + fr.getFile());
            }
            File destFile = new File(toDir, destFilenames[0]);
            signOneJar(fr.getFile(), destFile);
        }
    } finally {
        endExecution();
    }
}
Also used : Path(org.apache.tools.ant.types.Path) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) FileProvider(org.apache.tools.ant.types.resources.FileProvider) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) FileResource(org.apache.tools.ant.types.resources.FileResource) BuildException(org.apache.tools.ant.BuildException) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) File(java.io.File)

Example 12 with FileNameMapper

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

the class Get method execute.

/**
 * Does the work.
 *
 * @exception BuildException Thrown in unrecoverable error.
 */
@Override
public void execute() throws BuildException {
    checkAttributes();
    for (final Resource r : sources) {
        final URLProvider up = r.as(URLProvider.class);
        final URL source = up.getURL();
        File dest = destination;
        if (destination.isDirectory()) {
            if (mapperElement == null) {
                String path = source.getPath();
                if (path.endsWith("/")) {
                    path = path.substring(0, path.length() - 1);
                }
                final int slash = path.lastIndexOf('/');
                if (slash > -1) {
                    path = path.substring(slash + 1);
                }
                dest = new File(destination, path);
            } else {
                final FileNameMapper mapper = mapperElement.getImplementation();
                final String[] d = mapper.mapFileName(source.toString());
                if (d == null) {
                    log("skipping " + r + " - mapper can't handle it", Project.MSG_WARN);
                    continue;
                }
                if (d.length == 0) {
                    log("skipping " + r + " - mapper returns no file name", Project.MSG_WARN);
                    continue;
                }
                if (d.length > 1) {
                    log("skipping " + r + " - mapper returns multiple file" + " names", Project.MSG_WARN);
                    continue;
                }
                dest = new File(destination, d[0]);
            }
        }
        // set up logging
        final int logLevel = Project.MSG_INFO;
        DownloadProgress progress = null;
        if (verbose) {
            progress = new VerboseProgress(System.out);
        }
        // execute the get
        try {
            doGet(source, dest, logLevel, progress);
        } catch (final IOException ioe) {
            log("Error getting " + source + " to " + dest);
            if (!ignoreErrors) {
                throw new BuildException(ioe, getLocation());
            }
        }
    }
}
Also used : URLProvider(org.apache.tools.ant.types.resources.URLProvider) Resource(org.apache.tools.ant.types.Resource) URLResource(org.apache.tools.ant.types.resources.URLResource) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) URL(java.net.URL)

Example 13 with FileNameMapper

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

the class Rmic method scanDir.

/**
 * Scans the directory looking for class files to be compiled.
 * The result is returned in the class variable compileList.
 * @param baseDir the base direction
 * @param files   the list of files to scan
 * @param mapper  the mapper of files to target files
 */
protected void scanDir(File baseDir, String[] files, FileNameMapper mapper) {
    String[] newFiles = files;
    if (idl) {
        log("will leave uptodate test to rmic implementation in idl mode.", Project.MSG_VERBOSE);
    } else if (iiop && iiopOpts != null && iiopOpts.contains("-always")) {
        log("no uptodate test as -always option has been specified", Project.MSG_VERBOSE);
    } else {
        SourceFileScanner sfs = new SourceFileScanner(this);
        newFiles = sfs.restrict(files, baseDir, getOutputDir(), mapper);
    }
    Stream.of(newFiles).map(s -> s.replace(File.separatorChar, '.')).map(s -> s.substring(0, s.lastIndexOf(".class"))).forEach(compileList::add);
}
Also used : FileUtils(org.apache.tools.ant.util.FileUtils) Remote(java.rmi.Remote) AntClassLoader(org.apache.tools.ant.AntClassLoader) FilterSetCollection(org.apache.tools.ant.types.FilterSetCollection) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) RmicAdapterFactory(org.apache.tools.ant.taskdefs.rmic.RmicAdapterFactory) FacadeTaskHelper(org.apache.tools.ant.util.facade.FacadeTaskHelper) RmicAdapter(org.apache.tools.ant.taskdefs.rmic.RmicAdapter) File(java.io.File) Path(org.apache.tools.ant.types.Path) SourceFileScanner(org.apache.tools.ant.util.SourceFileScanner) Vector(java.util.Vector) Stream(java.util.stream.Stream) StringUtils(org.apache.tools.ant.util.StringUtils) Project(org.apache.tools.ant.Project) Reference(org.apache.tools.ant.types.Reference) SourceFileScanner(org.apache.tools.ant.util.SourceFileScanner)

Example 14 with FileNameMapper

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

the class PathConvert method add.

/**
 * Add a nested filenamemapper.
 * @param fileNameMapper the mapper to add.
 * @since Ant 1.6.3
 */
public void add(FileNameMapper fileNameMapper) {
    Mapper m = new Mapper(getProject());
    m.add(fileNameMapper);
    addMapper(m);
}
Also used : Mapper(org.apache.tools.ant.types.Mapper) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) IdentityMapper(org.apache.tools.ant.util.IdentityMapper)

Example 15 with FileNameMapper

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

the class Image method execute.

/**
 * Executes the Task.
 * @throws BuildException on error.
 */
@Override
public void execute() throws BuildException {
    validateAttributes();
    try {
        File dest = (destDir != null) ? destDir : srcDir;
        int writeCount = 0;
        // build mapper
        final FileNameMapper mapper = mapperElement == null ? new IdentityMapper() : mapperElement.getImplementation();
        // deal with specified srcDir
        if (srcDir != null) {
            writeCount += processDir(srcDir, super.getDirectoryScanner(srcDir).getIncludedFiles(), dest, mapper);
        }
        // deal with the filesets
        for (FileSet fs : filesets) {
            writeCount += processDir(fs.getDir(getProject()), fs.getDirectoryScanner(getProject()).getIncludedFiles(), dest, mapper);
        }
        if (writeCount > 0) {
            log("Processed " + writeCount + (writeCount == 1 ? " image." : " images."));
        }
    } catch (Exception err) {
        log(StringUtils.getStackTrace(err), Project.MSG_ERR);
        throw new BuildException(err.getMessage());
    }
}
Also used : IdentityMapper(org.apache.tools.ant.util.IdentityMapper) FileSet(org.apache.tools.ant.types.FileSet) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Aggregations

FileNameMapper (org.apache.tools.ant.util.FileNameMapper)24 BuildException (org.apache.tools.ant.BuildException)16 File (java.io.File)9 IdentityMapper (org.apache.tools.ant.util.IdentityMapper)8 Resource (org.apache.tools.ant.types.Resource)6 IOException (java.io.IOException)5 Project (org.apache.tools.ant.Project)5 Mapper (org.apache.tools.ant.types.Mapper)5 FlatFileNameMapper (org.apache.tools.ant.util.FlatFileNameMapper)5 MergingMapper (org.apache.tools.ant.util.MergingMapper)5 List (java.util.List)4 GlobPatternMapper (org.apache.tools.ant.util.GlobPatternMapper)4 SourceFileScanner (org.apache.tools.ant.util.SourceFileScanner)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Stream (java.util.stream.Stream)3 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)3 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)3 FileResource (org.apache.tools.ant.types.resources.FileResource)3 HashSet (java.util.HashSet)2