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
- Lets create a simple Maven Project: File -> New Maven Project ->
- Select Project type as “war” and below is the sample pom.xml file
- Let create the project structure like below
- 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
- add jaxb-2-maven plugin target in pom.xml under plugins
- 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:
-
<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!!