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();
}
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);
}
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());
}
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"));
}
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;
}
Aggregations