I notice the need in Maven to specify the jar/zip file dependency both in
the project.xml and in "lib.repo" or "maven.repo.remote".
However, our existing projects have pretty large sizes using a lot of
jar/zip files residing in multiple directories. So I suggest to support of
the optional feature of "zero dependency configuration".
Specifically,
1) specify the "lib.repo.dynamic" property to point to a list of directories
or direct jar/zip files. Jar/zip files both from those directories and
directly specified will be automatically added to the runtime classpath by
Maven.
2) specify the "lib.repo.dynamic.exclude" property to point to a list of
jar/zip files to be explicitly excluded from being loaded during the dynamic
loading process.
3) (2) always overrides (1).
4) These properties are an optional features, and they can work with the
existing properties of Maven at the same time without conflicts.
By using these properties, the <dependencies> elements can be entirely
commented out or omitted from project.xml.
To support the new properties, I created on class DynamicRepo.java and added
one line of code to ProjectProperties.java (MAVEN_1_0_B4 (revision: 1.43)).
Please find below the source codes for the suggested changes. They work
nicely in our installation.
Just wonder if your group like the idea, and if so if you can incorporate
the changes to your CVS repository, so we can still have the feature for
future releases.
Thanks,
Hanson
org.apache.maven.ProjectProperties.java
---------------------------------------
private void createClasspathReference()
{
Path classpath = new Path(getProject());
for (Iterator i = getMavenProject().getDependencies().iterator();
i.hasNext();)
{
Dependency dependency = (Dependency) i.next();
Path p = new Path(getProject());
p.setPath(new File(libRepo,
dependency.getJar()).getAbsolutePath());
classpath.append(p);
}
///////////// Change: start
DynamicRepo.addDynClasspathReference(getProject(), classpath);
///////////// Change: end
getProject().addReference("maven.dependency.classpath", classpath);
}
org.apache.maven.DynamicRepo.java (new class)
---------------------------------
package org.apache.maven;
import java.io.*;
import java.util.*;
import org.apache.tools.ant.*;
import org.apache.tools.ant.types.*;
/**
* Dynamic repository which supports the optional feature of zero-dependency
configuration.
* It automatic loads a set of jar/zip files to the classpath from a list of
comma-delimited
* directories or files specified in the project property
"lib.repo.dynamic".
*
* An optional set of files can be explicitely excluded from being loaded
via the dynamic repository.
* These files are specified as a list of comma-delimited jar/zip files in
the
* project property "lib.repo.dynamic.exclude".
*
* @author Hanson Char
* @version 1.0
*/
class DynamicRepo {
private static final String DELIMITER = ",";
/**
* Dynamically adds the jar/zip files to the classpath.
* @project the current project.
* @classpath the current classpath.
*/
static void addDynClasspathReference(Project project, final Path
classpath)
{
String dynlibRepo = project.getProperty("lib.repo.dynamic");
if (dynlibRepo == null || dynlibRepo.trim().length() == 0) {
// dynamic repository not specified.
return;
}
final Set excludeFileSet =
loadExcludeFileSet(project.getProperty("lib.repo.dynamic.exclude"));
// Figure out the set of file/dir to be used for dynamic lookup.
StringTokenizer st = new StringTokenizer(dynlibRepo, DELIMITER, true);
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.equals(DELIMITER)) {
continue;
}
File file = new File(token.trim());
if (!file.exists()) {
continue;
}
if (file.isFile()) {
String name = file.getName();
if (name.endsWith(".jar") || name.endsWith(".zip")) {
if (!excludeFileSet.contains(file)) {
addFilesToClasspath(project, classpath, new File[]{file});
}
}
continue;
}
// directory.
File[] list = file.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return (name.endsWith(".jar") || name.endsWith(".zip"))
&& !excludeFileSet.contains(new File(dir, name));
}
});
addFilesToClasspath(project, classpath, list);
}
}
/** Returns a set of jar/zip files to be excluded from dynamic loading. */
private static Set loadExcludeFileSet(String dynlibRepoExclude) {
Set set = new HashSet();
if (dynlibRepoExclude == null || dynlibRepoExclude.trim().length() == 0)
{
// exclude list not specified.
return set;
}
StringTokenizer st = new StringTokenizer(dynlibRepoExclude, DELIMITER,
true);
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.equals(DELIMITER)) {
continue;
}
File file = new File(token.trim());
if (!file.exists() || !file.isFile()) {
continue;
}
String name = file.getName();
if (!name.endsWith(".jar") && !name.endsWith(".zip")) {
continue;
}
set.add(file);
}
return set;
}
/** Adds the given set of files to the specified classpath. */
private static void addFilesToClasspath(Project project, Path classpath,
File[] file)
{
for (int i=0; i < file.length; i++) {
Path p = new Path(project);
System.out.println(new StringBuffer()
.append("DynamicRepo>>Adding ")
.append(file[i])
.append(" to classpath.")
.toString());
p.setPath(file[i].getAbsolutePath());
classpath.append(p);
}
}
}
/////////////////////////////// end ///////////////////////////////