Browse Source

Add nested fileset and filesetref attributes to Path.

This means you can now add somedir/*.jar to the CLASSPATH by
<classpath>
  <fileset dir="somedir">
    <include name="**/*.jar" />
  </fileset>
</classpath>

and probably adjust the pattern if you don't want to recurse the tree.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267864 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 25 years ago
parent
commit
a8d4fe6d9b
7 changed files with 139 additions and 70 deletions
  1. +3
    -3
      src/main/org/apache/tools/ant/taskdefs/Javac.java
  2. +1
    -1
      src/main/org/apache/tools/ant/taskdefs/Rmic.java
  3. +1
    -1
      src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
  4. +3
    -3
      src/main/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java
  5. +123
    -41
      src/main/org/apache/tools/ant/types/Path.java
  6. +5
    -4
      src/testcases/org/apache/tools/ant/types/CommandlineJavaTest.java
  7. +3
    -17
      src/testcases/org/apache/tools/ant/types/PathTest.java

+ 3
- 3
src/main/org/apache/tools/ant/taskdefs/Javac.java View File

@@ -357,7 +357,7 @@ public class Javac extends MatchingTask {
// add dest dir to classpath so that previously compiled and // add dest dir to classpath so that previously compiled and
// untouched classes are on classpath // untouched classes are on classpath


classpath.setLocation(destDir.getAbsolutePath());
classpath.setLocation(destDir);


// add our classpath to the mix // add our classpath to the mix


@@ -415,7 +415,7 @@ public class Javac extends MatchingTask {
File f = project.resolveFile(list[i]); File f = project.resolveFile(list[i]);


if (f.exists()) { if (f.exists()) {
target.setLocation(f.getAbsolutePath());
target.setLocation(f);
} else { } else {
log("Dropping from classpath: "+ log("Dropping from classpath: "+
f.getAbsolutePath(), Project.MSG_VERBOSE); f.getAbsolutePath(), Project.MSG_VERBOSE);
@@ -782,7 +782,7 @@ public class Javac extends MatchingTask {
for (int i=0 ; i < files.length ; i++) { for (int i=0 ; i < files.length ; i++) {
File f = new File(dir,files[i]); File f = new File(dir,files[i]);
if (f.exists() && f.isFile()) { if (f.exists() && f.isFile()) {
classpath.setLocation(f.getAbsolutePath());
classpath.setLocation(f);
} }
} }
} }


+ 1
- 1
src/main/org/apache/tools/ant/taskdefs/Rmic.java View File

@@ -410,7 +410,7 @@ public class Rmic extends MatchingTask {
File f = project.resolveFile(list[i]); File f = project.resolveFile(list[i]);


if (f.exists()) { if (f.exists()) {
target.setLocation(f.getAbsolutePath());
target.setLocation(f);
} else { } else {
log("Dropping from classpath: "+ log("Dropping from classpath: "+
f.getAbsolutePath(), Project.MSG_VERBOSE); f.getAbsolutePath(), Project.MSG_VERBOSE);


+ 1
- 1
src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java View File

@@ -103,7 +103,7 @@ public class JUnitTask extends Task {
* Set the path to junit classes. * Set the path to junit classes.
* @param junit path to junit classes. * @param junit path to junit classes.
*/ */
public void setJunit(String junit) {
public void setJunit(File junit) {
commandline.createClasspath(project).createPathElement().setLocation(junit); commandline.createClasspath(project).createPathElement().setLocation(junit);
} }




+ 3
- 3
src/main/org/apache/tools/ant/taskdefs/optional/metamata/MParse.java View File

@@ -134,9 +134,9 @@ public class MParse extends Task {
} }


final Path classpath = cmdl.createClasspath(project); final Path classpath = cmdl.createClasspath(project);
classpath.createPathElement().setLocation(metahome.getAbsolutePath() + "/lib/metamatadebug.jar");
classpath.createPathElement().setLocation(metahome.getAbsolutePath() + "/lib/metamata.jar");
classpath.createPathElement().setLocation(metahome.getAbsolutePath() + "/lib/JavaCC.zip");
classpath.createPathElement().setLocation(new File(metahome.getAbsolutePath() + "/lib/metamatadebug.jar"));
classpath.createPathElement().setLocation(new File(metahome.getAbsolutePath() + "/lib/metamata.jar"));
classpath.createPathElement().setLocation(new File(metahome.getAbsolutePath() + "/lib/JavaCC.zip"));


final Commandline.Argument arg = cmdl.createVmArgument(); final Commandline.Argument arg = cmdl.createVmArgument();
arg.setValue("-mx140M"); arg.setValue("-mx140M");


+ 123
- 41
src/main/org/apache/tools/ant/types/Path.java View File

@@ -54,6 +54,8 @@


package org.apache.tools.ant.types; package org.apache.tools.ant.types;


import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.PathTokenizer; import org.apache.tools.ant.PathTokenizer;


@@ -93,20 +95,44 @@ import java.text.StringCharacterIterator;


public class Path { public class Path {


private Vector definition;
private Vector elements;
private Project project; private Project project;


public static Path systemClasspath = public static Path systemClasspath =
new Path(null, System.getProperty("java.class.path")); new Path(null, System.getProperty("java.class.path"));



/**
* Helper class, holds the nested <pathelement> values.
*/
public class PathElement {
private String[] parts;

public void setLocation(File loc) {
parts = new String[] {translateFile(loc.getAbsolutePath())};
}

public void setPath(String path) {
parts = Path.translatePath(project, path);
}

public String[] getParts() {
return parts;
}
}

/**
* Invoked by IntrospectionHelper for <code>setXXX(Path p)</code>
* attribute setters.
*/
public Path(Project p, String path) { public Path(Project p, String path) {
this(p); this(p);
setPath(path);
createPathElement().setPath(path);
} }


public Path(Project p) { public Path(Project p) {
this.project = project; this.project = project;
definition = new Vector();
elements = new Vector();
} }


/** /**
@@ -114,56 +140,98 @@ public class Path {
* @param location the location of the element to add (must not be * @param location the location of the element to add (must not be
* <code>null</code> nor empty. * <code>null</code> nor empty.
*/ */
public void setLocation(String location) {
if (location != null && location.length() > 0) {
String element = translateFile(resolveFile(project, location));
if (definition.indexOf(element) == -1) {
definition.addElement(element);
}
}
public void setLocation(File location) {
createPathElement().setLocation(location);
} }




/** /**
* Append the contents of the other Path instance to this.
* Parses a path definition and creates single PathElements.
* @param path the path definition.
*/ */
public void append(Path other) {
String[] l = other.list();
for (int i=0; i<l.length; i++) {
if (definition.indexOf(l[i]) == -1) {
definition.addElement(l[i]);
}
}
public void setPath(String path) {
createPathElement().setPath(path);
} }



/** /**
* Parses a path definition and creates single PathElements.
* @param path the path definition.
* Created the nested <pathelement> element.
*/ */
public void setPath(String path) {
final Vector elements = translatePath(project, path);
for (int i=0; i < elements.size(); i++) {
String element = (String) elements.elementAt(i);
if (definition.indexOf(element) == -1) {
definition.addElement(element);
}
}
public PathElement createPathElement() {
PathElement pe = new PathElement();
elements.addElement(pe);
return pe;
} }


/**
* Adds a nested <fileset> element.
*/
public void addFileset(FileSet fs) {
elements.addElement(fs);
}


public Path createPathElement() {
return this;
/**
* Adds a nested <filesetref> element.
*/
public void addFilesetRef(Reference r) {
elements.addElement(r);
} }


/**
* Append the contents of the other Path instance to this.
*/
public void append(Path other) {
String[] l = other.list();
for (int i=0; i<l.length; i++) {
if (elements.indexOf(l[i]) == -1) {
elements.addElement(l[i]);
}
}
}


/** /**
* Returns all path elements defined by this and netsed path objects. * Returns all path elements defined by this and netsed path objects.
* @return list of path elements. * @return list of path elements.
*/ */
public String[] list() { public String[] list() {
final String[] result = new String[definition.size()];
definition.copyInto(result);
return result;
Vector result = new Vector(2*elements.size());
for (int i=0; i<elements.size(); i++) {
Object o = elements.elementAt(i);
if (o instanceof Reference) {
Reference r = (Reference) o;
o = r.getReferencedObject(project);
// we only support references to filesets right now
if (o == null || !(o instanceof FileSet)) {
String msg = r.getRefId()+" doesn\'t denote a fileset";
throw new BuildException(msg);
}
}
if (o instanceof String) {
// obtained via append
addUnlessPresent(result, (String) o);
} else if (o instanceof PathElement) {
String[] parts = ((PathElement) o).getParts();
if (parts == null) {
throw new BuildException("You must either set location or path on <pathelement>");
}
for (int j=0; j<parts.length; j++) {
addUnlessPresent(result, parts[j]);
}
} else if (o instanceof FileSet) {
FileSet fs = (FileSet) o;
DirectoryScanner ds = fs.getDirectoryScanner(project);
String[] s = ds.getIncludedFiles();
File dir = fs.getDir();
for (int j=0; j<s.length; j++) {
addUnlessPresent(result,
translateFile((new File(dir, s[j])).getAbsolutePath()));
}
}
}
String[] res = new String[result.size()];
result.copyInto(res);
return res;
} }




@@ -188,11 +256,12 @@ public class Path {
return result.toString(); return result.toString();
} }




public static Vector translatePath(Project project, String source) {
/**
* Splits a PATH (with : or ; as separators) into its parts.
*/
public static String[] translatePath(Project project, String source) {
final Vector result = new Vector(); final Vector result = new Vector();
if (source == null) return result;
if (source == null) return new String[0];


PathTokenizer tok = new PathTokenizer(source); PathTokenizer tok = new PathTokenizer(source);
StringBuffer element = new StringBuffer(); StringBuffer element = new StringBuffer();
@@ -204,10 +273,15 @@ public class Path {
} }
result.addElement(element.toString()); result.addElement(element.toString());
} }
return result;
String[] res = new String[result.size()];
result.copyInto(res);
return res;
} }



/**
* Returns its argument with all file separator characters
* replaced so that they match the local OS conventions.
*/
public static String translateFile(String source) { public static String translateFile(String source) {
if (source == null) return ""; if (source == null) return "";


@@ -219,7 +293,6 @@ public class Path {
return result.toString(); return result.toString();
} }



protected static boolean translateFileSep(StringBuffer buffer, int pos) { protected static boolean translateFileSep(StringBuffer buffer, int pos) {
if (buffer.charAt(pos) == '/' || buffer.charAt(pos) == '\\') { if (buffer.charAt(pos) == '/' || buffer.charAt(pos) == '\\') {
buffer.setCharAt(pos, File.separatorChar); buffer.setCharAt(pos, File.separatorChar);
@@ -228,8 +301,11 @@ public class Path {
return false; return false;
} }


/**
* How many parts does this Path instance consist of.
*/
public int size() { public int size() {
return definition.size();
return list().length;
} }


private static String resolveFile(Project project, String relativeName) { private static String resolveFile(Project project, String relativeName) {
@@ -239,4 +315,10 @@ public class Path {
return relativeName; return relativeName;
} }


private static void addUnlessPresent(Vector v, String s) {
if (v.indexOf(s) == -1) {
v.addElement(s);
}
}

} }

+ 5
- 4
src/testcases/org/apache/tools/ant/types/CommandlineJavaTest.java View File

@@ -92,15 +92,16 @@ public class CommandlineJavaTest extends TestCase {
assertEquals("no classpath", assertEquals("no classpath",
"org.apache.tools.ant.CommandlineJavaTest", s[3]); "org.apache.tools.ant.CommandlineJavaTest", s[3]);


c.createClasspath(project).setLocation("junit.jar");
c.createClasspath(project).setLocation("ant.jar");
c.createClasspath(project).setLocation(new File("junit.jar"));
c.createClasspath(project).setLocation(new File("ant.jar"));
s = c.getCommandline(); s = c.getCommandline();
assertEquals("with classpath", 6, s.length); assertEquals("with classpath", 6, s.length);
assertEquals("with classpath", "java", s[0]); assertEquals("with classpath", "java", s[0]);
assertEquals("with classpath", "-Djava.compiler=NONE", s[1]); assertEquals("with classpath", "-Djava.compiler=NONE", s[1]);
assertEquals("with classpath", "-classpath", s[2]); assertEquals("with classpath", "-classpath", s[2]);
assertEquals("with classpath",
"junit.jar"+java.io.File.pathSeparator+"ant.jar", s[3]);
assert("junit.jar contained",
s[3].indexOf("junit.jar"+java.io.File.pathSeparator) >= 0);
assert("ant.jar contained", s[3].endsWith("ant.jar"));
assertEquals("with classpath", "junit.textui.TestRunner", s[4]); assertEquals("with classpath", "junit.textui.TestRunner", s[4]);
assertEquals("with classpath", assertEquals("with classpath",
"org.apache.tools.ant.CommandlineJavaTest", s[5]); "org.apache.tools.ant.CommandlineJavaTest", s[5]);


+ 3
- 17
src/testcases/org/apache/tools/ant/types/PathTest.java View File

@@ -145,7 +145,7 @@ public class PathTest extends TestCase {


public void testSetLocation() { public void testSetLocation() {
Path p = new Path(project); Path p = new Path(project);
p.setLocation("/a");
p.setLocation(new File(File.separatorChar+"a"));
String[] l = p.list(); String[] l = p.list();
if (isUnixStyle) { if (isUnixStyle) {
assertEquals(1, l.length); assertEquals(1, l.length);
@@ -154,24 +154,13 @@ public class PathTest extends TestCase {
assertEquals(1, l.length); assertEquals(1, l.length);
assertEquals("\\a", l[0]); assertEquals("\\a", l[0]);
} }

p = new Path(project);
p.setLocation("\\a");
l = p.list();
if (isUnixStyle) {
assertEquals(1, l.length);
assertEquals("/a", l[0]);
} else {
assertEquals(1, l.length);
assertEquals("\\a", l[0]);
}
} }


public void testAppending() { public void testAppending() {
Path p = new Path(project, "/a:/b"); Path p = new Path(project, "/a:/b");
String[] l = p.list(); String[] l = p.list();
assertEquals("2 after construction", 2, l.length); assertEquals("2 after construction", 2, l.length);
p.setLocation("/c");
p.setLocation(new File("/c"));
l = p.list(); l = p.list();
assertEquals("3 after setLocation", 3, l.length); assertEquals("3 after setLocation", 3, l.length);
p.setPath("\\d;\\e"); p.setPath("\\d;\\e");
@@ -186,9 +175,6 @@ public class PathTest extends TestCase {
Path p = new Path(project, ""); Path p = new Path(project, "");
String[] l = p.list(); String[] l = p.list();
assertEquals("0 after construction", 0, l.length); assertEquals("0 after construction", 0, l.length);
p.setLocation("");
l = p.list();
assertEquals("0 after setLocation", 0, l.length);
p.setPath(""); p.setPath("");
l = p.list(); l = p.list();
assertEquals("0 after setPath", 0, l.length); assertEquals("0 after setPath", 0, l.length);
@@ -201,7 +187,7 @@ public class PathTest extends TestCase {
Path p = new Path(project, "/a:/a"); Path p = new Path(project, "/a:/a");
String[] l = p.list(); String[] l = p.list();
assertEquals("1 after construction", 1, l.length); assertEquals("1 after construction", 1, l.length);
p.setLocation("\\a");
p.setLocation(new File(File.separatorChar+"a"));
l = p.list(); l = p.list();
assertEquals("1 after setLocation", 1, l.length); assertEquals("1 after setLocation", 1, l.length);
p.setPath("\\a;/a"); p.setPath("\\a;/a");


Loading…
Cancel
Save