XML Schema generation from Java 8 code and Gradle integration

Using Java 8 and Gradle I had to generate XSD schema. Most of the Gradle plugins out there are not compatible with Java 8 yet. So I tweaked my way and following is how I did it.

I had following model class supporting both JAXB and JSON.

@XmlType(namespace = "http://com.invalid/1.0/request/schema") @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "Person") @JsonRootName(value = "Person") @JsonInclude(Include.NON_NULL) public class Person { private String name; private Gender gender; public String getName() { return name; } public void setName(String name) { this.name = name; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } }

Following is the Gender enumeration

public enum Gender { MALE, FEMALE }

Create a package-info.java file (if this file is not created and namespace is not same in every where multiple schema files are generated.)

@javax.xml.bind.annotation.XmlSchema(namespace = "http://com.invalid/1.0/request/schema", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package com.test;

Add the schema generation task in your gradle build script

task schemagen () { doLast { ext.schemasDir = file("${buildDir}/schemas") project.ant { mkdir(dir: schemasDir) }
ant.taskdef(name: 'schemagen', classname: 'com.sun.tools.jxc.SchemaGenTask', classpath: configurations.jaxb.asPath) ant.schemagen(srcdir: new File('src/main/java/com/test'), destdir: schemasDir, includeAntRuntime:'false') { schema { namespace = 'http://com.invalid/1.0/request/schema' //Known issue: due to some internal jaxb issue the file name property doesn't take effect file = 'request.xsd' } classpath { pathElement(path: configurations.jaxb.asPath ) } } } } dependencies { //other dependencies //Add following dependency this overrides the jaxb (jakson, and findbugs are added as schemagen fails to find definitions of some annotations) jaxb ( 'org.codehaus.mojo:jaxb2-maven-plugin:2.1', 'org.apache.avro:avro-tools:1.7.5', 'org.apache.avro:avro:1.7.5' , 'com.fasterxml.jackson.core:jackson-annotations:2.6.0', 'com.google.code.findbugs:jsr305:3.0.0' ) }

Now do a Gradle build and following schema is generated in the build/schemas directory

<xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="http://com.invalid/1.0/request/schema" xmlns:tns="http://com.invalid/1.0/request/schema" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person" type="tns:person"/> <xs:complexType name="person"> <xs:sequence> <xs:element name="name" type="xs:string" minOccurs="0"/> <xs:element name="gender" type="tns:gender" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:simpleType name="gender"> <xs:restriction base="xs:string"> <xs:enumeration value="MALE"/> <xs:enumeration value="FEMALE"/> </xs:restriction> </xs:simpleType> </xs:schema>

About: Muhammad Ali

I am a Java and Tibco expert. I have Master's degree in software engineering of distributed systems from Royal Institute of technology KTH Stockholm Sweden and I am an expert software engineer and integrator with over a decade of industry experience from Europe, Gulf and Asia regions. I am a strong opensource believer and encourage "sharing as caring" principle.