Repository navigation
Using JAXB2 Basics Plugins
JAXB RI provides a schema compiler called XJC. One of the most important features of XJC is extensibility. You can implement your own XJC plugins to enhance, modify or customize the generated code. See this blog entry for a short introduction.
In order to use JAXB plugins in your builds, you essentially need to make two things:
- Add the plugin library available into the XJC classpath (including all the required dependencies).
- Turn on the
extensionoption. - Activate (and possibly configure) the plugin by adding corresponding command-line arguments.
Following section describe how this can be done with Ant and Maven. Check Sample Projects for working examples.
In Maven builds, you have to list your JAXB plugin artifacts in configuration/plugins/plugin elements (you don't need to define the dependencies, Maven will take care of dependency resolution). JAXB plugins are activated and configured via the configuration/args/arg elements.
<project ...> ... <build> <plugins> ... <plugin> <groupId>org.jvnet.jaxb</groupId> <artifactId>jaxb-maven-plugin</artifactId> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <extension>true</extension> <args> <arg>-XtoString</arg> <arg>-Xequals</arg> <arg>-XhashCode</arg> <arg>-Xcopyable</arg> </args> <plugins> <plugin> <groupId>org.jvnet.jaxb</groupId> <artifactId>jaxb2-basics</artifactId> <version>[Align with jaxb-maven-plugin version]</version> </plugin> </plugins> </configuration> </plugin> </plugins> </build> ... </project>
Some of the JAXB2 Basics plugins (like hashCode, equals, toString, copyable, mergeable) have a runtime dependency to org.jvnet.jaxb:jaxb2-basics-runtime. This means you will need to include jaxb2-basics-runtime into the dependencies of your project:
<project> ... <dependencies> <dependency> <groupId>org.jvnet.jaxb</groupId> <artifactId>jaxb2-basics-runtime</artifactId> <version>...</version> </dependency> ... </dependencies> ... </project>
Assume your libraries (*.jar files) are placed in the lib directory. Below is the possible code generation target of an Ant build script:
<target name="generate-sources"> <!-- Define the xjc task --> <taskdef name="xjc" classname="org.jvnet.jaxb2_commons.xjc.XJC2Task"> <!-- XJC2 Task classpath --> <classpath> <fileset dir="${basedir}/lib"> <include name="activation-*.jar"/> <include name="jaxb-api-*.jar"/> <include name="jaxb-impl-*.jar"/> <include name="jsr173_api-*.jar"/> <include name="stax-api-*.jar"/> <include name="jaxb-xjc-*.jar"/> <include name="jaxb2-basics-ant-*.jar"/> </fileset> </classpath> </taskdef> <mkdir dir="${basedir}/target/generated-sources/xjc"/> <!-- Generate the code --> <xjc destdir="${basedir}/target/generated-sources/xjc" extension="true"> <arg line=" -Xequals -XhashCode -XtoString -Xcopyable"/> <binding dir="${basedir}/src/main/resources"> <include name="**/*.xjb"/> </binding> <schema dir="${basedir}/src/main/resources"> <include name="**/*.xsd"/> </schema> <!-- Plugins --> <classpath> <fileset dir="${basedir}/lib"> <!-- JAXB2 Basics library --> <include name="jaxb2-basics-*.jar"/> <!-- JAXB2 Basics library dependencies --> <include name="jaxb2-basics-runtime-*.jar"/> <include name="jaxb2-basics-tools-*.jar"/> <include name="commons-beanutils-*.jar"/> <include name="commons-lang-*.jar"/> <include name="commons-logging-*.jar"/> </fileset> </classpath> </xjc> </target>
In Ant builds, plugin libraries are provided to XJC via the xjc/classpath element. Note that I had to list the plugin library (jaxb2-basics-*.jar) as well as all of its dependencies (jaxb2-basics-runtime-*.jar and so on).
Plugins are activated using the line attribute of the xjc/arg element:
<xjc destdir="${basedir}/target/generated-sources/xjc" extension="true"> <arg line=" -Xequals -XhashCode -XtoString -Xcopyable"/> <!-- ... --> </xjc>
You can also use this element to provide configuration to the plugins that you use:
<arg line=" -XtoString -XtoString-toStringBuilder=com.acme.foo.MyStringBuilder"/>
- Home
- Migration guide
-
JAXB Maven Plugin
- Quick Start
-
User Guide
- Basic Usage
- Specifying What To Compile
- Referencing Resources in Maven Artifacts
- Reproducible Builds using jaxb-maven-plugin
- Using Catalogs
- Using Episodes
- Modular Schema Compilation
- Controlling the Output
- Using JAXB Plugins
- Using a Specific JAXB Version
- Configuring Extension, Validation and XML Security
- IDE Integration
- Miscellaneous
- Configuring Proxies
- Maven Documentation
- Configuration Cheat Sheet
- Common Pitfalls and Problems
-
JAXB2 Basics Plugins
- Using JAXB2 Basics Plugins
- JSR-305 Support
-
JAXB2 Basics Plugins List
- SimpleEquals Plugin
- SimpleHashCode Plugin
- Equals Plugin
- HashCode Plugin
- ToString Plugin
- Copyable Plugin
- Mergeable Plugin
- Inheritance Plugin
- AutoInheritance Plugin
- Wildcard Plugin
- Setters Plugin
- Simplify Plugin
- EnumValue Plugin
- JAXBIndex Plugin
- FixJAXB1058 Plugin
- Commons Lang Plugin
- Default Value Plugin
- Fluent API Plugin
- Namespace Prefix Plugin
- Value Constructor Plugin
- Boolean Getter Plugin
- CamelCase Plugin
- XML ElementWrapper Plugin
- Parent Pointer Plugin
- Property Listener Injector Plugin
- Annox
- JAXB Annotate Plugin
-
HyperJAXB3
- Build System Support
- Customization Guide
- Databases
- Development guide
- Extension guide
- FAQ
- IDE Support
- Java Persistence
- JAXB
- JDK Support
- Project Templates
-
Reference
- Adding vendor-specific annotations
- Features
- Integrating Hyperjaxb3 in builds
- Introduction
- Making schema-derived classes ready for JPA
- Adding required properties
- Applying workarounds for JAXB vs. JPA conflicts
- Enforcing top-level classes
- Generating equals and hashCode methods
- Generating ORM metadata
- Generating persistence unit descriptor
- JPA 2 Support
- Making classes serializable
- Testing generated mappings
- Reference - single page
- Related Projects
- Sample projects
- Solutions
- Target Scenarios
- Test Projects
- Tutorials
- Best Practices
- FAQ
- Sample Projects
- Support
- License
- Distribution