diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index b7ab25bc6d..fa055279fc 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -185,10 +185,9 @@ protected Boolean initialValue() { }; /** - * The Ant core classloader--may be null if using - * parent classloader. + * The Ant core classloader */ - private ClassLoader coreLoader = null; + private AntClassLoader coreLoader = null; /** Records the latest task to be executed on a thread. */ private final Map threadTasks = @@ -302,7 +301,8 @@ public void initSubProject(final Project subProject) { */ public void init() throws BuildException { initProperties(); - + if (coreLoader == null) + setCoreLoader(null); ComponentHelper.getComponentHelper(this).initDefaultDefinitions(); } @@ -361,18 +361,31 @@ public AntClassLoader createClassLoader( /** * Set the core classloader for the project. If a null * classloader is specified, the parent classloader should be used. + *

+ * Since Ant 1.9.5, if the argument is not a + * non-null instance of {@link AntClassLoader}, then + * an ant class loader will be constructed around the specified + * class loader. This ensures that the core class loader is always + * an ant class loader which can be modified using the + * <classloader> task. * * @param coreLoader The classloader to use for the project. * May be null. */ public void setCoreLoader(final ClassLoader coreLoader) { - this.coreLoader = coreLoader; + if (coreLoader == null || coreLoader instanceof AntClassLoader == false) + this.coreLoader = createClassLoader(coreLoader, null); + else + this.coreLoader = (AntClassLoader)coreLoader; } /** * Return the core classloader to use for this project. - * This may be null, indicating that - * the parent classloader should be used. + *

+ * Since Ant 1.9.5 the returned loader will always be a + * non-null instance of {@link AntClassLoader}. + * In older versions, this might be null, + * indicating that the parent classloader should be used. * * @return the core classloader to use for this project. * diff --git a/src/main/org/apache/tools/ant/taskdefs/Classloader.java b/src/main/org/apache/tools/ant/taskdefs/Classloader.java index 8a5967c701..c8898b255a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Classloader.java +++ b/src/main/org/apache/tools/ant/taskdefs/Classloader.java @@ -161,17 +161,26 @@ public Path createClasspath() { */ public void execute() { try { - // Gump friendly - don't mess with the core loader if only classpath - if ("only".equals(getProject().getProperty("build.sysclasspath")) - && (name == null || SYSTEM_LOADER_REF.equals(name))) { - log("Changing the system loader is disabled " - + "by build.sysclasspath=only", Project.MSG_WARN); - return; + String loaderName; + Object obj; + if (name == null || SYSTEM_LOADER_REF.equals(name)) { + if ("only".equals(getProject() + .getProperty("build.sysclasspath"))) { + // Gump friendly - don't mess with the core loader + // if only the system classpath is to be used + log("Changing the system loader is disabled " + + "by build.sysclasspath=only", Project.MSG_WARN); + return; + } + name = null; + loaderName = SYSTEM_LOADER_REF; + obj = getProject().getCoreLoader(); + } + else { + loaderName = name; + obj = getProject().getReference(loaderName); } - String loaderName = (name == null) ? SYSTEM_LOADER_REF : name; - - Object obj = getProject().getReference(loaderName); if (reset) { // Are any other references held ? Can we 'close' the loader // so it removes the locks on jars ? diff --git a/src/tests/antunit/taskdefs/classloader-test.xml b/src/tests/antunit/taskdefs/classloader-test.xml new file mode 100644 index 0000000000..4b53823058 --- /dev/null +++ b/src/tests/antunit/taskdefs/classloader-test.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +