Gradle Configuration to generate Java classes from WSDL with JAXB

Tags: Java SOAP Web Service WSDL Gradle

In this blog post we will learn the step by step guide to configure gradle build file in order to generate Java classes from a WSDL file. This configure will be useful when you are working on project related to SOAP web service.

Let start with an empty Java project with build.gradle file as below

group 'simplesolution.dev'
version '1.0.0'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    
}

For example, I have a SOAP web service to get all employees of a company with the WSDL as below

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:tns="https://simplesolution.dev/EmployeeService/"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  name="EmployeeService"
                  targetNamespace="https://simplesolution.dev/EmployeeService/">
    <wsdl:types>
        <xsd:schema targetNamespace="https://simplesolution.dev/EmployeeService/">
            <xsd:element name="GetAllEmployees">
                <xsd:complexType />
            </xsd:element>
            <xsd:element name="GetAllEmployeesResponse">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="Employee" minOccurs="0" maxOccurs="unbounded">
                            <xsd:complexType>
                                <xsd:sequence>
                                    <xsd:element name="ID" type="xsd:string"></xsd:element>
                                    <xsd:element name="FirstName" type="xsd:string"></xsd:element>
                                    <xsd:element name="LastName" type="xsd:string"></xsd:element>
                                </xsd:sequence>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
    </wsdl:types>

    <wsdl:message name="GetAllEmployeesRequest">
        <wsdl:part name="parameters" element="tns:GetAllEmployees"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="GetAllEmployeesResponse">
        <wsdl:part name="parameters" element="tns:GetAllEmployeesResponse"></wsdl:part>
    </wsdl:message>

    <wsdl:portType name="EmployeeService">
        <wsdl:operation name="GetAllEmployees">
            <wsdl:input message="tns:GetAllEmployeesRequest"></wsdl:input>
            <wsdl:output message="tns:GetAllEmployeesResponse"></wsdl:output>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="EmployeeServiceSOAP" type="tns:EmployeeService">
        <soap:binding style="document"
                      transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="GetAllEmployees">
            <soap:operation
                    soapAction="https://simplesolution.dev/EmployeeService/GetAllEmployees" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="EmployeeService">
        <wsdl:port binding="tns:EmployeeServiceSOAP" name="EmployeeServiceSOAP">
            <soap:address location="https://simplesolution.dev/EmployeeService" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

We need to put these WSDL file under project resources directory at:
src/main/resources/employee.wsdl

First of all you need to add configuration and dependencies as below to build.gradle file

configurations {
    jaxb
}

dependencies {
    jaxb (
            'com.sun.xml.bind:jaxb-core:2.3.0.1',
            'com.sun.xml.bind:jaxb-xjc:2.3.2',
            'com.sun.xml.bind:jaxb-impl:2.3.2'
    )
}

Then we will implement a new gradle task to generate Java classes from employee.wsdl

task generateJavaClasses {
    System.setProperty('javax.xml.accessExternalSchema', 'all')
    def jaxbTargetDir = file("src/main/java/")
    doLast {
        jaxbTargetDir.mkdirs()
        ant.taskdef(
                name: 'xjc',
                classname: 'com.sun.tools.xjc.XJCTask',
                classpath: configurations.jaxb.asPath
        )
        ant.jaxbTargetDir = jaxbTargetDir
        ant.xjc(
                destdir: '${jaxbTargetDir}',
                package: 'simplesolution.dev',
                schema: 'src/main/resources/employee.wsdl',
                language: 'WSDL'
        )
    }
}
There are few things to notice in gradle task above:

  • jaxbTargetDir configure to generate Java classes to src/main/java/ directory
  • There is default package of generated source is ‘simplesolution.dev’
  • The WSDL schema file is ‘src/main/resources/employee.wsdl’

The final build.gradle file as below

group 'simplesolution.dev'
version '1.0.0'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    jaxb
}

dependencies {
    jaxb (
            'com.sun.xml.bind:jaxb-core:2.3.0.1',
            'com.sun.xml.bind:jaxb-xjc:2.3.2',
            'com.sun.xml.bind:jaxb-impl:2.3.2'
    )
}

task generateJavaClasses {
    System.setProperty('javax.xml.accessExternalSchema', 'all')
    def jaxbTargetDir = file("src/main/java/")
    doLast {
        jaxbTargetDir.mkdirs()
        ant.taskdef(
                name: 'xjc',
                classname: 'com.sun.tools.xjc.XJCTask',
                classpath: configurations.jaxb.asPath
        )
        ant.jaxbTargetDir = jaxbTargetDir
        ant.xjc(
                destdir: '${jaxbTargetDir}',
                package: 'simplesolution.dev',
                schema: 'src/main/resources/employee.wsdl',
                language: 'WSDL'
        )
    }
}

That’s it for configuration, now you can run the gradle task to generate Java source:

gradlew generateJavaClasses

After finished running the gradle task, you can see the Java source files generated as below:

Genarated Java source files

Download Source Code

That’s all for a simple solution to generate Java source code from WSDL file.
The source code in this blog can be found at: https://github.com/simplesolutiondev/WSDLtoJavaClassUsingGradle

Happy Coding!