/jaxb-tools

The most advanced JAXB2 Maven Plugin for XML Schema compilation.

Primary LanguageJavaOtherNOASSERTION

JAXB Tools

Maven Central GitHub Actions Status CodeQL

Welcome to the most advanced and feature-full sets of JAXB-related tools.

The project is currently containing the following tools :

Java versions

This project supports Java 8 and higher.

The build is tested against JDK8, JDK11 and JDK17.

Please refer to the wiki for the full documentation.

Disclaimer

This project is not developed, supported or in any other way affiliated with Apache, Eclipse or Oracle.

It was a completely independent development by its creator, Alexey Valikov.

It is now maintained by a group of people who are interested in keeping jaxb-tools working with future versions of Java and Jakarta.

JAXB Maven Plugin

This Maven plugin generates Java classes during Maven builds from XML Schemas (as well as WSDL, DTDs, RELAX NG formats).

It wraps and enhances the JAXB Schema Compiler (XJC) with its own set of plugins and customization points.

Quick start

  • Put your schemas (*.xsd) and bindings (*.xjb) into the src/main/resources folder.
  • Add the plugin to your pom.xml:
<project ...>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.jvnet.jaxb</groupId>
        <artifactId>jaxb-maven-plugin</artifactId>
        <version>2.0.5</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

JAXB Versions

The current version 2.X of this plugin supports only JAXB 2.3.

A version of this plugin that supports JAXB >= 3.x is currently being worked on.

If you need an older JAXB version, you can use one of the following variants, which are no longer supported:

  • org.jvnet.jaxb2.maven2:maven-jaxb22-plugin:0.15.1 - JAXB 2.2.
  • org.jvnet.jaxb2.maven2:maven-jaxb21-plugin:0.15.2 - JAXB 2.1.
  • org.jvnet.jaxb2.maven2:maven-jaxb20-plugin:0.15.2 - JAXB 2.0.

Similar Projects

If you experience issues with the Mojohaus JAXB2 Maven Plugin (org.codehaus.mojo:jaxb2-maven-plugin), please file it on their project page.

JAXB Basics

JAXB Basics is an open source project which provides useful plugins and tools for JAXB 3.x reference implementation.

Documentation

Please refer to the wiki for documentation.

JAXB Basics can only be used with JAXB/XJC 3.x.

Using JAXB Basics

JAXB Basics Plugins

  • SimpleEquals Plugin - generates runtime-free reflection-free equals(...) methods.
  • SimpleHashCode Plugin - generates runtime-free reflection-free hashCode() methods.
  • Equals Plugin - generates reflection-free strategic equals(...) method.
  • HashCode Plugin - generates reflection-free strategic hashCode() method.
  • ToString Plugin - generates reflection-free strategic toString() methods.
  • Copyable Plugin - generates reflection-free strategic copy(...) deep copying.
  • Mergeable Plugin - generates reflection-free strategic merge(...) methods to merge data from two source objects into the given object.
  • Inheritance Plugin - makes schema-derived classes extend certain class or implement certain interfaces.
  • Wildcard Plugin - allows you to specify the wildcard mode for the wildcard properties.
  • AutoInheritance Plugin - makes classes derived from global elements or complex types extend or implement certain classes or interfaces automatically.
  • Setters Plugin - generates setters for collections.
  • Simplify Plugin - simplifies weird properties like aOrBOrC.
  • EnumValue Plugin - makes all the generated enums implement the EnumValue<T> interface.
  • JAXBIndex Plugin - generated jaxb.index files listing schema-derived classes.
  • FixJAXB1058 Plugin - fixes JAXB-1058.
  • Commons Lang Plugin - generates the toString(), hashCode() and equals() methods using Apache commons-lang3.
  • Default Value Plugin - modifies the JAXB code model to set default values to the schema default attribute.
  • Fluent API Plugin - support a fluent api in addition to the default (JavaBean) setter methods.
  • Namespace Prefix Plugin - adds javax.xml.bind.annotation.XmlNs annotations to package-info.java files
  • Value Constructor Plugin - generates another constructor, taking an argument for each field in the class and initialises the field with the argument value.

Credits

Annox

Parse Java annotations from text or XML resources.

Please refer to the wiki for documentation.

// Parse annotation from the string
XAnnotation<XmlRootElement> xannotation =
	(XAnnotation<XmlRootElement>) XAnnotationParser.INSTANCE.parse
		("@jakarta.xml.bind.annotation.XmlRootElement(name=\"foo\")");

// Create an instance of the annotation
XmlRootElement xmlRootElement = xannotation.getResult();
assertEquals("foo", xmlRootElement.name());
assertEquals("##default", xmlRootElement.namespace());

// Analyze the structure of the annotation
assertEquals(String.class, xannotation.getFieldsMap().get("name").getType());
assertEquals("##default", xannotation.getFieldsMap().get("namespace").getResult());

JAXB Annotate Plugin

JAXB Annotate Plugin is capable of adding or removing arbitrary annotations to/from the generated sources.

Please refer to the wiki for documentation.

Usage overview

  • Annotate your schema using binding files or directly in schema
  • Add the plugin to the XJC classpath.
  • Add your annotation classes to the XJC classpath.
  • Activate the plugin using -Xannotate-switch.

Providing annotations

You can annotate your schema-derived elements using normal Java annotation syntax. (Old XML syntax is still supported but no longer recommended.)

Current limitations:

  • Annotation classes must be known in compile time. I.e. annotation classes must be made available in the XJC classpath. If you want to use your own annotations, you have to pre-compile them and add your annotation classes to the XJC classpath.
  • As a consequence, currently you can't use schema-derived enums in your annotations. Since you have to compile annotations before compiling the schema - and as your enums are first generated from the schema, this is a chicken-and-egg-problem.
  • All class names (classes of annotations or classes you use as values in annotations) must be fully qualified. Inner classes should use the dot (.) as delimiter (not $).

You can put your annotations directly in schema:

<xsd:schema
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
        jaxb:version="3.0"
        xmlns:annox="http://annox.dev.java.net"
        jaxb:extensionBindingPrefixes="annox">


    <xsd:complexType name="FooType">
        <xsd:annotation>
            <xsd:appinfo>
                <annox:annotate>@java.lang.SuppressWarnings({"unchecked","rawtypes"})</annox:annotate>
                <annox:annotate target="package">@javax.annotation.Generated({"XJC","JAXB Annotate Plugin"})</annox:annotate>
            </xsd:appinfo>
        </xsd:annotation>
        <xsd:sequence>
            <xsd:element name="bar" type="xsd:string"/>
            <xsd:element name="foobar" type="xsd:string">
                <xsd:annotation>
                    <xsd:appinfo>
                        <annox:annotate>@java.lang.SuppressWarnings({"unchecked","rawtypes"})</annox:annotate>
                        <annox:annotate target="setter">@java.lang.Deprecated</annox:annotate>
                        <annox:annotate target="setter-parameter">@java.lang.Deprecated</annox:annotate>
                        <annox:annotate target="getter">@java.lang.Deprecated</annox:annotate>
                        <annox:annotate target="field">@java.lang.Deprecated</annox:annotate>
                        <annox:annotate target="class">@java.lang.Deprecated</annox:annotate>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>

    <xs:simpleType name="FooEnum">
        <xs:annotation>
            <xs:appinfo>
                <annox:annotateEnumValueMethod>@java.lang.Deprecated</annox:annotateEnumValueMethod>
            </xs:appinfo>
        </xs:annotation>

        <xs:restriction base="xs:string">
            <xs:enumeration value="BAR"/>
            <xs:enumeration value="BAZ"/>
        </xs:restriction>
    </xs:simpleType>

</xsd:schema>

Or in binding files:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
        xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:annox="http://annox.dev.java.net"
        xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
        jaxb:extensionBindingPrefixes="xjc annox"
        version="2.1">

    <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <jaxb:bindings node="xs:complexType[@name='issueJIIB39CType']">
            <annox:annotateClass>@jakarta.xml.bind.annotation.XmlRootElement(name="IssueJIIB39CType")</annox:annotateClass>
        </jaxb:bindings>
        <jaxb:bindings node="xs:complexType[@name='issueJIIB39CType']/xs:attribute[@name='test']">
            <annox:annotate target="field">@javax.xml.bind.annotation.XmlAttribute(required=false, name="test")</annox:annotate>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

You can use the following customization elements in the http://annox.dev.java.net namespace:

  • annotate with the optional target attribute
  • package
  • class
  • getter
  • setter
  • setter-parameter
  • field
  • enum-value-method
  • enum-fromValue-method
  • annotateProperty
  • annotatePackage
  • annotateClass
  • annotateElement
  • annotateEnum
  • annotateEnumConstant
  • annotateEnumValueMethod - annotate the value() method of the enum
  • annotateEnumFromValueMethod - annotate the fromValue(String) method of the enum

The http://annox.dev.java.net namespace must be declared in the jaxb:extensionBindingPrefixes attribute via prefix, ex.:

xmlns:annox="http://annox.dev.java.net"
jaxb:extensionBindingPrefixes="xjc annox"

Note: yes, I know that http://annox.dev.java.net no longer exists. Changing this namespace would break old builds. This is just a namespace, there must not necessarily be content there. Treat it as a logical identifier, nothing else.

Removing annotations

  • Customize your schema using binding files or directly in schema
  • Add the plugin to the XJC classpath.
  • Activate the plugin using -XremoveAnnotation-switch.

You can remove annotations using customizations directly in schema:

<xsd:schema
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
        jaxb:version="3.0"
        xmlns:annox="http://annox.dev.java.net"
        jaxb:extensionBindingPrefixes="annox">

    <xsd:complexType name="FooType">
        <xsd:annotation>
            <xsd:appinfo>
                <annox:removeAnnotation class="jakarta.xml.bind.annotation.XmlType" />
            </xsd:appinfo>
        </xsd:annotation>
        <xsd:sequence>
            <xsd:element name="bar" type="xsd:string"/>
            <xsd:element name="foobar" type="xsd:string">
                <xsd:annotation>
                    <xsd:appinfo>
                        <annox:removeAnnotation class="jakarta.xml.bind.annotation.XmlElement" target="field" />
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>

Or in binding files:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
        xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:annox="http://annox.dev.java.net"
        xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
        jaxb:extensionBindingPrefixes="xjc annox"
        version="2.1">

    <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
        <jaxb:bindings node="xs:complexType[@name='FooType']">
            <annox:removeAnnotation class="jakarta.xml.bind.annotation.XmlType" />
        </jaxb:bindings>
        <jaxb:bindings node="xs:complexType[@name='FooType']//xs:element[@name='foobar']">
            <annox:removeAnnotation class="jakarta.xml.bind.annotation.XmlElement" target="field" />
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

You can use the following customization elements in the http://annox.dev.java.net namespace:

  • removeAnnotation with the optional target attribute:
  • package
  • class
  • getter
  • setter
  • setter-parameter
  • field
  • enum-value-method
  • enum-fromValue-method
  • removeAnnotationFromProperty
  • removeAnnotationFromPackage
  • removeAnnotationFromClass
  • removeAnnotationFromElement
  • removeAnnotationFromeEnum
  • removeAnnotationFromEnumConstant
  • removeAnnotationFromEnumValueMethod - removes annotation from the value() method of the enum
  • removeAnnotationFromEnumFromValueMethod - removes annotation from the fromValue(String) method of the enum

The http://annox.dev.java.net namespace must be declared in the jaxb:extensionBindingPrefixes attribute via prefix, ex.:

xmlns:annox="http://annox.dev.java.net"
jaxb:extensionBindingPrefixes="xjc annox"

Note: yes, I know that http://annox.dev.java.net no longer exists. Changing this namespace would break old builds. This is just a namespace, there must not necessarily be content there. Treat it as a logical identifier, nothing else.

Using JAXB Annotate Plugin with Maven

  • Add org.jvnet.jaxb:jaxb-basics-annotate as XJC plugin
  • Turn on the plugin using -Xannotate or -XremoveAnnotationswitch
  • Add artifact with your annotations as another XJC plugin

Example:

<plugin>
    <groupId>org.jvnet.jaxb</groupId>
    <artifactId>jaxb-maven-plugin</artifactId>
    <configuration>
        <extension>true</extension>
        <args>
            <arg>-Xannotate</arg>
            <arg>-XremoveAnnotation</arg>
        </args>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb</groupId>
                <artifactId>jaxb-basics-annotate</artifactId>
            </plugin>
            <!-- Artifact with custom annotations -->
            <plugin>
                <groupId>com.acme.foo</groupId>
                <artifactId>my-custom-annotations</artifactId>
            </plugin>
        </plugins>
    </configuration>
</plugin>

See this example.

Note that annotations are first compiled in the annotations module and the added to the classpath of the jaxb-maven-plugin in the schema module:

Using JAXB Annotate Plugin with Ant

See this example.

JAXB Hyperjaxb

Hyperjaxb3 provides relational persistence for JAXB objects. This is preview feature (please give feedback if any problems).