* Copies the classpath elements to WEB-INF.
*
* @throws java.io.IOException
*/
protected void doLibCopy() throws IOException {
Enunciate enunciate = getEnunciate();
File buildDir = getBuildDir();
File webinf = new File(buildDir, "WEB-INF");
File webinfClasses = new File(webinf, "classes");
File webinfLib = new File(webinf, "lib");
//initialize the include filters.
AntPatternMatcher pathMatcher = new AntPatternMatcher();
pathMatcher.setPathSeparator(File.separator);
List<File> explicitIncludes = new ArrayList<File>();
List<String> includePatterns = new ArrayList<String>();
WebAppConfig webAppConfig = getWebAppConfig();
if (webAppConfig != null) {
for (IncludeExcludeLibs el : webAppConfig.getIncludeLibs()) {
if (el.getFile() != null) {
//add explicit files to the include files list.
explicitIncludes.add(el.getFile());
}
String pattern = el.getPattern();
if (pattern != null) {
//normalize the pattern to the platform.
pattern = pattern.replace('/', File.separatorChar);
if (pathMatcher.isPattern(pattern)) {
//make sure that the includes pattern list only has patterns.
includePatterns.add(pattern);
}
else {
warn("Pattern '%s' is not a valid pattern, so it will not be applied.", pattern);
}
}
}
}
if (includePatterns.isEmpty()) {
//if no include patterns are specified, the implicit pattern is "**/*".
String starPattern = "**" + File.separatorChar + "*";
debug("No include patterns have been specified. Using the implicit '%s' pattern.", starPattern);
includePatterns.add(starPattern);
}
List<String> warLibs = new ArrayList<String>();
if (webAppConfig == null || webAppConfig.isIncludeClasspathLibs()) {
debug("Using the Enunciate classpath as the initial list of libraries to be passed through the include/exclude filter.");
//prime the list of libs to include in the war with what's on the enunciate classpath.
warLibs.addAll(Arrays.asList(enunciate.getEnunciateRuntimeClasspath().split(File.pathSeparator)));
}
// Apply the "in filter" (i.e. the filter that specifies the files to be included).
List<File> includedLibs = new ArrayList<File>();
for (String warLib : warLibs) {
File libFile = new File(warLib);
if (libFile.exists()) {
for (String includePattern : includePatterns) {
String absolutePath = libFile.getAbsolutePath();
if (absolutePath.startsWith(File.separator)) {
//lob off the beginning "/" for Linux boxes.
absolutePath = absolutePath.substring(1);
}
if (pathMatcher.match(includePattern, absolutePath)) {
debug("Library '%s' passed the include filter. It matches pattern '%s'.", libFile.getAbsolutePath(), includePattern);
includedLibs.add(libFile);
break;
}
else if (enunciate.isDebug()) {
debug("Library '%s' did NOT match include pattern '%s'.", includePattern);
}
}
}
}
//Now, with what's left, apply the "exclude filter".
boolean excludeDefaults = webAppConfig == null || webAppConfig.isExcludeDefaultLibs();
List<String> manifestClasspath = new ArrayList<String>();
Iterator<File> toBeIncludedIt = includedLibs.iterator();
while (toBeIncludedIt.hasNext()) {
File toBeIncluded = toBeIncludedIt.next();
if (excludeDefaults && knownExclude(toBeIncluded)) {
toBeIncludedIt.remove();
}
else if (webAppConfig != null) {
for (IncludeExcludeLibs excludeLibs : webAppConfig.getExcludeLibs()) {
boolean exclude = false;
if ((excludeLibs.getFile() != null) && (excludeLibs.getFile().equals(toBeIncluded))) {
exclude = true;
debug("%s was explicitly excluded.", toBeIncluded);
}
else {
String pattern = excludeLibs.getPattern();
if (pattern != null) {
pattern = pattern.replace('/', File.separatorChar);
if (pathMatcher.isPattern(pattern)) {
String absolutePath = toBeIncluded.getAbsolutePath();
if (absolutePath.startsWith(File.separator)) {
//lob off the beginning "/" for Linux boxes.
absolutePath = absolutePath.substring(1);
}
if (pathMatcher.match(pattern, absolutePath)) {
exclude = true;
debug("%s was excluded because it matches pattern '%s'", toBeIncluded, pattern);
}
}
}
}
if (exclude) {
toBeIncludedIt.remove();
if ((excludeLibs.isIncludeInManifest()) && (!toBeIncluded.isDirectory())) {
//include it in the manifest anyway.
manifestClasspath.add(toBeIncluded.getName());
debug("'%s' will be included in the manifest classpath.", toBeIncluded.getName());
}
break;
}
}
}
}
//now add the lib files that are explicitly included.
includedLibs.addAll(explicitIncludes);
//now we've got the final list, copy the libs.
for (File includedLib : includedLibs) {
if (includedLib.isDirectory()) {
debug("Adding the contents of %s to WEB-INF/classes.", includedLib);
enunciate.copyDir(includedLib, webinfClasses);
}
else {
debug("Including %s in WEB-INF/lib.", includedLib);
enunciate.copyFile(includedLib, includedLib.getParentFile(), webinfLib);
}
}
// write the manifest file.
Manifest manifest = webAppConfig == null ? WebAppConfig.getDefaultManifest() : webAppConfig.getManifest();