You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by jv...@periapt.com on 2000/11/29 22:12:46 UTC

Defining classpath with Taskdef

Hi,

I think is the Stefan who answered my question about
specifying classpath in the <taskdef> task. I am
trying the following and getting ClassNotFoundExceptions.

If I <echo> out the ${cp} property it is correct and
lists all the jar files that are required. In the snippet
listed below there are two ways of defining the classpath.
I have tried both methods individually and still get
ClassNotFoundExceptions. Is what is listed below the
correct way to specify a classpath for a <taskdef>?

This little snippet is also called via an <ant> task in
a parent build file. I'm not how or if this affects
the classloading. Thanks.

<project name="Torque" default="main" basedir=".">

  <!-- Build classpath -->
  <path id="classpath">
    <fileset dir="../bin">
      <include name="*.jar"/>
    </fileset>
    <fileset dir="../lib">
      <include name="*.jar"/>
    </fileset>
  </path>

  <property name="cp" refid="classpath"/>

  <taskdef name="vtorque" classname="org.apache.turbine.torque.ant.VTorqueTask">
    classpath="${cp}"
    <classpath refid="classpath"/>
  </taskdef>

  ...


Re: Defining classpath with Taskdef

Posted by Stefan Bodewig <bo...@apache.org>.
Stefan Bodewig <bo...@apache.org> wrote:

> Stefan Bodewig <bo...@apache.org> wrote:
> 
> Fixed.

finally.

Stefan

Re: Defining classpath with Taskdef

Posted by Stefan Bodewig <bo...@apache.org>.
Stefan Bodewig <bo...@apache.org> wrote:

> I'm not sure where the problem is,

OK, we invoked Task.execute for tasks not living in targets before
their child elements have been configured. Fixed.

Stefan

Re: Defining classpath with Taskdef

Posted by jv...@periapt.com.

On 30 Nov 2000, Stefan Bodewig wrote:

> <jv...@periapt.com> wrote:
> 
> > I am defining the classpath as you suggested, and this still
> > doesn't work.
> 
> Jason, I've tried your example and it didn't work for me either. 
> 
> I'm not sure where the problem is, but I do have a workaround for
> you. The same construct works, if you put the taskdef into a target,
> so instead of making the taskdef a child of <project>, but the extra
> layer of a prepare target in between.

Thanks Stefan! That works beatifully :-) Much appreciated!

jvz.


Re: Defining classpath with Taskdef

Posted by Stefan Bodewig <bo...@apache.org>.
<jv...@periapt.com> wrote:

> I am defining the classpath as you suggested, and this still
> doesn't work.

Jason, I've tried your example and it didn't work for me either. 

I'm not sure where the problem is, but I do have a workaround for
you. The same construct works, if you put the taskdef into a target,
so instead of making the taskdef a child of <project>, but the extra
layer of a prepare target in between.

Stefan

Re: Defining classpath with Taskdef

Posted by jv...@periapt.com.
Hi Stefan,

I am defining the classpath as you suggested, and this still
doesn't work. Below are the simplest of build files, and
the simplest of tasks and the Taskdef just will not get
loaded. From the output of the debug log it doesn't
look like an attempt is even made to setup
the classpath. If this is known to work, then I'm
doing something really wrong. Everything works fine
as long as I build the classpath in a .bat|.sh script
before running Ant, but that's what I'm trying to
avoid.

-- LOG --

Ant version 1.2 compiled on October 24 2000

Buildfile: build-turbine.xml

...

Setting ro project property: ant.file ->
/home/jvanzyl/js/turbine/build/build-simple.xml
Project base dir set to: /home/jvanzyl/js/turbine/build
   +Task: taskdef

BUILD FAILED

/home/jvanzyl/js/turbine/build/build-simple.xml:3: taskdef class
org.apache.turbine.torque.ant.Si$
java.lang.ClassNotFoundException: org.apache.turbine.torque.ant.SimpleTask
...

-- BUILD FILE --

<project name="Torque" default="main" basedir=".">

  <taskdef name="simple" classname="org.apache.turbine.torque.ant.SimpleTask">
    <classpath>
      <fileset dir="../bin">
        <include name="*.jar"/>
      </fileset>
      <fileset dir="../lib">
        <include name="*.jar"/>
      </fileset>
    </classpath>
  </taskdef>

  <target name="main">
    <simple data="simple"/>
  </target>

</project>

-- TASK

package org.apache.turbine.torque.ant;

import org.apache.tools.ant.*;

public class SimpleTask extends Task
{
    private String data;

    public String getData() { return data; }
    public void setData(String v) { data = v; }
    public void execute() throws BuildException {
    System.out.println(data); }
}



On 30 Nov 2000, Stefan Bodewig wrote:

> <jv...@periapt.com> wrote:
> 
> > Hi,
> > 
> > If I <echo> out the ${cp} property it is correct and
> > lists all the jar files that are required. In the snippet
> > listed below there are two ways of defining the classpath.
> 
> If you are calling the taskdef by means of <ant> and your <path> is
> part of the calling project you'll need to "tunnel" it through the
> property, so the refid version is not supposed to work. 
> 
> Otherwise, both ways should work as well as
> 
> <taskdef name="vtorque" classname="org.apache.turbine.torque.ant.VTorqueTask">
>   <classpath> 
>     <fileset dir="../bin">
>       <include name="*.jar"/>
>     </fileset>
>     <fileset dir="../lib">
>       <include name="*.jar"/>
>     </fileset>
>   </classpath>
> </taskdef>
> 
> You know that a <taskdef> performed in the called project won't have
> any effect on the calling project, right?
> 
> > I have tried both methods individually and still get
> > ClassNotFoundExceptions.
> 
> Where do you get the Exception? Inside the taskdef or when you try to
> use the task?
> 
> I'd suggest to use "ant -debug", this will list where Ant tries to
> load a class from, some classes must be loaded via the system
> classloader (using your system CLASSPATH), others will be loaded by
> Ant. You cannot load any class from a package starting with java. from
> your custom CLASSPATH for example.
> 
> Stefan
> 


Re: Defining classpath with Taskdef

Posted by Stefan Bodewig <bo...@apache.org>.
<jv...@periapt.com> wrote:

> Hi,
> 
> If I <echo> out the ${cp} property it is correct and
> lists all the jar files that are required. In the snippet
> listed below there are two ways of defining the classpath.

If you are calling the taskdef by means of <ant> and your <path> is
part of the calling project you'll need to "tunnel" it through the
property, so the refid version is not supposed to work. 

Otherwise, both ways should work as well as

<taskdef name="vtorque" classname="org.apache.turbine.torque.ant.VTorqueTask">
  <classpath> 
    <fileset dir="../bin">
      <include name="*.jar"/>
    </fileset>
    <fileset dir="../lib">
      <include name="*.jar"/>
    </fileset>
  </classpath>
</taskdef>

You know that a <taskdef> performed in the called project won't have
any effect on the calling project, right?

> I have tried both methods individually and still get
> ClassNotFoundExceptions.

Where do you get the Exception? Inside the taskdef or when you try to
use the task?

I'd suggest to use "ant -debug", this will list where Ant tries to
load a class from, some classes must be loaded via the system
classloader (using your system CLASSPATH), others will be loaded by
Ant. You cannot load any class from a package starting with java. from
your custom CLASSPATH for example.

Stefan