How do I call ant in a custom task


(Dan Ciarniello) #1

I have the following task which I would like to convert to a custom task so that it is reusable:

configurations {
    jaxws
}
  dependencies {
    jaxws 'com.sun.xml.ws:jaxws-tools:2.1.7'
}
  task wsimport
{
    destDir = file("${buildDir}/generated")
    wsdlSrc = file('src/main/wsdl/SDD2.wsdl')
    inputs.file wsdlSrc
    outputs.dir destDir
    doLast{
        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: wsdlSrc,
                    package:'com.cucbc.sdd2.ws')
        }
    }
}

And this is the custom task:

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.OutputDirectory
  configurations {
    jaxws
}
  dependencies {
    jaxws 'com.sun.xml.ws:jaxws-tools:2.1.7'
}
  class JaxWsTask extends DefaultTask {
    String wsdlSrc
    String packageName
      @OutputDirectory
    File outputDir = new File("build/generated")
      @TaskAction
    def generate() {
        ant {
            project.sourceSets.main.output.classesDir.mkdirs()
            outputDir.mkdirs()
            taskdef(name:'wsimport',
                    classname:'com.sun.tools.ws.ant.WsImport',
                    classpath:project.configurations.jaxws.asPath)
              wsimport(keep:true,
                    destdir:project.sourceSets.main.output.classesDir,
                    sourcedestdir:outputDir,
                    wsdl: wsdlSrc,
                    package: packageName)
        }
    }
}

The problem is that this give an error:

Could not find method ant() for arguments [JaxWsTask$_generate_closure1@7416f46a] on task …

What am I missing here?

Thanks, Dan.


(Peter Niederwieser) #2

In a task or plugin class, there is no implicit ‘project’ context, so it’s ‘project.ant’. Same for all other properties and methods on the ‘Project’ class.


(Dan Ciarniello) #3

That did the trick. I hate it when it’s such a simple thing :slight_smile:

Thanks, Dan.