Executing wsdl2Java in Gradle - bahkified/Notes GitHub Wiki

For whatever reason, you might want to use a command line tool from within the gradle build. In this case, several things need to be declared before the command line tool can successfully be executed. For this example, the Apache CXF tool, wsdl2java, will be executed during a dedicated build task.

There are other, possibly better ways to structure this. This code defines a task that executes the wsdl2Java command line tool provided by Apache CXF to generate client code for a SOAP based web service, defined in a WSDL file as well as necessary dependencies. When the defined task is executed, Java client code is generated and put into the directory specified by generatedWsdlDir. This directory is also specified as a source directory, so that the generated code can be found and used.

repositories {
    mavenCentral()
    mavenLocal()
}

sourceSets {
    genSrc {
        java {
            srcDir 'generated-sources/cxf'
        }
    }
}

project.ext {
    wsdlDir = file("src/main/resources/wsdl")
    generatedWsdlDir = file("generated-sources/cxf")
    wsdlToGenerate = ['-d', generatedWsdlDir, '-fe', 'jaxws21', "$wsdlDir/WSDLFileName.wsdl"]
    cxfVersion = '2.7.8'
    cxfArtifacts = [
        'cxf-rt-frontend-jaxws',
        'cxf-rt-frontend-jaxrs',
        'cxf-rt-transports-http',
        'cxf-rt-rs-client',
        'cxf-rt-rs-service-description'
    ]
}

configurations {
    cxfTool
}

ext {
    cxfToolArtifacts = [
        'cxf-tools-wsdlto-frontend-jaxws',
        'cxf-tools-wsdlto-databinding-jaxb',
        'cxf-tools-common',
        'cxf-tools-wsdlto-core'
    ]
}

dependencies {
    cxfToolArtifacts.each { artifact ->
        cxfTool "org.apache.cxf:$artifact:$cxfVersion"
    }
}

task wsdl2Java {
    if (!wsdlDir.listFiles()) {
        // nothing to do
    } else {
        inputs.files wsdlDir.listFiles()
        outputs.files generatedWsdlDir
        doLast {
            javaexec {
                classpath configurations.cxfTools
                main = 'org.apache.cxf.tools.wsdlto.WSDLToJava'
                args = wsdlToGenerate
                args.add(0, '-xjc-mark-generated')
                args.add(0, '-client')
                systemProperties = ['exitOnFinish' : 'TRUE']
            }
        }
    }
}

Other examples

Resources

Apache CXF: https://cxf.apache.org/docs/wsdl-to-java.html