You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Hans Deragon <ha...@deragon.biz> on 2009/08/25 19:12:56 UTC
Self written ant task does not get the CLASSPATH passed to ant
with -lib option
Greetings.
Moving ant scripts from Eclipse to a standalone system, I found out that
my own written Ant task does not get the full CLASSPATH passed to ant with
the -lib option.
ant \
-lib /somepath \
-lib /somepath/ant.personal.properties \
-lib /usr/java5/lib \
-lib /usr/java5/jre/lib \
-lib /usr/java5/jre/lib/ext \
-lib /usr/java5/jre/lib/security \
My ant task tries to load the ant.personal.properties file that is the
CLASSPATH, using the following code:
InputStream resourceAsStream =
ClassLoader.getSystemClassLoader().getResourceAsStream(
"ant.personal.properties");
This fails. When I check for the CLASSPATH my ant task gets from ant, it
returns:
export-import.xml:151: 'ant.personal.properties' file not found in
CLASSPATH: /application/cwf/apache-ant-1.7.1/lib/ant-launcher.jar
I obtain the CLASSPATH using the code:
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) sysClassLoader).getURLs();
The urls variable contains all the paths found in the CLASSPATH and from
it, the task prints the values and the error message shown above.
As you notice, the CLASSPATH passed to my own ant task only contains
ant-launcher.jar. Curiously, ant.core.lib only contains that jar.
Why the paths passed to ant with -lib are not found in the CLASSPATH? How
can I fix this? I do not have this problem when running my ant scripts
from within Eclipse.
Please accept my apologies if this is a frequently asked question; I am
sure I am not the first user who has hit this problem. Strange thing, I
could not find on the web any reference to such a problem. I guess a
poor choice of keywords from my part...
Best regards,
Hans Deragon
--
Consultant en informatique/Software Consultant
Deragon Informatique inc.
http://www.deragon.biz Open source (contribution):
mailto://hans@deragon.biz http://autopoweroff.sourceforge.net
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: Self written ant task does not get the CLASSPATH passed to ant
with -lib option
Posted by Hans Deragon <ha...@deragon.biz>.
For posterity, I confirm that it works. Thank you.
Best regards,
Hans Deragon
> On 2009-08-26, Hans Deragon <ha...@deragon.biz> wrote:
>
>> Now the question is: how can I access the instance of AntClassLoader
>> being used by ant and which contains all the -lib entries?
>
> Use a class loaded by that classloader, say
>
> org.apache.tools.ant.Main.class.getClassloader()
>
> this will be an instance of AntClassLoader which has a convenience
> getClasspath() method which should give you a formatted PATH.
>
> Stefan
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: Self written ant task does not get the CLASSPATH passed to ant with -lib option
Posted by Stefan Bodewig <bo...@apache.org>.
On 2009-08-26, Hans Deragon <ha...@deragon.biz> wrote:
> Now the question is: how can I access the instance of AntClassLoader
> being used by ant and which contains all the -lib entries?
Use a class loaded by that classloader, say
org.apache.tools.ant.Main.class.getClassloader()
this will be an instance of AntClassLoader which has a convenience
getClasspath() method which should give you a formatted PATH.
Stefan
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: Self written ant task does not get the CLASSPATH passed to ant
with -lib option
Posted by Hans Deragon <ha...@deragon.biz>.
Greetings.
Thank you for the answer. I found this section that explains what you are
stating: http://ant.apache.org/faq.html#delegating-classloader-1.6
Now the question is: how can I access the instance of AntClassLoader
being used by ant and which contains all the -lib entries? I searched the
web but could not find any recipe.
Any help is appreciated. Thanks for your quick and insightful response.
Hans Deragon
--
Consultant en informatique/Software Consultant
Deragon Informatique inc.
http://www.deragon.biz Open source (contribution):
mailto://hans@deragon.biz http://autopoweroff.sourceforge.net
> On 2009-08-25, Hans Deragon <ha...@deragon.biz> wrote:
>
>> Why the paths passed to ant with -lib are not found in the CLASSPATH?
>
> Because -lib doesn't work that way 8-)
>
> Ant's launcher constructs a new classloader containing the stuff from
> -lib and loads Ant's Main class using that classloader.
>
>> How can I fix this?
>
> If it must be the system classloader that you use, use CLASSPATH
> directly. Most likely you could use the classloader that loaded your
> task instead of the system classloader, though. This one should contain
> all your -lib parts.
>
> Stefan
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: Self written ant task does not get the CLASSPATH passed to ant with -lib option
Posted by Stefan Bodewig <bo...@apache.org>.
On 2009-08-25, Hans Deragon <ha...@deragon.biz> wrote:
> Why the paths passed to ant with -lib are not found in the CLASSPATH?
Because -lib doesn't work that way 8-)
Ant's launcher constructs a new classloader containing the stuff from
-lib and loads Ant's Main class using that classloader.
> How can I fix this?
If it must be the system classloader that you use, use CLASSPATH
directly. Most likely you could use the classloader that loaded your
task instead of the system classloader, though. This one should contain
all your -lib parts.
Stefan
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org