File sourcesFile = new File(destFile.getParentFile(), name);
if (sourcesFile.exists()) {
sourcesFile.delete();
}
Jar jar = new Jar();
jar.setTaskName(getTaskName());
jar.setProject(getProject());
// set the destination file
jar.setDestFile(sourcesFile);
// use the resolved classes to determine included source files
List<FileResource> sourceFiles = new ArrayList<FileResource>();
Map<File, Set<String>> packageResources = new HashMap<File, Set<String>>();
if (resolvedLocal.size() == 0) {
console.warn(getTaskName() + " has not resolved any class files local to {0}", build.getPom().getManagementId());
}
List<File> folders = build.getConfig().getSourceDirectories(Scope.compile, tag);
for (String className : resolvedLocal) {
String sourceName = className.substring(0, className.length() - ".class".length()).replace('.', '/') + ".java";
console.debug(sourceName);
for (File folder : folders) {
File file = new File(folder, sourceName);
if (file.exists()) {
FileResource resource = new FileResource(getProject(), file);
resource.setBaseDir(folder);
sourceFiles.add(resource);
if (!packageResources.containsKey(folder)) {
// always include default package resources
packageResources.put(folder, new TreeSet<String>(Arrays.asList( "/*" )));
}
String packagePath = FileUtils.getRelativePath(folder, file.getParentFile());
packageResources.get(folder).add(packagePath + "/*");
console.debug(1, file.getAbsolutePath());
break;
}
}
}
// add the discovered source files for the resolved classes
jar.add(new FileResourceSet(sourceFiles));
// add the resolved package folders for resource files
for (Map.Entry<File, Set<String>> entry : packageResources.entrySet()) {
FileSet res = new FileSet();
res.setDir(entry.getKey());
res.setExcludes(excludes);
StringBuilder includes = new StringBuilder();
for (String packageName : entry.getValue()) {
includes.append(packageName + ",");
}
includes.setLength(includes.length() - 1);
res.setIncludes(includes.toString());
console.debug("adding resource fileset {0}", entry.getKey());
console.debug(1, "includes={0}", includes.toString());
jar.add(res);
}
if (includeResources) {
for (File dir : build.getConfig().getResourceDirectories(Scope.compile, tag)) {
FileSet res = resources.createFileset();
res.setDir(dir);
res.setExcludes(Toolkit.DEFAULT_RESOURCE_EXCLUDES);
jar.add(res);
}
}
// set the source jar manifest
try {
Manifest mft = new Manifest();
configureManifest(mft);
jar.addConfiguredManifest(mft);
} catch (ManifestException e) {
console.error(e);
}
start = System.currentTimeMillis();
jar.execute();
console.log(1, sourcesFile.getAbsolutePath());
console.log(1, "{0} KB, generated in {1} ms", (sourcesFile.length()/1024), System.currentTimeMillis() - start);
}
}