JAXB Generated Class Prefix

I have a Maven job to generate Java classes from an XSD file using JAXB.

<!-- XML to Java classes --> <plugin> <groupId>com.sun.tools.xjc.maven2</groupId> <artifactId>maven-jaxb-plugin</artifactId> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <generatePackage>nl.compay.service</generatePackage> <schemaDirectory>src/main/webapp/compay</schemaDirectory> </configuration> </plugin> 

For the XSD type User, it generates a class called User (duh). However, I also have a JPA entity class called "User" (albeit in a different package). Can I reconfigure the XML above so that the JAXB prefix the generated classes with something like "XML"?

+12
jaxb xjc
May 21 '09 at 10:40
source share
1 answer

This is a general requirement. You can do this by providing an additional JAXB binding file to customize how JAXB translates schema type names to Java class names.

These files usually end with the extension ".xjb". You need to create it for your circuit, for example:

 <jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jxb:extensionBindingPrefixes="xjc"> <jxb:bindings schemaLocation="nl.company.service.xsd" node="/xs:schema"> <jxb:schemaBindings> <jxb:nameXmlTransform> <jxb:typeName prefix="XML"/> <jxb:anonymousTypeName prefix="XML"/> </jxb:nameXmlTransform> </jxb:schemaBindings> </jxb:bindings> </jxb:bindings> 

After you have done this, release the xjb file somewhere in your build directory and tell Maven to use it during translation:

 <includeBindings> <includeBinding>mybindings.xjb</includeBinding> </includeBindings> 

And here is a hint for the road: if you are on a path that contains spaces (for example, โ€œDocuments and Settings \ user \ projectโ€), then JAXB will crash with strange errors.

+19
May 24 '09 at 12:01
source share



All Articles