Search in sources :

Example 1 with MergingMapper

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

the class UpToDate method getMapper.

private FileNameMapper getMapper() {
    if (mapperElement == null) {
        MergingMapper mm = new MergingMapper();
        mm.setTo(targetFile.getAbsolutePath());
        return mm;
    }
    return mapperElement.getImplementation();
}
Also used : MergingMapper(org.apache.tools.ant.util.MergingMapper)

Example 2 with MergingMapper

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

the class Zip method getResourcesToAdd.

/**
 * Collect the resources that are newer than the corresponding
 * entries (or missing) in the original archive.
 *
 * <p>If we are going to recreate the archive instead of updating
 * it, all resources should be considered as new, if a single one
 * is.  Because of this, subclasses overriding this method must
 * call <code>super.getResourcesToAdd</code> and indicate with the
 * third arg if they already know that the archive is
 * out-of-date.</p>
 *
 * @param filesets The filesets to grab resources from
 * @param zipFile intended archive file (may or may not exist)
 * @param needsUpdate whether we already know that the archive is
 * out-of-date.  Subclasses overriding this method are supposed to
 * set this value correctly in their call to
 * <code>super.getResourcesToAdd</code>.
 * @return an array of resources to add for each fileset passed in as well
 *         as a flag that indicates whether the archive is uptodate.
 *
 * @exception BuildException if it likes
 */
protected ArchiveState getResourcesToAdd(final FileSet[] filesets, final File zipFile, boolean needsUpdate) throws BuildException {
    final Resource[][] initialResources = grabResources(filesets);
    if (isEmpty(initialResources)) {
        if (Boolean.FALSE.equals(HAVE_NON_FILE_SET_RESOURCES_TO_ADD.get())) {
            if (needsUpdate && doUpdate) {
                /*
                     * This is a rather hairy case.
                     *
                     * One of our subclasses knows that we need to
                     * update the archive, but at the same time, there
                     * are no resources known to us that would need to
                     * be added.  Only the subclass seems to know
                     * what's going on.
                     *
                     * This happens if <jar> detects that the manifest
                     * has changed, for example.  The manifest is not
                     * part of any resources because of our support
                     * for inline <manifest>s.
                     *
                     * If we invoke createEmptyZip like Ant 1.5.2 did,
                     * we'll loose all stuff that has been in the
                     * original archive (bugzilla report 17780).
                     */
                return new ArchiveState(true, initialResources);
            }
            if ("skip".equals(emptyBehavior)) {
                if (doUpdate) {
                    logWhenWriting(archiveType + " archive " + zipFile + " not updated because no new files were" + " included.", Project.MSG_VERBOSE);
                } else {
                    logWhenWriting("Warning: skipping " + archiveType + " archive " + zipFile + " because no files were included.", Project.MSG_WARN);
                }
            } else if ("fail".equals(emptyBehavior)) {
                throw new BuildException("Cannot create " + archiveType + " archive " + zipFile + ": no files were included.", getLocation());
            } else {
                // Create.
                if (!zipFile.exists()) {
                    needsUpdate = true;
                }
            }
        }
        // (re-)create the archive anyway
        return new ArchiveState(needsUpdate, initialResources);
    }
    if (!zipFile.exists()) {
        return new ArchiveState(true, initialResources);
    }
    if (needsUpdate && !doUpdate) {
        // we are recreating the archive, need all resources
        return new ArchiveState(true, initialResources);
    }
    final Resource[][] newerResources = new Resource[filesets.length][];
    for (int i = 0; i < filesets.length; i++) {
        if (!(fileset instanceof ZipFileSet) || ((ZipFileSet) fileset).getSrc(getProject()) == null) {
            final File base = filesets[i].getDir(getProject());
            for (int j = 0; j < initialResources[i].length; j++) {
                final File resourceAsFile = FILE_UTILS.resolveFile(base, initialResources[i][j].getName());
                if (resourceAsFile.equals(zipFile)) {
                    throw new BuildException("A zip file cannot include " + "itself", getLocation());
                }
            }
        }
    }
    for (int i = 0; i < filesets.length; i++) {
        if (initialResources[i].length == 0) {
            newerResources[i] = new Resource[] {};
            continue;
        }
        FileNameMapper myMapper = new IdentityMapper();
        if (filesets[i] instanceof ZipFileSet) {
            final ZipFileSet zfs = (ZipFileSet) filesets[i];
            if (zfs.getFullpath(getProject()) != null && !zfs.getFullpath(getProject()).equals("")) {
                // in this case all files from origin map to
                // the fullPath attribute of the zipfileset at
                // destination
                final MergingMapper fm = new MergingMapper();
                fm.setTo(zfs.getFullpath(getProject()));
                myMapper = fm;
            } else if (zfs.getPrefix(getProject()) != null && !zfs.getPrefix(getProject()).equals("")) {
                final GlobPatternMapper gm = new GlobPatternMapper();
                gm.setFrom("*");
                String prefix = zfs.getPrefix(getProject());
                if (!prefix.endsWith("/") && !prefix.endsWith("\\")) {
                    prefix += "/";
                }
                gm.setTo(prefix + "*");
                myMapper = gm;
            }
        }
        newerResources[i] = selectOutOfDateResources(initialResources[i], myMapper);
        needsUpdate = needsUpdate || (newerResources[i].length > 0);
        if (needsUpdate && !doUpdate) {
            // to scan further.
            break;
        }
    }
    if (needsUpdate && !doUpdate) {
        // we are recreating the archive, need all resources
        return new ArchiveState(true, initialResources);
    }
    return new ArchiveState(needsUpdate, newerResources);
}
Also used : IdentityMapper(org.apache.tools.ant.util.IdentityMapper) GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) ZipResource(org.apache.tools.ant.types.resources.ZipResource) BuildException(org.apache.tools.ant.BuildException) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) MergingMapper(org.apache.tools.ant.util.MergingMapper)

Example 3 with MergingMapper

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

the class MappedResourceCollection method getCollection.

private Collection<Resource> getCollection() {
    FileNameMapper m = mapper == null ? new IdentityMapper() : mapper.getImplementation();
    Stream<MappedResource> stream;
    if (enableMultipleMappings) {
        stream = nested.stream().flatMap(r -> Stream.of(m.mapFileName(r.getName())).filter(Objects::nonNull).map(MergingMapper::new).map(mm -> new MappedResource(r, mm)));
    } else {
        stream = nested.stream().map(r -> new MappedResource(r, m));
    }
    return stream.collect(Collectors.toList());
}
Also used : DataType(org.apache.tools.ant.types.DataType) Mapper(org.apache.tools.ant.types.Mapper) Resource(org.apache.tools.ant.types.Resource) Iterator(java.util.Iterator) Collection(java.util.Collection) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) Collectors(java.util.stream.Collectors) File(java.io.File) Stack(java.util.Stack) Objects(java.util.Objects) MergingMapper(org.apache.tools.ant.util.MergingMapper) Stream(java.util.stream.Stream) Project(org.apache.tools.ant.Project) Reference(org.apache.tools.ant.types.Reference) IdentityMapper(org.apache.tools.ant.util.IdentityMapper) Objects(java.util.Objects) FileNameMapper(org.apache.tools.ant.util.FileNameMapper)

Example 4 with MergingMapper

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

the class MapperTest method testNested.

@Test
public void testNested() {
    Mapper mapper1 = new Mapper(project);
    Mapper.MapperType mt = new Mapper.MapperType();
    mt.setValue("glob");
    mapper1.setType(mt);
    mapper1.setFrom("from*");
    mapper1.setTo("to*");
    // mix element types
    FileNameMapper mapper2 = new FlatFileNameMapper();
    FileNameMapper mapper3 = new MergingMapper();
    mapper3.setTo("mergefile");
    Mapper container = new Mapper(project);
    container.addConfiguredMapper(mapper1);
    container.add(mapper2);
    container.add(mapper3);
    FileNameMapper fileNameMapper = container.getImplementation();
    String[] targets = fileNameMapper.mapFileName("fromfilename");
    assertNotNull("no filenames mapped", targets);
    assertEquals("wrong number of filenames mapped", 3, targets.length);
    List list = Arrays.asList(targets);
    assertTrue("cannot find expected target \"tofilename\"", list.contains("tofilename"));
    assertTrue("cannot find expected target \"fromfilename\"", list.contains("fromfilename"));
    assertTrue("cannot find expected target \"mergefile\"", list.contains("mergefile"));
}
Also used : GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper) MergingMapper(org.apache.tools.ant.util.MergingMapper) ChainedMapper(org.apache.tools.ant.util.ChainedMapper) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper) List(java.util.List) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) FlatFileNameMapper(org.apache.tools.ant.util.FlatFileNameMapper) MergingMapper(org.apache.tools.ant.util.MergingMapper) Test(org.junit.Test)

Example 5 with MergingMapper

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

the class Tar method archiveIsUpToDate.

/**
 * Is the archive up to date in relationship to a list of files.
 * @param files the files to check
 * @param dir   the base directory for the files.
 * @return true if the archive is up to date.
 * @since Ant 1.5.2
 */
protected boolean archiveIsUpToDate(final String[] files, final File dir) {
    final SourceFileScanner sfs = new SourceFileScanner(this);
    final MergingMapper mm = new MergingMapper();
    mm.setTo(tarFile.getAbsolutePath());
    return sfs.restrict(files, dir, null, mm).length == 0;
}
Also used : SourceFileScanner(org.apache.tools.ant.util.SourceFileScanner) MergingMapper(org.apache.tools.ant.util.MergingMapper)

Aggregations

MergingMapper (org.apache.tools.ant.util.MergingMapper)5 FileNameMapper (org.apache.tools.ant.util.FileNameMapper)3 File (java.io.File)2 BuildException (org.apache.tools.ant.BuildException)2 Resource (org.apache.tools.ant.types.Resource)2 GlobPatternMapper (org.apache.tools.ant.util.GlobPatternMapper)2 IdentityMapper (org.apache.tools.ant.util.IdentityMapper)2 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Objects (java.util.Objects)1 Stack (java.util.Stack)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Project (org.apache.tools.ant.Project)1 DataType (org.apache.tools.ant.types.DataType)1 Mapper (org.apache.tools.ant.types.Mapper)1 Reference (org.apache.tools.ant.types.Reference)1 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)1 ZipFileSet (org.apache.tools.ant.types.ZipFileSet)1