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:
Prerequisites
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 {
mavenCentral()
}
//sourceSets.main.java.srcDirs "src/generated-sources/jaxb"
configurations {
jaxb
}
dependencies {
jaxb (
'com.sun.xml.bind:jaxb-xjc:2.3.1',
'com.sun.xml.bind:jaxb-impl:2.3.1',
'org.glassfish.jaxb:jaxb-runtime:2.3.3'
)
}
task jaxb {
System.setProperty('javax.xml.accessExternalSchema', 'all')
def jaxbTargetDir = file("src/generated-sources/jaxb")
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: '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 (https://msdn.microsoft.com/en-us/library/dd489271.aspx), so put the below shiporder.xsd file under src/main/resources/xsd folder.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://localhost/hello/hello-service"
targetNamespace="http://localhost/hello/hello-service"
elementFormDefault="qualified">
<xs:element name='comment' type='xs:string' />
<xs:element name='purchaseOrder'
type='tns:PurchaseOrderType' />
<xs:complexType name='USAddress'>
<xs:annotation>
<xs:documentation>
Purchase order schema
</xs:documentation>
</xs:annotation>
<xs:sequence>
<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:sequence>
<xs:attribute name='country' type='xs:NMTOKEN' fixed='US' />
</xs:complexType>
<xs:simpleType name='SKU'>
<xs:restriction base='xs:string'>
<xs:pattern value='\d{3}\w{3}' />
</xs:restriction>
</xs:simpleType>
<xs:complexType name='Items'>
<xs:sequence>
<xs:element name='item' minOccurs='0'
maxOccurs='unbounded'>
<xs:complexType>
<xs:sequence>
<xs:element name='productName' type='xs:string' />
<xs:element name='quantity'>
<xs:simpleType>
<xs:restriction base='xs:positiveInteger'>
<xs:minInclusive value='1' />
<xs:maxExclusive value='100' />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name='USPrice' type='xs:decimal' />
<xs:element ref='tns:comment' />
<xs:element name='shipDate' type='xs:date'
minOccurs='0' />
</xs:sequence>
<xs:attribute name='partNum' type='tns:SKU' />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name='PurchaseOrderType'>
<xs:sequence>
<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:sequence>
<xs:attribute name='orderDate' type='xs:date' />
<xs:attribute name='confirmDate' type='xs:date'
use='required' />
</xs:complexType>
</xs:schema>
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.

Source Code
Thanks for reading.
Thanks! It simply worked :)
facing issue Unrecognized option –add-modules=java.xml.bind.activation