Generating jaxb classes from xsd using Gradle

This tutorial will show you how to generate Java classes from XSD file using JAXB API and Gradle. Generating JAXB classes from xsd using Gradle is not an easy task because Gradle does not have yet any ready-made plugin like Maven. So to generate Java classes from XSD schema using Gradle you have to write an an Ant Task. The below example will show you how to do it.

Related Posts:


Java at least 8, JAXB 2 API, Gradle 6.5.1

Project Setup

You can create gradle based project in your favorite IDE. The name of the project is java-jaxb-gradle.

The build.gradle script is given below:

plugins {
    id 'java-library'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

// "src/generated-sources/jaxb"

configurations {

dependencies {
    jaxb (

task jaxb {
    System.setProperty('javax.xml.accessExternalSchema', 'all')
    def jaxbTargetDir = file("src/generated-sources/jaxb")
    doLast {
			name: 'xjc',
			classname: '',
			classpath: configurations.jaxb.asPath
        ant.jaxbTargetDir = jaxbTargetDir
			destdir: '${jaxbTargetDir}',
			package: 'com.roytuts.jaxb',
			schema: 'src/main/resources/xsd/shiporder.xsd'

compileJava.dependsOn jaxb

In the above build script, look I have written an Ant task to generate Java classes from XSD file. Gradle does not have a JAXB plugin (yet), it involves an ant task, which makes it a bit more complex than maven.

XSD File

I am using sample XSD file (, so put the below shiporder.xsd file under src/main/resources/xsd folder.

<xs:schema xmlns:xs=""
	<xs:element name='comment' type='xs:string' />
	<xs:element name='purchaseOrder'
		type='tns:PurchaseOrderType' />
	<xs:complexType name='USAddress'>
				Purchase order schema
			<xs:element name='name' type='xs:string' />
			<xs:element name='street' type='xs:string' />
			<xs:element name='city' type='xs:string' />
			<xs:element name='state' type='xs:string' />
			<xs:element name='zip' type='xs:decimal' />
		<xs:attribute name='country' type='xs:NMTOKEN' fixed='US' />
	<xs:simpleType name='SKU'>
		<xs:restriction base='xs:string'>
			<xs:pattern value='\d{3}\w{3}' />
	<xs:complexType name='Items'>
			<xs:element name='item' minOccurs='0'
						<xs:element name='productName' type='xs:string' />
						<xs:element name='quantity'>
								<xs:restriction base='xs:positiveInteger'>
									<xs:minInclusive value='1' />
									<xs:maxExclusive value='100' />
						<xs:element name='USPrice' type='xs:decimal' />
						<xs:element ref='tns:comment' />
						<xs:element name='shipDate' type='xs:date'
							minOccurs='0' />
					<xs:attribute name='partNum' type='tns:SKU' />
	<xs:complexType name='PurchaseOrderType'>
			<xs:element name='shipTo' type='tns:USAddress' />
			<xs:element name='billTo' type='tns:USAddress' />
			<xs:element ref='tns:comment' minOccurs='0' />
			<xs:element name='items' type='tns:Items' />
		<xs:attribute name='orderDate' type='xs:date' />
		<xs:attribute name='confirmDate' type='xs:date'
			use='required' />

Generate JAXB Classes

Once you build the project you will find required Java classes have been generated under directory src/generated-sources/jaxb folder as shown in the below image.

generate jaxb classes using gradle

Source Code


Thanks for reading.

Related posts

2 Thoughts to “Generating jaxb classes from xsd using Gradle”

  1. anagha

    facing issue Unrecognized option –add-modules=java.xml.bind.activation

    1. Simply remove this module from the file

Leave a Comment