Gradle:无法创建任务或输入taskDef,名称未定义

时间:2014-11-16 19:00:14

标签: ant gradle

我在创建一个ant-task时遇到了问题,并且不知道发生了什么。

这是我的build.gradle:

apply plugin: 'java'

/** Create a classpath for the jarbundler ant-task */
configurations  {
    jarbundler
}

dependencies {
    jarbundler 'net.sourceforge.jarbundler:jarbundler:2.1.0'
}


repositories {
    maven {
        url 'http://ooo-maven.googlecode.com/hg/repository'
    }
}


task distMac(dependsOn: assemble) << {
    println "Create MacOSX distribution....."
    println "Classpath: " + configurations.jarbundler.asPath

    ant.taskDef(name: 'jarbundler',
            classname: 'net.sourceforge.jarbundler.JarBundler',
            classpath: configurations.jarbundler.asPath)
}

失败了:

Dieters-MBP-3:jdbexp rehdie$ gradle distMac
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:distMac
Create MacOSX distribution.....
Classpath: /Users/rehdie/.gradle/caches/modules-2/files-2.1/net.sourceforge.jarbundler/jarbundler/2.1.0/84f1fbcf60aeb90560ba3d554b72217c0836935e/jarbundler-2.1.0.jar
:distMac FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/rehdie/development/projects/jdbexp/build.gradle' line: 25

* What went wrong:
Execution failed for task ':distMac'.
> Problem: failed to create task or type taskDef
  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.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.9 secs

在ant文件中执行相同的操作可以正常工作:

<project name="jdbexp_wrapper" default="dist_osx">

    <taskdef name="jarbundler"
             classpath="/Users/rehdie/.gradle/caches/modules-2/files-2.1/net.sourceforge.jarbundler/jarbundler/2.1.0/84f1fbcf60aeb90560ba3d554b72217c0836935e/jarbundler-2.1.0.jar"
             classname="net.sourceforge.jarbundler.JarBundler"/>


    <target name="hello">
        <echo>Hallo</echo>
    </target>

</project>

有什么想法吗?

jarbundler-jar存在于/Users/rehdie/.gradle/caches/modules-2/files-2.1/net.sourceforge.jarbundler/jarbundler/2.1.0/84f1fbcf60aeb90560ba3d554b72217c0836935e/jarbundler-2.1.0.jar包含一个名为net.sourceforge.jarbundler.JarBundler的类。

不幸的是我没有找到谷歌的任何暗示.....

1 个答案:

答案 0 :(得分:2)

taskdef区分大小写,应全部为小写:

task distMac(dependsOn: assemble) << {
    println "Create MacOSX distribution....."
    println "Classpath: " + configurations.jarbundler.asPath

    ant.taskdef(name: 'jarbundler',
            classname: 'net.sourceforge.jarbundler.JarBundler',
            classpath: configurations.jarbundler.asPath)
}