ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Reilly <peter.rei...@corvil.com>
Subject Re: creating a custom Condition
Date Thu, 17 Jun 2004 07:20:19 GMT
You need to use <typedef> and not <taskdef>

One can mix typedefs and taskdefs  in an xml resource
file:

magmic.xml
<antlib>
    <typedef name="exists" classname="com.magmic.anttasks.Exists"/>
    <taskdef name="mytask" classname="com.magmic.anttasks.MyTask"/>
</antlib>

build.xml
<typedef resource="magmic.xml"/>

If you place the definitions a resource called com/magmic/antlib.xml, 
you can
use the preferred xml namespace short-cut for third-party ant type/task 
plugins:

<project xmlns:mg="antlib:com.magmic"
         xmlns:ac="antlib:net.sf.antcontrib">
   <ac:if>
      <mg:exists file="bin"/>
      <ac:then>
         <echo>The file 'bin', it does exist!</echo>
      </ac:then>
   </ac:if>
</project>

The reason it is preferred to use xml namespaces for third-partry tasks
is that it is possible for a type/task of the name "exists" to be present
in ant-contrib or ant core at some time in the future.

Peter
  
Jeffrey Bacon wrote:

> I would like to create a custom condition (like the equals condition) 
> that I can use in the <if> task from the ant-contrib library.  I 
> looked at the source code to the Equals.java file and replicated that 
> however Ant doesn't like me to define it as a task since it doesn't 
> have an execute() method.  Below is my build.xml snippit, properties 
> file line and code for a sample custom condition.  Can anyone help me 
> out as to what's wrong?
>
> build.xml:
>   <taskdef resource="magmictasks.properties" />
>
> magmictasks.properties:
>   exists=com.magmic.anttasks.Exists
>
> usage in build.xml:
>     <if>
>       <exists file="bin" />
>       <then>
>         <echo>Not clean</echo>
>       </then>
>     </if>
>
> build.xml output:
>
> $ ant clean build install
> Buildfile: build.xml
> No public execute() in class com.magmic.anttasks.Exists
>
> BUILD FAILED
> C:\workspace\MagmicAntTasks\build.xml:6: No public execute() in class 
> com.magmic.anttasks.Exists
>
> Total time: 0 seconds
>
> ============== CODE ==============
>
> package com.magmic.anttasks;
>
> import java.io.File;
>
> import org.apache.tools.ant.BuildException;
> import org.apache.tools.ant.Task;
> import org.apache.tools.ant.taskdefs.condition.Condition;
>
>
> /**
>  * @author <a href="mailto:jbacon@magmic.com">Jeffrey Bacon</a>
>  * @version $Revision: 1 $
>  */
> public class Exists implements Condition {
>
>   private File file;
>
>   public void setFile(File file) {
>       this.file = file;
>   }
>
>   /**
>    * @see org.apache.tools.ant.taskdefs.condition.Condition#eval()
>    */
>   public boolean eval() throws BuildException {
>     return this.file != null && this.file.exists();
>   }
> }
>
>



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


Mime
View raw message