public File archive(String sourcePath, String destPath, Artifact artifact) throws ArchiverException, IOException {
File source = serverInfo.resolve(sourcePath);
File dest = serverInfo.resolve(destPath);
String serverName = artifact.getArtifactId() + "-" + artifact.getVersion();
dest = new File(dest, serverName + "-bin." + artifact.getType());
Archiver archiver;
if ("tar.gz".equals(artifact.getType())) {
archiver = new TarArchiver();
TarArchiver.TarCompressionMethod tarCompressionMethod = new TarArchiver.TarCompressionMethod();
tarCompressionMethod.setValue("gzip");
((TarArchiver) archiver).setCompression(tarCompressionMethod);
TarLongFileMode fileMode = new TarLongFileMode();
fileMode.setValue(TarLongFileMode.GNU);
((TarArchiver) archiver).setLongfile(fileMode);
} else if ("zip".equals(artifact.getType())) {
archiver = new ZipArchiver();
} else {
throw new IllegalArgumentException("Unknown target type: " + artifact.getType());
}
archiver.setIncludeEmptyDirs(true);
archiver.setDestFile(dest);
/* see if using plexus-archiver 1.0-alpha-7 same as maven lets us share code. Following is for 1.0-alpha-9
DefaultFileSet all = new DefaultFileSet();
all.setDirectory(source);
archiver.addFileSet(all);
*/
// add in all files and mark them with default file permissions
Map<File, Boolean> emptyDirs = new HashMap<File, Boolean>();
Map<String, File> all = IOUtil.listAllFileNames(source);
removeExcludes(source, all);
for (Map.Entry<String, File> entry : all.entrySet()) {
String destFileName = serverName + "/" + entry.getKey();
File sourceFile = entry.getValue();
if (sourceFile.isFile()) {
archiver.addFile(sourceFile, destFileName, UnixStat.DEFAULT_FILE_PERM);
// mark parent directories non-empty
for (File parentDir = sourceFile.getParentFile();
parentDir != null && !parentDir.equals(source);
parentDir = parentDir.getParentFile()) {
emptyDirs.put(parentDir, Boolean.FALSE);
}
} else if (sourceFile.isDirectory()) {
Boolean isEmpty = emptyDirs.get(sourceFile);
if (isEmpty == null) {
emptyDirs.put(sourceFile, Boolean.TRUE);
// mark parent directories non-empty
for (File parentDir = sourceFile.getParentFile();
parentDir != null && !parentDir.equals(source);
parentDir = parentDir.getParentFile()) {
emptyDirs.put(parentDir, Boolean.FALSE);
}
}
}
}
if (!all.isEmpty()) {
emptyDirs.put(source, Boolean.FALSE);
}
String sourceDirPath = source.getAbsolutePath();
for (Map.Entry<File, Boolean> entry : emptyDirs.entrySet()) {
if (entry.getValue().booleanValue()) {
String emptyDirPath = entry.getKey().getAbsolutePath();
String relativeDir = emptyDirPath.substring(sourceDirPath.length());
relativeDir = relativeDir.replace('\\', '/');
archiver.addDirectory(entry.getKey(), serverName + relativeDir);
}
}
emptyDirs.clear();
all.clear();
// add execute permissions to all non-batch files in the bin/ directory
File bin = new File(source, "bin");
if (bin.exists()) {
Map<String, File> includes = IOUtil.listAllFileNames(bin);
for (Map.Entry<String, File> entry : includes.entrySet()) {
String destFileName = serverName + "/bin/" + entry.getKey();
File sourceFile = entry.getValue();
if (!destFileName.endsWith(".bat") && sourceFile.isFile()) {
archiver.addFile(sourceFile, destFileName, UnixStat.DEFAULT_DIR_PERM);
}
}
}
archiver.createArchive();
return dest;
}