Create a common xsd generated class to be used by other packages
Solution 1
There is a part in the maven-jaxb2-plugin
documentation dedicated specifically to the separate schema compilation. But you can also achieve your goal with usual XJC bindings.
-
Do not use
generatePackage
in the plugin config, usejaxb:package
in bindings, ex.:<jaxb:schemaBindings> <jaxb:package name="generatePackage"/> </jaxb:schemaBindings>
- Use
<jaxb:class ref="com.test.common.CommonType"/>
oncommonType
inxjb/commons.xjb
.
SO Disclaimer: I'm the author of the maven-jaxb2-plugin
.
Solution 2
You can use the -episode
extension in the JAXB XJC to handle this use case:
XJC call for common.xsd
xjc -d out -episode common.episode common.xsd
XJC call for objectA.xsd
The episode file produced from the first step is really a JAXB external bindings file that contains links between schema types and existing classes. This prevents XJC from regenerating these classes.
xjc -d out objectA.xsd -extension -b common.episode
For a Detailed Example
Matt
Besides being a developer i enjoy Music, Guitar, Drawing, Hiking, Movies, and Camping
Updated on June 03, 2022Comments
-
Matt almost 2 years
I am trying to use the same generated class but in separate packages. So the structure should look something like this:
com.test.common -commonType.java com.test.A -objectA.java com.test.B -objectB.java
But i keep getting this:
com.test.common -commonType.java com.test.A -objectA.java -commonType.java com.test.B -objectB.java -commonType.java
My common.xsd looks like this:
<?xml version="1.0"?> <xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="http://test.com/magic/common" xmlns="http://test.com/magic/common" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"> <xs:complexType name="CommonType"> <xs:sequence> <xs:element name="name" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:schema>
the objectA.xsd looks like
<?xml version="1.0"?> <xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="http://test.com/magic/objectA" xmlns:common="http://test.com/magic/common" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"> <xs:complexType name="ObjectA"> <xs:sequence> <xs:element name="size" type="xs:string" /> <xs:element name="commonA" type="common:CommonType" /> </xs:sequence> </xs:complexType> </xs:schema>
And objectB.xsd looks like:
<?xml version="1.0"?> <xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="http://test.com/magic/objectB" xmlns:common="http://test.com/magic/common" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"> <xs:complexType name="ObjectB"> <xs:sequence> <xs:element name="version" type="xs:string" /> <xs:element name="commonB" type="common:CommonType" /> </xs:sequence> </xs:complexType> </xs:schema>
I have a common binding file common.xjb which looks like this:
<jxb:bindings schemaLocation="../xsd/common.xsd" node="/xsd:schema"> <jxb:schemaBindings> <jxb:package name="com.test.common"/> </jxb:schemaBindings> </jxb:bindings>
And finally my maven job looks like this:
<plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <configuration> <args> <arg>-Xequals</arg> </args> <plugins> <plugin> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics</artifactId> <version>0.6.3</version> </plugin> </plugins> <episode>true</episode> <extension>true</extension> <verbose>true</verbose> <generateDirectory>src/main/java</generateDirectory> </configuration> <executions> <execution> <id>common</id> <goals> <goal>generate</goal> </goals> <configuration> <generatePackage>com.test.common</generatePackage> <schemaIncludes> <includeSchema>xsd/common.xsd</includeSchema> </schemaIncludes> </configuration> </execution> <execution> <id>login</id> <goals> <goal>generate</goal> </goals> <configuration> <generatePackage>com.test.A</generatePackage> <bindingIncludes> <includeBinding>xjb/commons.xjb</includeBinding> </bindingIncludes> <schemaIncludes> <includeSchema>xsd/objectA.xsd</includeSchema> </schemaIncludes> </configuration> </execution> <execution> <id>alert</id> <goals> <goal>generate</goal> </goals> <configuration> <generatePackage>com.test.B</generatePackage> <bindingIncludes> <includeBinding>xjb/commons.xjb</includeBinding> </bindingIncludes> <schemaIncludes> <includeSchema>xsd/objectB.xsd</includeSchema> </schemaIncludes> </configuration> </execution> </executions> </plugin> </plugins>
-
Matt over 12 yearsI tried using that but i still result in the CommonType being generated both times
-
bdoughan over 12 yearsI had to add the necessary XML schema imports
<xs:import namespace="http://test.com/magic/common" schemaLocation="common.xsd"/>
but once I did I was able to use the command line XJC as described in my answer to only generate the classes from common.xsd once. -
Matt over 12 yearsthat's correct the command line works as expected but for some reason the maven plugin is not working even when i specify <episodeFile> <extenstion> <bindingInclude> configurations
-
lexicore over 5 years@SarvarNishonboev Done.