From eb64d092a48900216af76452f0fb33e371d30d17 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 1 Sep 2009 14:00:17 +0000 Subject: [PATCH] add a parentFirst attribute to javaresource so you can load resources from the specified classpath instead of the system classpath. PR 41369 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@810060 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 5 ++ docs/manual/CoreTypes/resources.html | 9 +++ .../resources/AbstractClasspathResource.java | 24 ++++++- .../types/resources/javaresource-test.xml | 62 +++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/tests/antunit/types/resources/javaresource-test.xml diff --git a/WHATSNEW b/WHATSNEW index 94b0dd4ab..3ae4a3c08 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -939,6 +939,11 @@ Other changes: * can now specify values as nested text. Bugzilla Report 32917. + * a new parentFirst attribute on allows resources to + be loaded from the specified classpath rather than the system + classloader. + Bugzilla Report 41369. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTypes/resources.html b/docs/manual/CoreTypes/resources.html index 094efda14..a4417450d 100644 --- a/docs/manual/CoreTypes/resources.html +++ b/docs/manual/CoreTypes/resources.html @@ -149,6 +149,15 @@ implementations are also usable as single-element used to load the resource, constructed from the specified classpath. No + + parentFirst + Whether to consult the parent classloader first - + the parent classloader most likely is the system classloader - + when using a nested classpath. Defaults + to true.
+ Since Ant 1.8.0 + No +

The classpath can also be specified as nested classpath element, diff --git a/src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java b/src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java index a7de4aaa1..371341341 100644 --- a/src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java +++ b/src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java @@ -17,6 +17,7 @@ */ package org.apache.tools.ant.types.resources; +import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.Path; @@ -31,13 +32,14 @@ import java.util.Stack; * * A Resource representation of anything that is accessed via a Java classloader. * The core methods to set/resolve the classpath are provided. - * @since Ant 1.8 + * @since Ant 1.8.0 * */ public abstract class AbstractClasspathResource extends Resource { private Path classpath; private Reference loader; + private boolean parentFirst = true; /** * Set the classpath to use when looking up a resource. @@ -116,6 +118,17 @@ public abstract class AbstractClasspathResource extends Resource { loader = r; } + /** + * Whether to consult the parent classloader first. + * + *

Only relevant if a classpath has been specified.

+ * + * @since Ant 1.8.0 + */ + public void setParentFirst(boolean b) { + parentFirst = b; + } + /** * Overrides the super version. * @param r the Reference to set. @@ -165,7 +178,14 @@ public abstract class AbstractClasspathResource extends Resource { if (cl == null) { if (getClasspath() != null) { Path p = getClasspath().concatSystemClasspath(); - cl = getProject().createClassLoader(p); + if (parentFirst) { + cl = getProject().createClassLoader(p); + } else { + cl = AntClassLoader.newAntClassLoader(getProject() + .getCoreLoader(), + getProject(), + p, false); + } } else { cl = JavaResource.class.getClassLoader(); } diff --git a/src/tests/antunit/types/resources/javaresource-test.xml b/src/tests/antunit/types/resources/javaresource-test.xml new file mode 100644 index 000000000..4b2c00b84 --- /dev/null +++ b/src/tests/antunit/types/resources/javaresource-test.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + Hello, world! + + + + + + + + + + + + + + + + + + + + + +