ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Guru Balse <guru.ba...@oracle.com>
Subject Re: ANT Task classpath?
Date Fri, 03 Sep 2004 00:20:21 GMT
Hi Jim,

The problem is that setting the classpath within taskdef does not seem to work (at least not
in ANT 1.6.0).  A few simple files I have that illustrate this are as follows:

C:\ant_learn
|   TestCP.java		Task that instantiates a specified object (to test the classpath problem)
|   test_cp.xml		Task driver
\---test
        Test.jar	Test jar file
        Test.java	Test class - prints out Hello when constructed

c:/ant_learn/test_cp.xml contains
---------------------------------
<?xml version="1.0"?>
<project name="test" default="test">
  <taskdef name="test_cp" classname="TestCP">
    <classpath>
      <fileset dir="c:/ant_learn/test">
        <include name="Test.jar"/>
      </fileset>
    </classpath>
  </taskdef>
  <target name="test">
    <test_cp createObject="Test" />
  </target>
</project>
---------------------------------
c:/ant_learn/TestCP.java contains
---------------------------------
import org.apache.tools.ant.*;
public class TestCP extends Task {
  private String createObject = null;
  public void setCreateObject(String createObject) { this.createObject = createObject; }
  public void execute() throws BuildException {
    try {
      if (createObject == null) {
          log("Must set object to create!", Project.MSG_ERR);
      } else {
	log("Creating object of type " + createObject, Project.MSG_INFO);
	(Class.forName(createObject)).newInstance();
      }
    } catch (Exception e) {
      log("Exception encountered: " + e, Project.MSG_ERR);
      throw new BuildException("Build failed!", e);
    }
  }
}
---------------------------------
c:/ant_learn/test/Test.java contains
---------------------------------
public class Test {
  public Test() {
    System.out.println("Hello there!");
  }
}
---------------------------------

c:/ant_learn/test is NOT in my classpath, so when I run ant -f test_cp.xml, I get:

Buildfile: C:\ant_learn\test_cp.xml

test:
  [test_cp] Creating object of type Test
  [test_cp] Exception encountered: java.lang.ClassNotFoundException: Test

BUILD FAILED
C:\ant_learn\test_cp.xml:11: Build failed!

Total time: 4 seconds

Tool completed successfully

BUT if I add c:/ant_learn/test to my CLASSPATH in a command prompt, I get:
Buildfile: test_cp.xml

test:
  [test_cp] Creating object of type Test
  [test_cp] Hello there!

BUILD SUCCESSFUL
=======================================================
What am I doing wrong?  I have tried various combinations of pathelement, filelist, fileset,
and dirset, but to no avail.  How do I add c:/ant_learn/test/Test.jar in my task class path
without having to set it in my environment?

Thanks.

jfuller@ruminate.co.uk wrote:
> 
> Guru Balse <guru.balse@oracle.com> wrote ..
> > I am sure this has been discussed before, but I could not find it in the
> > archives.  If my task depends on a particular set of jars in the classpath,
> > how do I ensure that the task will work?  While I can set a classpath in
> > the environment - I would like to avoid such a solution, or hack, as some
> > of you would call this! :-)
> 
> check out the following;
> 
> <?xml version="1.0"?>
> <project>
> 
> <!-- ===================================================== -->
> <!-- Library Paths                                         -->
> <!-- ===================================================== -->
> <path id="checkstyle.class.path">
>         <pathelement location="path_to_checkstyle"/>
> </path>
> 
> <!-- ===================================================== -->
> <!-- taskdef                                               -->
> <!-- ===================================================== -->
> 
> <taskdef name="yourtask" classname="org.ant.taskdefs.testtask">
>     <classpath>
>       <pathelement path="${classpath}"/>
>       <fileset dir="lib">
>         <include name="**/*.jar"/>
>       </fileset>
>       <pathelement location="classes"/>
>       <dirset dir="${build.dir}">
>         <include name="apps/**/classes"/>
>         <exclude name="apps/**/*Test*"/>
>       </dirset>
>       <filelist refid="third-party_jars"/>
>     </classpath>
> </taskdef>
...
> 
> hth, Jim Fuller
> 

-- 
Guru Balse
   Oracle - Server Technologies - iAS Integration QA
   Work: 650.506.1611 (1op668 - HQ)


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


Mime
View raw message