So first you'll need the plugin. Unfortunately as of now its not in a maven repo (which I think is mostly due to the minimal need for maven automation), but you can download the plugin here and add it to your local repo or local/companies repository.
Now you need some stchuff in your pom.
<plugin> <groupId>com.sun.tools.xjc.maven2</groupId> <artifactId>maven-jaxb-plugin</artifactId> <version>1.1</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <generatePackage>com.noviidesign.xjcexample</generatePackage> <includeSchemas> <includeSchema>**/*.xsd</includeSchema> </includeSchemas> <includeBindings> <includeBinding>*.xjb</includeBinding> </includeBindings> <strict>true</strict> <verbose>true</verbose> <removeOldOutput>true</removeOldOutput> </configuration> </plugin>This will remove your old classes and creating code whenever you run a maven goal.
Now when you run this you should have your *.xsd schema in your /resources folder (for a webapp). I also have an *.xjb file in the plugin configuration above. First, lets look at what an xsd schema would look like that'd need the xjb file.
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.noviidesign.com/xjcexample/train" xmlns:tns="http://www.noviidesign.com/xjcexample/train" elementFormDefault="qualified"> <element name="train" type="tns:train" /> <complexType name="train"> <sequence> <element name="car" type="tns:car" minOccurs="1" maxOccurs="unbounded" /> </sequence> <attribute type="string" name="name" use="required" /> <attribute type="string" name="id" use="required" /> <attribute type="string" name="color" use="required" /> </complexType> <complexType name="car"> <attribute type="string" name="name" use="required" /> <attribute type="string" name="id" use="required" /> <attribute type="string" name="maxPassengers" use="required" /> </complexType> </schema>Now this schema is just for example purposes but should get the point across. If needed you can find more information than you could care about on xml schemas on le internet. So, now when the classes are created you'll have the following
package com.noviidesign.xjcexample; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "train", propOrder = {"car"} ) public class Train { @XmlElement(required = true) protected List car; @XmlAttribute(required = true) protected String color; @XmlAttribute(required = true) protected String id; @XmlAttribute(required = true) protected String name; public List getCar() { if (car == null) { car = new ArrayList(); } return this.car; } public String getColor() { return color; } public void setColor(String value) { this.color = value; } public String getId() { return id; } public void setId(String value) { this.id = value; } public String getName() { return name; } public void setName(String value) { this.name = value; } } package com.noviidesign.xjcexample; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "car") public class Car { @XmlAttribute(required = true) protected String id; @XmlAttribute(required = true) protected String maxPassengers; @XmlAttribute(required = true) protected String name; public String getId() { return id; } public void setId(String value) { this.id = value; } public String getMaxPassengers() { return maxPassengers; } public void setMaxPassengers(String value) { this.maxPassengers = value; } public String getName() { return name; } public void setName(String value) { this.name = value; } }So nothing mind blowing, just some regular ol' classes w/some xml annotation. But what I don't like is in Train.java we have protected List
<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="1.0"> <jaxb:bindings schemaLocation="train.xsd" node="//xsd:element[@name='car']"> <jaxb:property name="cars" /> </jaxb:bindings> </jaxb:bindings>And thats really it. Now again you'd usually do this with an ant task, but if you have a need for maven hopefully this will help get you going in the right direction.
In a bit I'll cover how to read in xml into your classes you created.