ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stanS <stanley_...@ml.com>
Subject Re: Getting classpath attribute of custom task
Date Mon, 30 Jul 2007 20:38:50 GMT

It helps, thx.
Do you think this will also handle both the cases where classpath is defined
in <taskdef> and 
mytask.jar is in ANTHOME\lib or elsewhere?


        if (this.getClass().getClassLoader() instanceof AntClassLoader){
            AntClassLoader antClassLoader =
(AntClassLoader)this.getClass().getClassLoader();
            taskClasspath = System.getProperty("java.class.path") + ";" +
antClassLoader.getClasspath();
        }
        else {
            taskClasspath = System.getProperty("java.class.path");
        }
        Path classpath = new Path(getProject());
        classpath.setPath(taskClasspath);
        javaTask.setClasspath(taskClasspath);


Originally when I started it, I didn't want to wrap it with <java>. My jar
file has some external dependencies on JAXB, Spring etc. I can only succeed
when I copy it to Ant/lib. Injecting my own classpath into Ant class loader
is too challenging for me.  I believe it is quite common situation for task
writer.  I am interested to learn how to do it too if possible.

thanks again.





Peter Reilly-2 wrote:
> 
> The classpath attribute is not available to the created task.
> 
> In the task one can use MyTask.class.getClassLoader() to
> get the classloader that loaded the task.
> If this is of type AntClassloader, one can use
> AntClassloader.getClassPath()
> to get the path used in the loader.
> 
> However there are a number of issues with this:
>   - the MyTask may be in a system classpath, or other classpath - in which
>     case, the classloader seen will not be the classloader used by
> <taskdef>
>  - there must be other means to achive the same goal.
>    - for example keep a reference to the path use use this refernce for
>    the <java> task.
> 
> Peter
> 
> 
> On 7/30/07, stanS <stanley_siu@ml.com> wrote:
>>
>> Lets say I have a taskdef in the build.xml
>>    <taskdef name="mytask"
>>                  classname="MyTask"
>>                  classpath="path-to/my.jar" />
>>
>> How do I get the 'classpath' part in the custom task that derived from
>> Ant
>> Task class?
>> Could you point me to the right API?
>>
>> I want to use that classpath back with a 'java' task.
>>
>> Thanks
>> Stan.
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Getting-classpath-attribute-of-custom-task-tf4171205.html#a11866788
>> Sent from the Ant - Users mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>> For additional commands, e-mail: user-help@ant.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Getting-classpath-attribute-of-custom-task-tf4171205.html#a11883604
Sent from the Ant - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message