Search results
Generate JAXB Classes from XSD with JAXB Maven Plugins
pom.xml
</properties>
<!-- Maven Plugins -->
<jaxb-plugins.version>4.0.9</jaxb-plugins.version>
<xjc-lombok-plugin.version>1.0</xjc-lombok-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- JAXB Plugin for generating Java classes from XSD -->
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<version>${jaxb-plugins.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<generatePackage>com.asimiotech.demo.messaging.model</generatePackage>
<generateDirectory>${project.build.directory}/generated-sources/jaxb</generateDirectory>
<removeOldOutput>true</removeOldOutput>
<verbose>true</verbose>
<extension>true</extension>
<args>
<arg>-no-header</arg>
<arg>-Xlombok</arg>
</args>
<specVersion>3.0</specVersion>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-plugins</artifactId>
<version>${jaxb-plugins.version}</version>
</dependency>
<dependency>
<groupId>de.plushnikov.xjc</groupId>
<artifactId>xjc-lombok-plugin</artifactId>
<version>${xjc-lombok-plugin.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- Build Helper Plugin to add the generated sources to the project -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/jaxb</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Usage
mvn clean package
...
[INFO] --- jaxb:4.0.9:generate (generate-sources) @ springboot3-jms-subscriber ---
[INFO] Started execution.
[INFO] JAXB API is loaded from the [jar:file:/Users/ootero/.m2/repository/jakarta/xml/bind/jakarta.xml.bind-api/4.0.2/jakarta.xml.bind-api-4.0.2.jar!].
[INFO] pluginArtifacts:[org.jvnet.jaxb:jaxb-maven-plugin:maven-plugin:4.0.9:, org.jvnet.jaxb:jaxb-plugins:jar:4.0.9:runtime, org.jvnet.jaxb:jaxb-plugins-runtime:jar:4.0.9:runtime, org.jvnet.jaxb:jaxb-plugins-tools:jar:4.0.9:runtime, commons-beanutils:commons-beanutils:jar:1.9.4:runtime, commons-collections:commons-collections:jar:3.2.2:runtime, org.slf4j:jcl-over-slf4j:jar:1.7.36:runtime, com.github.javaparser:javaparser-core:jar:3.25.9:runtime, de.plushnikov.xjc:xjc-lombok-plugin:jar:1.0:runtime, org.jvnet.jaxb:jaxb-maven-plugin-core:jar:4.0.9:compile, org.slf4j:slf4j-api:jar:1.7.36:compile, org.apache.commons:commons-lang3:jar:3.12.0:compile, xml-resolver:xml-resolver:jar:1.2:compile, org.glassfish.jaxb:txw2:jar:4.0.5:compile, org.apache.maven:maven-plugin-api:jar:3.9.6:compile, org.apache.maven:maven-model:jar:3.9.6:compile, org.apache.maven:maven-artifact:jar:3.9.6:compile, org.eclipse.sisu:org.eclipse.sisu.plexus:jar:0.9.0.M2:compile, javax.annotation:javax.annotation-api:jar:1.2:compile, org.codehaus.plexus:plexus-classworlds:jar:2.7.0:compile, org.codehaus.plexus:plexus-build-api:jar:1.2.0:compile, javax.inject:javax.inject:jar:1:compile, org.sonatype.plexus:plexus-build-api:jar:0.0.7:compile, org.apache.maven:maven-core:jar:3.9.6:compile, org.apache.maven:maven-settings:jar:3.9.6:compile, org.apache.maven:maven-settings-builder:jar:3.9.6:compile, org.codehaus.plexus:plexus-sec-dispatcher:jar:2.0:compile, org.codehaus.plexus:plexus-cipher:jar:2.0:compile, org.apache.maven:maven-builder-support:jar:3.9.6:compile, org.apache.maven:maven-repository-metadata:jar:3.9.6:compile, org.apache.maven:maven-model-builder:jar:3.9.6:compile, org.apache.maven:maven-resolver-provider:jar:3.9.6:compile, org.apache.maven.resolver:maven-resolver-impl:jar:1.9.18:compile, org.apache.maven.resolver:maven-resolver-named-locks:jar:1.9.18:compile, org.apache.maven.resolver:maven-resolver-api:jar:1.9.18:compile, org.apache.maven.resolver:maven-resolver-spi:jar:1.9.18:compile, org.apache.maven.resolver:maven-resolver-util:jar:1.9.18:compile, org.apache.maven.shared:maven-shared-utils:jar:3.3.4:compile, org.eclipse.sisu:org.eclipse.sisu.inject:jar:0.9.0.M2:compile, com.google.inject:guice:jar:5.1.0:compile, aopalliance:aopalliance:jar:1.0:compile, com.google.guava:guava:jar:32.0.1-jre:compile, com.google.guava:failureaccess:jar:1.0.1:compile, org.codehaus.plexus:plexus-component-annotations:jar:2.1.0:compile, org.codehaus.plexus:plexus-utils:jar:3.5.1:compile, jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.2:compile, jakarta.activation:jakarta.activation-api:jar:2.1.3:compile, org.glassfish.jaxb:jaxb-runtime:jar:4.0.5:compile, org.glassfish.jaxb:jaxb-core:jar:4.0.5:compile, com.sun.istack:istack-commons-runtime:jar:4.1.2:compile, org.glassfish.jaxb:jaxb-xjc:jar:4.0.5:compile, org.glassfish.jaxb:xsom:jar:4.0.5:compile, com.sun.xml.bind.external:relaxng-datatype:jar:4.0.5:compile, org.glassfish.jaxb:codemodel:jar:4.0.5:compile, com.sun.xml.bind.external:rngom:jar:4.0.5:compile, com.sun.xml.dtd-parser:dtd-parser:jar:1.5.1:compile, com.sun.istack:istack-commons-tools:jar:4.1.2:compile, org.eclipse.angus:angus-activation:jar:2.0.2:compile, org.apache.maven.plugin-tools:maven-plugin-annotations:jar:3.8.2:compile]
...
[INFO] optionsConfiguration:OptionsConfiguration [specVersion=3.0
generateDirectory=/Users/ootero/Projects/bitbucket.org/springboot3-jms-subscriber/target/generated-sources/jaxb
generatePackage=com.asimiotech.demo.messaging.model
schemaLanguage=null
grammars.systemIds=[file:/Users/ootero/Projects/bitbucket.org/springboot3-jms-subscriber/src/main/resources/xsd/CustomersOrders.xsd]
bindFiles.systemIds=[]
plugins=[]
readOnly=false
packageLevelAnnotations=true
noFileHeader=false
enableIntrospection=false
disableXmlSecurity=true
accessExternalSchema=all
accessExternalDTD=all
contentForWildcard=false
extension=true
strict=true
verbose=true
debugMode=false
arguments=[-no-header, -Xlombok, -episode, /Users/ootero/Projects/bitbucket.org/springboot3-jms-subscriber/target/generated-sources/jaxb/META-INF/sun-jaxb.episode]]
...
[INFO] Writing output to [/Users/ootero/Projects/bitbucket.org/springboot3-jms-subscriber/target/generated-sources/jaxb].
[INFO] Cleaning package directories.
[INFO] XJC writing: com/asimiotech/demo/messaging/model/AddressType.java
[INFO] XJC writing: com/asimiotech/demo/messaging/model/CustomerType.java
[INFO] XJC writing: com/asimiotech/demo/messaging/model/ObjectFactory.java
[INFO] XJC writing: com/asimiotech/demo/messaging/model/OrderType.java
[INFO] XJC writing: com/asimiotech/demo/messaging/model/Root.java
[INFO] XJC writing: com/asimiotech/demo/messaging/model/ShipInfoType.java
[INFO] Finished execution.
...
[INFO] --- build-helper:3.6.1:add-source (add-source) @ springboot3-jms-subscriber ---
[INFO] Source directory: /Users/ootero/Projects/bitbucket.org/springboot3-jms-subscriber/target/generated-sources/jaxb added.
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...
ls target/generated-sources/jaxb/com/asimiotech/demo/messaging/model/
AddressType.java CustomerType.java ObjectFactory.java OrderType.java Root.java ShipInfoType.java
Root.java
package com.asimiotech.demo.messaging.model;
import java.util.ArrayList;
import java.util.List;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;
import lombok.EqualsAndHashCode;
import lombok.ToString;
/**
* <p>Java class for anonymous complex type</p>.
*
* <p>The following schema fragment specifies the expected content contained within this class.</p>
*
* <pre>{@code
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Customers">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Customer" type="{}CustomerType" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* <element name="Orders">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Order" type="{}OrderType" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* }</pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"customers",
"orders"
})
@XmlRootElement(name = "Root")
@ToString
@EqualsAndHashCode
public class Root {
@XmlElement(name = "Customers", required = true)
protected Root.Customers customers;
@XmlElement(name = "Orders", required = true)
protected Root.Orders orders;
/**
* Gets the value of the customers property.
*
* @return
* possible object is
* {@link Root.Customers }
*
*/
public Root.Customers getCustomers() {
return customers;
}
/**
* Sets the value of the customers property.
*
* @param value
* allowed object is
* {@link Root.Customers }
*
*/
public void setCustomers(Root.Customers value) {
this.customers = value;
}
/**
* Gets the value of the orders property.
*
* @return
* possible object is
* {@link Root.Orders }
*
*/
public Root.Orders getOrders() {
return orders;
}
/**
* Sets the value of the orders property.
*
* @param value
* allowed object is
* {@link Root.Orders }
*
*/
public void setOrders(Root.Orders value) {
this.orders = value;
}
/**
* <p>Java class for anonymous complex type</p>.
*
* <p>The following schema fragment specifies the expected content contained within this class.</p>
*
* <pre>{@code
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Customer" type="{}CustomerType" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* }</pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"customer"
})
@ToString
@EqualsAndHashCode
public static class Customers {
@XmlElement(name = "Customer")
protected List<CustomerType> customer;
/**
* Gets the value of the customer property.
*
* <p>This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the customer property.</p>
*
* <p>
* For example, to add a new item, do as follows:
* </p>
* <pre>
* getCustomer().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link CustomerType }
* </p>
*
*
* @return
* The value of the customer property.
*/
public List<CustomerType> getCustomer() {
if (customer == null) {
customer = new ArrayList<>();
}
return this.customer;
}
}
/**
* <p>Java class for anonymous complex type</p>.
*
* <p>The following schema fragment specifies the expected content contained within this class.</p>
*
* <pre>{@code
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="Order" type="{}OrderType" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* }</pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"order"
})
@ToString
@EqualsAndHashCode
public static class Orders {
@XmlElement(name = "Order")
protected List<OrderType> order;
/**
* Gets the value of the order property.
*
* <p>This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the order property.</p>
*
* <p>
* For example, to add a new item, do as follows:
* </p>
* <pre>
* getOrder().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link OrderType }
* </p>
*
*
* @return
* The value of the order property.
*/
public List<OrderType> getOrder() {
if (order == null) {
order = new ArrayList<>();
}
return this.order;
}
}
}
No need to copy these classes to the project, build-helper-maven-plugin handles adding target/generated-sources/jaxb/ package(s) to the resulting jar files, and to the IDE classpath.
The JAXB classes include @ToString and @EqualsAndHashCode through the xjc-lombok-plugin inside the jaxb-maven-plugin Maven plugin and <arg>-Xlombok</arg> configuration element.
If you wish to leave the src/main/resources/xsd folder out of the build, add this configuration to the Maven’s build -> resources section in pom.xml:
pom.xml:
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>xsd/**</exclude>
</excludes>
</resource>
</resources>
</build>
