Browse Source

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
master
Stefan Bodewig 16 years ago
parent
commit
eb64d092a4
4 changed files with 98 additions and 2 deletions
  1. +5
    -0
      WHATSNEW
  2. +9
    -0
      docs/manual/CoreTypes/resources.html
  3. +22
    -2
      src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java
  4. +62
    -0
      src/tests/antunit/types/resources/javaresource-test.xml

+ 5
- 0
WHATSNEW View File

@@ -939,6 +939,11 @@ Other changes:
* <property> can now specify values as nested text. * <property> can now specify values as nested text.
Bugzilla Report 32917. Bugzilla Report 32917.


* a new parentFirst attribute on <javaresource> 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 Changes from Ant 1.7.0 TO Ant 1.7.1
============================================= =============================================




+ 9
- 0
docs/manual/CoreTypes/resources.html View File

@@ -149,6 +149,15 @@ implementations are also usable as single-element
used to load the resource, constructed from the specified classpath.</td> used to load the resource, constructed from the specified classpath.</td>
<td align="center" valign="top">No</td> <td align="center" valign="top">No</td>
</tr> </tr>
<tr>
<td valign="top">parentFirst</td>
<td valign="top">Whether to consult the parent classloader first -
the parent classloader most likely is the system classloader -
when using a nested classpath. Defaults
to <code>true</code>.<br/>
<em>Since Ant 1.8.0</em></td>
<td align="center" valign="top">No</td>
</tr>
</table> </table>


<p>The classpath can also be specified as nested classpath element, <p>The classpath can also be specified as nested classpath element,


+ 22
- 2
src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java View File

@@ -17,6 +17,7 @@
*/ */
package org.apache.tools.ant.types.resources; package org.apache.tools.ant.types.resources;


import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.Path; 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. * A Resource representation of anything that is accessed via a Java classloader.
* The core methods to set/resolve the classpath are provided. * 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 { public abstract class AbstractClasspathResource extends Resource {
private Path classpath; private Path classpath;
private Reference loader; private Reference loader;
private boolean parentFirst = true;


/** /**
* Set the classpath to use when looking up a resource. * Set the classpath to use when looking up a resource.
@@ -116,6 +118,17 @@ public abstract class AbstractClasspathResource extends Resource {
loader = r; loader = r;
} }


/**
* Whether to consult the parent classloader first.
*
* <p>Only relevant if a classpath has been specified.</p>
*
* @since Ant 1.8.0
*/
public void setParentFirst(boolean b) {
parentFirst = b;
}

/** /**
* Overrides the super version. * Overrides the super version.
* @param r the Reference to set. * @param r the Reference to set.
@@ -165,7 +178,14 @@ public abstract class AbstractClasspathResource extends Resource {
if (cl == null) { if (cl == null) {
if (getClasspath() != null) { if (getClasspath() != null) {
Path p = getClasspath().concatSystemClasspath(); Path p = getClasspath().concatSystemClasspath();
cl = getProject().createClassLoader(p);
if (parentFirst) {
cl = getProject().createClassLoader(p);
} else {
cl = AntClassLoader.newAntClassLoader(getProject()
.getCoreLoader(),
getProject(),
p, false);
}
} else { } else {
cl = JavaResource.class.getClassLoader(); cl = JavaResource.class.getClassLoader();
} }


+ 62
- 0
src/tests/antunit/types/resources/javaresource-test.xml View File

@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit">

<import file="../../antunit-base.xml"/>

<target name="setUp">
<mkdir dir="${output}"/>
</target>

<target name="testUsesSystemClasspath" depends="setUp">
<copy todir="${output}">
<javaresource name="org/apache/tools/ant/antlib.xml"/>
</copy>
<au:assertFileExists file="${output}/org/apache/tools/ant/antlib.xml"/>
</target>

<target name="-setUpAntlibXmlInInput" depends="setUp">
<mkdir dir="${input}/org/apache/tools/ant"/>
<echo file="${input}/org/apache/tools/ant/antlib.xml">Hello, world!</echo>
</target>

<target name="testParentFirstIsDefault" depends="-setUpAntlibXmlInInput">
<copy todir="${output}">
<javaresource name="org/apache/tools/ant/antlib.xml">
<classpath location="${input}"/>
</javaresource>
</copy>
<au:assertFileExists file="${output}/org/apache/tools/ant/antlib.xml"/>
<au:assertFilesDiffer
expected="${input}/org/apache/tools/ant/antlib.xml"
actual="${output}/org/apache/tools/ant/antlib.xml"/>
</target>

<target name="testParentFirstFalse" depends="-setUpAntlibXmlInInput">
<copy todir="${output}">
<javaresource name="org/apache/tools/ant/antlib.xml"
parentFirst="false">
<classpath location="${input}"/>
</javaresource>
</copy>
<au:assertFileExists file="${output}/org/apache/tools/ant/antlib.xml"/>
<au:assertFilesMatch
expected="${input}/org/apache/tools/ant/antlib.xml"
actual="${output}/org/apache/tools/ant/antlib.xml"/>
</target>
</project>

Loading…
Cancel
Save