JAXB2 Maven Plugin- XSD to Java Code generation

In this tutorial, We will see “How to generate JAXB Pojo’s from XSD in maven pom.xml file.

What is XSD? 

XSD defines structure of an XML and XSD’s are used in REST/SOAP webservices to define meta data of request and response. In webservices, data exchange will be through either JSON or XML. In this example, we will see auto create Java POJO classes for Organization XMl

  1. Lets create a simple Maven Project: File -> New Maven Project ->
  2. Select Project type as “war” and below is the sample pom.xml file
  3. Let create the project structure like below jaxb2-maven-plugin
  4. Create xsd directory in src/main/resources and copy yours XSD’s into this folder. In this case, we use  org.xsd to create java objects
  5. add jaxb-2-maven plugin target in pom.xml under pluginsjaxb2-maven-plugin

 

  • Configuration :
    • By default, jaxb2-maven-plugin scans XSD schema directory src/main/xsd for XML schema files to  create Java source code
    • We can given explicit  scheme directory with “schemaDirectory” xml element:
      • <configuration>
             <schemaDirectory>
                src/main/resources/xsd
               </schemaDirectory>
         </configuration>
    • multiple schema XSD fiiles :  What if you have multiple directories and files like org.xsd, customer.xsd, student.xs. We can configure them under sources xml element like below:
      1. <configuration>
         <sources>
             <source>src/main/resources/dir1</source>
             <source>src/main/resources/dir2</source>
            <source>src/main/resource/org/org.xsd</source>
            <source>src/main/resource/customer/customer.xsd</source>
         </sources>
         </configuration>
  • When you run “maven install” then it will execute XJC goal and creates java POJO’s under target folder. you can modify this under “outputDirectory” xml element.
  • Copy the java classes to src directory and start working on it.

Hope this help!!

Download XSD to Java Source code from here

Leave a Comment

Your email address will not be published. Required fields are marked *