in src/main/org/apache/tools/ant/taskdefs/Tar.java [260:353]
public void execute() throws BuildException {
if (tarFile == null) {
throw new BuildException("tarfile attribute must be set!",
getLocation());
}
if (tarFile.exists() && tarFile.isDirectory()) {
throw new BuildException("tarfile is a directory!",
getLocation());
}
if (tarFile.exists() && !tarFile.canWrite()) {
throw new BuildException("Can not write to the specified tarfile!",
getLocation());
}
final Vector<TarFileSet> savedFileSets = new Vector<>(filesets);
try {
if (baseDir != null) {
if (!baseDir.exists()) {
throw new BuildException("basedir does not exist!",
getLocation());
}
// add the main fileset to the list of filesets to process.
final TarFileSet mainFileSet = new TarFileSet(fileset);
mainFileSet.setDir(baseDir);
filesets.addElement(mainFileSet);
}
if (filesets.isEmpty() && resourceCollections.isEmpty()) {
throw new BuildException(
"You must supply either a basedir attribute or some nested resource collections.",
getLocation());
}
// check if tar is out of date with respect to each
// fileset
boolean upToDate = true;
for (final TarFileSet tfs : filesets) {
upToDate &= check(tfs);
}
for (final ResourceCollection rcol : resourceCollections) {
upToDate &= check(rcol);
}
if (upToDate) {
log("Nothing to do: " + tarFile.getAbsolutePath()
+ " is up to date.", Project.MSG_INFO);
return;
}
final File parent = tarFile.getParentFile();
if (parent != null && !parent.isDirectory()
&& !(parent.mkdirs() || parent.isDirectory())) {
throw new BuildException(
"Failed to create missing parent directory for %s",
tarFile);
}
log("Building tar: " + tarFile.getAbsolutePath(), Project.MSG_INFO);
try (TarOutputStream tOut = new TarOutputStream(
compression.compress(new BufferedOutputStream(
Files.newOutputStream(tarFile.toPath()))),
encoding)) {
tOut.setDebug(true);
if (longFileMode.isTruncateMode()) {
tOut.setLongFileMode(TarOutputStream.LONGFILE_TRUNCATE);
} else if (longFileMode.isFailMode()
|| longFileMode.isOmitMode()) {
tOut.setLongFileMode(TarOutputStream.LONGFILE_ERROR);
} else if (longFileMode.isPosixMode()) {
tOut.setLongFileMode(TarOutputStream.LONGFILE_POSIX);
} else {
// warn or GNU
tOut.setLongFileMode(TarOutputStream.LONGFILE_GNU);
}
longWarningGiven = false;
for (final TarFileSet tfs : filesets) {
tar(tfs, tOut);
}
for (final ResourceCollection rcol : resourceCollections) {
tar(rcol, tOut);
}
} catch (final IOException ioe) {
final String msg = "Problem creating TAR: " + ioe.getMessage();
throw new BuildException(msg, ioe, getLocation());
}
} finally {
filesets = savedFileSets;
}
}