【问题标题】:Gradle: Call ant from a custom taskGradle:从自定义任务调用 ant
【发布时间】:2014-01-21 16:32:35
【问题描述】:

我是 gradle 新手,正在尝试迁移我们当前基于 Maven 的自动化 JAX-WS 客户端构建项目,因为 gradle 似乎为我们提供了一种更简单的方式来配置新项目的构建。

我遵循了本教程here 并能够为 WSDL 端点生成客户端类。我现在想要实现的是将 Task 定义放在一个新的 DefaultTask 类中以保持 build.gradle 文件更干净,所以我创建了以下文件,将它放在一个新的 Groovy 项目中并使其可用于我的构建:

class WsimportTask extends DefaultTask {
    def List<String> wsdlUrls
    @OutputDirectory
    File destDir

    @TaskAction
    def wsimport() {
        wsdlUrls.each() {
            println "run wsimport for "+ it
            ant {
                sourceSets.main.output.classesDir.mkdirs()  
                destDir.mkdirs()
                taskdef(name:'wsimport',
                    classname:'com.sun.tools.ws.ant.WsImport',
                    classpath:configurations.jaxws.asPath)
                wsimport(keep:true,
                    destdir: sourceSets.main.output.classesDir,
                    sourcedestdir: destDir,
                    wsdl: it)
            }
        }
    }
}

由于我想为我们拥有的每个 Web 服务客户端创建一个包含子项目的单个项目,因此我更改了主项目的 build.gradle 文件并添加了:

ma​​in build.gradle

subprojects {
    configurations {
        wsimport
    }
    dependencies {
        wsimport group: 'com.mycompany.gradle', name: 'tasks', version: '0.0.2-SNAPSHOT'
    }
    task wsimport(type: com.mycompany.gradle.WsimportTask) {
        destDir = file("${buildDir}/generated")
    }
    compileJava.dependsOn(wsimport)
}

子项目本身应该只需要包含以下配置:

子项目 build.gradle

buildscript {
    wsimport {
        wsdlUrls = [
            "http://...endpoint1.wsdl",
            "http://...endpoint2.wsdl"
        ]
    }
}

在主项目上运行 gradle clean build 时,我收到以下消息和异常:

:clean UP-TO-DATE 
:Subproject:clean 
:Subproject:wsimport 
run wsimportfor http://endpoint1.wsdl 
:Subproject:wsimport 
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':Subproject:wsimport'.
....
FAILED Caused by: org.gradle.api.internal.MissingMethodException: Could not find method ant() for arguments [com.mycompany.gradle.WsimportTask$_wsimport_closure1_closure2@16caac05] on task ':Subproject:wsimport'.

所以 gradle 了解我的配置,应用 WSDL 端点并调用我的自定义任务。然后 ant {} 被评估为调用方法 ant() 的本地方法,该方法不存在。这对我来说很有意义,但是我怎样才能实现从这个自定义 gradle 任务中调用实际 ant wsimport 任务的目标?


解决方案感谢彼得的回答。我想我确实开始了解任务是如何连接到我的构建脚本中的。我为未来的奋斗者粘贴了完整的任务,因为我不认为创建这样一个任务的整个过程(尤其是第一次)有点难以理解:

class WsimportTask extends DefaultTask {
    def List<String> wsdlUrls
    @OutputDirectory
    File outDir

    @TaskAction
    def wsimport() {
        wsdlUrls.each() {
            def temp = it
            println "run wsimport for "+ temp
            project.sourceSets.main.output.classesDir.mkdirs()  
            outDir.mkdirs()
            def classpath = project.configurations.jaxws.asPath
            def destDir = project.sourceSets.main.output.classesDir
            project.ant {
                taskdef(name:'wsimport',
                    classname:'com.sun.tools.ws.ant.WsImport',
                    classpath:classpath)
                wsimport(keep:true,
                    destdir: destDir,
                    sourcedestdir: outDir,
                    wsdl: temp)
            }
        }
    }
}

同样值得注意的是,不能像 Peter 指出的那样在 build.gradle 文件的 buildscipt 块中定义任务

【问题讨论】:

    标签: java ant gradle wsimport


    【解决方案1】:

    与构建脚本不同,任务或插件类没有隐式 Project 上下文。因此,它是project.antproject.file 等。此外,必须在buildscript 块之外声明和配置任务(参见子项目build.gradle)。

    【讨论】:

      【解决方案2】:

      试试这个:

      
      @TaskAction
      def wsimport() {
          wsdlUrls.each() {
              println "run wsimport for "+ it
      
              sourceSets.main.output.classesDir.mkdirs()  
              destDir.mkdirs()
      
              ant.taskdef(name:'wsimport',
                      classname:'com.sun.tools.ws.ant.WsImport',
                      classpath:configurations.jaxws.asPath)
              ant.wsimport(keep:true,
                      destdir: sourceSets.main.output.classesDir,
                      sourcedestdir: destDir,
                      wsdl: it)       
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-07
        • 2021-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多