ant: failed to create task or type

ant
58,698

Solution 1

I've struggled with this for two days now. The answer I appear to be getting is implied by http://ant.apache.org/manual/tutorial-writing-tasks.html:

The <taskdef name="sqlscriptpreprocessor" ...> element must be placed down inside the target that uses it, in this case, mysql-preprocess. I was not doing this. Now, I have:

<target name="mysql-preprocess"
        description="Preprocess MySQL database scripts into one file">
    <taskdef name="sqlscriptpreprocessor"
             classname="com.etretatlogiciels.ant.task.SqlScriptPreprocessor"
             classpath="${basedir}/lib/ant-tasks/sqlscriptpreprocessor.jar" />
    <sqlscriptpreprocessor inputfilepath="${basedir}/extras/blackpearl.sql.in"
                          outputfilepath="${basedir}/extras/blackpearl.sql" />
</target>

I don't understand why my custom task must be defined in the target element that consumes it, but this is fine by me. I'm not certain this is couched as gracefully as it could be, but it works now.

I'm sorry for answering my own question; it's happened before once or twice and it seems pretty lame of me. I just hope it helps someone else.

Thanks for all the replies surrounding this topic in all the forums and posts I read over the last two days.

Solution 2

Adding the next line in build file solved the problem:

<taskdef resource="net/sf/antcontrib/antlib.xml" />
Share:
58,698
Russ Bateman
Author by

Russ Bateman

Old C/assembly guy who converted to Java/JEE in 2005. I am engaged in back-end work, especially ReSTful based work. Other technologies include SQL, Hibernate, HTML/CSS, ant, Maven, Git. I do have some passing acquaintance with front-end technologies such as JSF and Facelets. Yesteryear: JSPs and servlets. I use Eclipse as my IDE, but I prefer editing in Vim and using the Unix/Linux command line. I've also dabbled successfully in Android. I'm an Agile (Scrum) and test-driven development adept.

Updated on May 20, 2020

Comments

  • Russ Bateman
    Russ Bateman almost 4 years

    (Yes, I've read and played around based on answers to similar questions in this forum and in many others such as JavaRanch--to no avail yet.)

    I've created a custom ant task according to Apache doc.

    Running ant, I get:

    BUILD FAILED
    /home/russ/blackpearl/fun/build.xml:121: Problem: failed to create task or type sqlscriptpreprocessor
    Cause: The name is undefined.
    Action: Check the spelling.
    Action: Check that any custom tasks/types have been declared.
    Action: Check that any <presetdef>/<macrodef> declarations have taken place.
    at org.apache.tools.ant.UnknownElement.getNotFoundException(UnknownElement.java:487)
    at org.apache.tools.ant.UnknownElement.makeObject(UnknownElement.java:419)
    at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163)
    at org.apache.tools.ant.Task.perform(Task.java:347)
    

    This is pursuant to a target in my build.xml file:

    <target name="mysql-preprocess"
            description="Preprocess MySQL database scripts into one file">
        <sqlscriptpreprocessor inputfilepath="${basedir}/extras/blackpearl.sql.in"
                              outputfilepath="${basedir}/extras/blackpearl.sql" />
    </target>
    

    I have ant-contrib-1.0b3.jar on the path *$ANT_HOME/lib*. I have sqlscriptpreprocessor.jar on that path, plus the local classpath for my build.

    In an attempt to exorcise this problem, I've tried every combination of the following set of statements, which I've picked up all over the place via Google, meaning one of the <taskdef ant-contrib> with one of the <taskdef sqlscriptpreprocessor> constructs, two of the first with one of the latter, one of the first with two of the latter, all together, none of them, etc.

    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" />
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="/home/russ/dev/downloads/ant-contrib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>
    
    <taskdef name="sqlscriptpreprocessor" classname="com.etretatlogiciels.ant.task.SqlScriptPreprocessor" />
    <taskdef resource="${basedir}/lib/ant-tasks/SqlScriptPreprocessor.properties"
             classpath="${basedir}/lib/ant-tasks/sqlscriptpreprocessor.jar" />
    <taskdef resource="${basedir}/lib/ant-tasks/SqlScriptPreprocessor.properties">
        <classpath>
            <pathelement location="${basedir}/lib/ant-tasks/sqlscriptpreprocessor.jar" />
        </classpath>
    </taskdef>
    

    It's frustrating that it's not as easy as they say to add custom tasks to ant.

    I would greatly appreciate any and all comments.

    Thanks,

    Russ

  • Geoff Langenderfer
    Geoff Langenderfer about 2 years
    You've given me an idea to track down my bug. Thanks Russ.