ant:无法创建任务或类型

时间:2011-11-01 16:18:32

标签: ant

(是的,我已根据本论坛和JavaRanch等许多其他类似问题的答案阅读和播放 - 无济于事。)

我已根据Apache doc创建了一个自定义ant任务。

运行蚂蚁,我得到:

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)

这符合我的 build.xml 文件中的目标:

<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>

我在路径* $ ANT_HOME / lib *上有 ant-contrib-1.0b3.jar 。我在该路径上有 sqlscriptpreprocessor.jar ,以及我构建的本地类路径。

为了驱除这个问题,我尝试了以下一组语句的组合,我已经通过Google在整个地方找到了这些语句,这意味着其中一个&lt; taskdef ant-contrib&gt;与其中一个&lt; taskdef sqlscriptpreprocessor&gt;构造,第一个与后者之一,第一个与后者中的两个,所有在一起,没有一个,等等。

<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>

令人沮丧的是,它并不像他们所说的那样容易向蚂蚁添加自定义任务。

我非常感谢任何和所有评论。

谢谢,

拉​​斯

3 个答案:

答案 0 :(得分:16)

我现在已经挣扎了两天了。我似乎得到的答案由http://ant.apache.org/manual/tutorial-writing-tasks.html暗示:

&lt; taskdef name =“sqlscriptpreprocessor”...&gt;必须将元素放在使用它的目标内部,在本例中为 mysql-preprocess 。我没有这样做。现在,我有:

<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>

我不明白为什么必须在使用它的目标元素中定义我的自定义任务,但这对我来说没问题。我不确定它是否尽可能优雅,但它现在有效。

对不起回答我自己的问题;它发生在一两次之前,对我来说似乎很蹩脚。我希望它可以帮助别人。

感谢我在过去两天阅读的所有论坛和帖子中围绕此主题的所有回复。

答案 1 :(得分:1)

在构建文件中添加下一行解决了问题:

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

答案 2 :(得分:0)

对我来说,这则消息:

/Users/packrd/Downloads/source/build.xml:435: The following error occurred while executing this line:
/Users/packrd/Downloads/source/build.xml:440: The following error occurred while executing this line:
/Users/packrd/Downloads/source/Documentation/docs/build.xml:221: The following error occurred while executing this line:
/Users/packrd/Downloads/source/Documentation/docs/asciidoc-macros.xml:249: Problem: failed to create task or type antlib:net.sf.antcontrib:switch
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.
No types or tasks have been defined in this namespace yet

This appears to be an antlib declaration. 
Action: Check that the implementing library exists in one of:
        -/usr/local/Cellar/ant/1.10.8/libexec/lib
        -/Users/packrd/.ant/lib
        -a directory added on the command line with the -lib argument

我没有安装ant-contrib,请在OS X上修复:

brew install ant-contrib