jaxb2-maven-plugin XJC example to Generate Java Classes from XSD

Filed Under: Maven

Today we will look into jaxb2-maven-plugin XJC example to generate java classes from XSD. JAXB is the middleware technology between Java Classes and XML. We can use JAXB to generate XML from Java Object and vice versa.


We use XSD to define the contract data structure, so it’s not common to generate java classes that represent the XML schema.

jaxb2-maven-plugin XJC is JAXB Binding compiler tool that can be used to generate Java Classes from XSD files.

Here we will learn how to use jaxb2-maven-plugin XJC in a maven project to generate java classes from XSD.

First of all we have to create a maven project, then we need to use jaxb2-maven-plugin plugin to generate java classes from XSD.


<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <name>JD Example XSD to Java</name>

			<!-- Plugin required to build java classes from XSD using XJC -->
                   <!-- The name of your generated source package -->
                	<arguments>-extension -npa -b ${project.basedir}/src/main/xsd/global.xjb</arguments> 

Notice the global.xjb file in arguments, this is where we specify java binding rules.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0"
    <xjc:simple />
    <xjc:serializable uid="-1" />
    <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
      printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />

Here is the XSD that will be used to generate java classes.


<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="https://www.w3.org/2001/XMLSchema"

	<element name="empRequest" type="empns:EmpRequest"></element>
	<element name="empResponse" type="empns:EmpResponse"></element>

	<complexType name="EmpRequest">
			<element name="id" type="int" minOccurs="0" maxOccurs="1" />
			<element name="name" type="string" minOccurs="0" maxOccurs="1" />
	<complexType name="EmpResponse">
			<element name="id" type="int" minOccurs="1" maxOccurs="1" />
			<element name="name" type="string" minOccurs="1" maxOccurs="1" />
			<element name="role" type="string" minOccurs="1" maxOccurs="unbounded" />
			<element name="gender" type="string" minOccurs="1" maxOccurs="1" />
			<element name="salary" type="string" minOccurs="1" maxOccurs="1" />


Just build the maven project using mvn clean install and you will see java classes generated in target/generated-sources/jaxb directory.

Finally the project would look something like below image.

jaxb2-maven-plugin XJC Example

Further Read: JAXB Tutorial

Reference: jaxb2 maven plugin official page, java xjc


  1. RM says:

    Just FYI the schema namespaces are NOT https:// … etc, they must be http://

  2. Azizasm says:

    Proper config for JAXB2 for version below 2.0 is :


  3. Mageswran says:

    How to add rootelement into java class using XJB

  4. Mageswran says:

    How to add Root element into Java class using XJB using Maven

  5. Jalaram says:

    Hi Pankaj,

    I have one question, what is purpose of -extention and -npa? where you have use this?

    -extension -npa -b ${project.basedir}/src/main/xsd/global.xjb


  6. DeanS says:

    You forgot to tell mvn where Employee.xsd is. You need a in your element.

  7. A S says:

    change the goal from xjc to generate if you getting Errors when trying to install with maven.

  8. Bhawani Singh Shekhawat says:

    Thanks Pankaj ! 馃檪

  9. Ricardo says:

    Thanks Pankaj. I was looking for an example of the xjc maven plugin usage and yours was exactly what I needed.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors