You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Gerry Healy <ni...@mac.com> on 2006/08/15 16:59:06 UTC

Running ant from the API

Hi there,

I am having trouble using third party ant tasks, with nested elements, 
from the Ant API. For example the ant-contrib IfTask and the svnant 
SvnTask.

I am able to use both of these tasks in build scripts without a problem, 
when running ant from the command line. But I want to use them from the 
Ant API. However when I do this with Ant 1.6.0 I get an error similar to 
below, which is clearly not correct:

 build.xml:215: Class net.sf.antcontrib.logic.IfTask doesn't support the 
nested "equals" element.

With Ant 1.6.5 I get:

 build.xml:215: java.lang.NullPointerException

I'm running:

RedHat 9
Java 1.4.2 (build 1.4.2_03-b02)
Ant 1.6.0 (compiled on December 18 2003)  and 1.6.5 (compiled on June 2 
2005)

Below is some sample code and a link to a working (but broken) project 
that demonstrates the problem.

Any help would be appreciated. Thanks,

Gerry

SAMPLE CODE:

The section of the build that is failing is trivial:

  <if>
    <equals arg1="yes" arg2="no" />
    <then>
      <echo message="same"/>
    </then>
    <else>
      <echo message="different"/>
    </else>
  </if>

And the taskdef is declared as below:

<taskdef resource="net/sf/antcontrib/antlib.xml">
  <classpath refid="antlib.classpath" />
</taskdef>


I've also tried the following declarations, which makes no difference

<taskdef resource="net/sf/antcontrib/antlib.xml">
  <classpath>
    <pathelement location="/full/path/to/ant-contrib.jar" />
  </classpath>
</taskdef>

<taskdef name="svn" classname="net.sf.antcontrib.logic.IfTask">
  <classpath refid="antlib.classpath" />
</taskdef>


The relevent API code is:

String buildFile = "/path/to/build/file";
String buildDir = "/path/to/build/dir";

try {

  project.fireBuildStarted();
  project.init();

  project.setBaseDir( new File( buildDir ));
  project.setUserProperty( "ant.file", buildFile);
  project.setUserProperty( "ant.version", Main.getAntVersion());

  ProjectHelper projHelper = new ProjectHelperImpl();
  projHelper.parse(project, new File(buildFile));
  project.executeTarget( target );

} catch( Throwable ex) {

  error = ex;              
} finally {

  project.fireBuildFinished(error);

}


EXAMPLE PROJECT:

http://www.soci.org.uk/anttest.zip

Files:
build.xml: buids AntTest.class
anttest.sh: runs AntTest, which uses Ant API
anttest.xml: build file  used by anttest.sh
 
to build:
# ant

to run:
# ./anttest.sh


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


Re: Running ant from the API

Posted by Gerry Healy <ni...@mac.com>.
This may be a repeat as I also posted this problem to the user list.

I've solved this myself in the end, but it was a little bit tricky. I 
was creating and instance of ProjectHelperImpl directly, whereas it 
seems I should have been using the static method 
ProjectHelper.getProjectHelper().

I only needed an instance of ProjectHelper to avoid the compile warnings 
for the depracated ProjectHelper.configureProject() method. However 
there were no docs or code examples I could find on how to use the 
recommended parse() method.

Perhaps the docs of the ProjectHelper class could include a code segment 
showing how to instansiate the class and use the parse method. e.g.

  ProjectHelper ph = ProjectHelper.getProjectHelper();
  ph.parse(project, buildFile);

Thanks,

Gerry

Gerry Healy wrote:
> Hi there,
>
> I am having trouble using third party ant tasks, with nested elements, 
> from the Ant API. For example the ant-contrib IfTask and the svnant 
> SvnTask.
>
> I am able to use both of these tasks in build scripts without a 
> problem, when running ant from the command line. But I want to use 
> them from the Ant API. However when I do this with Ant 1.6.0 I get an 
> error similar to below, which is clearly not correct:
>
> build.xml:215: Class net.sf.antcontrib.logic.IfTask doesn't support 
> the nested "equals" element.
>
> With Ant 1.6.5 I get:
>
> build.xml:215: java.lang.NullPointerException
>
> I'm running:
>
> RedHat 9
> Java 1.4.2 (build 1.4.2_03-b02)
> Ant 1.6.0 (compiled on December 18 2003)  and 1.6.5 (compiled on June 
> 2 2005)
>
> Below is some sample code and a link to a working (but broken) project 
> that demonstrates the problem.
>
> Any help would be appreciated. Thanks,
>
> Gerry
>
> SAMPLE CODE:
>
> The section of the build that is failing is trivial:
>
>  <if>
>    <equals arg1="yes" arg2="no" />
>    <then>
>      <echo message="same"/>
>    </then>
>    <else>
>      <echo message="different"/>
>    </else>
>  </if>
>
> And the taskdef is declared as below:
>
> <taskdef resource="net/sf/antcontrib/antlib.xml">
>  <classpath refid="antlib.classpath" />
> </taskdef>
>
>
> I've also tried the following declarations, which makes no difference
>
> <taskdef resource="net/sf/antcontrib/antlib.xml">
>  <classpath>
>    <pathelement location="/full/path/to/ant-contrib.jar" />
>  </classpath>
> </taskdef>
>
> <taskdef name="svn" classname="net.sf.antcontrib.logic.IfTask">
>  <classpath refid="antlib.classpath" />
> </taskdef>
>
>
> The relevent API code is:
>
> String buildFile = "/path/to/build/file";
> String buildDir = "/path/to/build/dir";
>
> try {
>
>  project.fireBuildStarted();
>  project.init();
>
>  project.setBaseDir( new File( buildDir ));
>  project.setUserProperty( "ant.file", buildFile);
>  project.setUserProperty( "ant.version", Main.getAntVersion());
>
>  ProjectHelper projHelper = new ProjectHelperImpl();
>  projHelper.parse(project, new File(buildFile));
>  project.executeTarget( target );
>
> } catch( Throwable ex) {
>
>  error = ex;              } finally {
>
>  project.fireBuildFinished(error);
>
> }
>
>
> EXAMPLE PROJECT:
>
> http://www.soci.org.uk/anttest.zip
>
> Files:
> build.xml: buids AntTest.class
> anttest.sh: runs AntTest, which uses Ant API
> anttest.xml: build file  used by anttest.sh
>
> to build:
> # ant
>
> to run:
> # ./anttest.sh
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>


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