run antunit with optional classpath (compiles fine for what it's worth but proper functioning requires Ant with https://github.com/apache/ant/commit/ce494e8365658f76b2648f5d0ccfd6753c880249)
diff --git a/src/main/org/apache/ant/antunit/AntUnit.java b/src/main/org/apache/ant/antunit/AntUnit.java
index 9ed4169..e00187e 100644
--- a/src/main/org/apache/ant/antunit/AntUnit.java
+++ b/src/main/org/apache/ant/antunit/AntUnit.java
@@ -31,6 +31,7 @@
import java.util.Map;
import java.util.Set;
+import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildListener;
@@ -41,6 +42,7 @@
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Ant;
import org.apache.tools.ant.types.Mapper;
+import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.PropertySet;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
@@ -147,6 +149,10 @@
*/
private String errorProperty = null;
+ private Path classpath;
+
+ private AntClassLoader subprojectCoreLoader;
+
/**
* Add build files to run as tests.
* @param rc the ResourceCollection to add.
@@ -212,12 +218,37 @@
}
/**
+ * Create the nested classpath element.
+ * @return {@link Path}
+ */
+ public Path createClasspath() {
+ if (classpath != null) {
+ throw new BuildException("Can only set classpath once");
+ }
+ return classpath = new Path(getProject());
+ }
+
+ /**
+ * Set the test classpath as a reference to a {@link Path} defined elsewhere.
+ * @param classpathRefid
+ */
+ public void setClasspathRefid(String classpathRefid) {
+ if (classpathRefid == null || classpathRefid.trim().length() == 0) {
+ throw new BuildException("Null/empty/blank @classpathrefid specified");
+ }
+ createClasspath().setRefid(new org.apache.tools.ant.types.Reference(getProject(), classpathRefid));
+ }
+
+ /**
* Execute the tests.
*/
public void execute() {
if (buildFiles == null) {
throw new BuildException(ERROR_NO_TESTS);
}
+ if (classpath != null && classpath.size() > 0) {
+ subprojectCoreLoader = getProject().createClassLoader(classpath);
+ }
doResourceCollection(buildFiles);
if (failures > 0 || errors > 0) {
if (errorProperty != null) {
@@ -342,6 +373,9 @@
*/
private Project createProjectForFile(File f) {
Project p = new Project();
+ if (subprojectCoreLoader != null) {
+ p.setCoreLoader(subprojectCoreLoader);
+ }
p.setDefaultInputStream(getProject().getDefaultInputStream());
p.initProperties();
p.setInputHandler(getProject().getInputHandler());