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")
@XmlRootElement(name = "Person")
@JsonRootName(value = "Person")
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 {
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
configurations {
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.apache.avro:avro:1.7.5' ,
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:element name="name" type="xs:string" minOccurs="0"/>
 <xs:element name="gender" type="tns:gender" minOccurs="0"/>
<xs:simpleType name="gender">
 <xs:restriction base="xs:string">
 <xs:enumeration value="MALE"/>
 <xs:enumeration value="FEMALE"/>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s