User Guide

About the plugin

JAXB2 Maven2 plugin which allows you to generate code with JAXB RI in your Maven builds. The plugin participates in the generate-code phase and produces code of the schema-derived classes (plus maybe some other resources) out of the XML Schemas, DTDs and so on.

Basic usage

Since version 0.7.4 maven-jaxb2-plugin is distributed via the central Maven repository.

Simply add the generate execution goal to the build:

<build>
	<plugins>
		<plugin>
			<groupId>org.jvnet.jaxb2.maven2</groupId>
			<artifactId>maven-jaxb2-plugin</artifactId>
			<executions>
				<execution>
					<goals>
						<goal>generate</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

To compile the generated sources, set at 1.5 (or higher) version in source and target of the maven-compiler-plugin:

<build>
	<plugins>
		...
		<plugin>
			<inherited>true</inherited>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.5</source>
				<target>1.5</target>
			</configuration>
		</plugin>
	</plugins>
</build>

See the [sample purchase order project] for example.

The plugin has a large number of configuration options. Please see the plugin documentation for a complete reference.

Specifying schemas and bindings to compile

Below is an example of a non-standard directory layout configuration:

<configuration>
	<!-- Changes the default schema directory -->
	<schemaDirectory>src/main/schema</schemaDirectory>
	<schemaIncludes>
		<include>one/*/*.xsd</include>
	</schemaIncludes>			
	<schemaExcludes>
		<exclude>one/two/*.xsd</exclude>
	</schemaExcludes>			
	<bindingDirectory>src/main/binding</bindingDirectory>
	<bindingIncludes>
		<include>one/*/*.xjb</include>
	</bindingIncludes>			
	<bindingExcludes>
		<exclude>one/two/*.xjb</exclude>
	</bindingExcludes>			
</configuration>

Resource entries

Since version 0.8.0 you can specify schema and binding resources via resource entries in configuration elements schemas and bindings:

<configuration>                                                                                         
	<forceRegenerate>true</forceRegenerate>                                                         
	<schemas>                                                                                       
		<schema>                                                                                
			<fileset>                                                                       
				<!-- Defaults to schemaDirectory -->                                    
				<directory>${basedir}/src/main/schemas</directory>                                 
				<!-- Defaults to schemaIncludes -->                                     
				<includes>                                                              
					<include>*.*</include>                                          
				</includes>                                                             
				<!-- Defaults to schemaExcludes -->                                     
				<excludes>                                                              
					<exclude>*.xs</exclude>                                         
				</excludes>                                                             
			</fileset>                                                                      
			<url>http://schemas.opengis.net/wms/1.3.0/capabilities_1_3_0.xsd</url>          
			<dependencyResource>                                                            
				<groupId>org.jvnet.jaxb2.maven2</groupId>                               
				<artifactId>maven-jaxb2-plugin-tests-po</artifactId>                    
				<!-- Can be defined in project dependencies or dependency management -->
				<version>${project.version}</version>                                   
				<resource>purchaseorder.xsd</resource>                                  
			</dependencyResource>                                                           
		</schema>                                                                               
	</schemas>                                                                                      
</configuration>                                                                                        

The same works for bindings with fileset elements defaulting to bindingDirectory, bindingIncludes and bindingExcludes.

Compiling a schema from an URL

<configuration>                                                                                         
	<forceRegenerate>true</forceRegenerate>                                                         
	<schemas>                                                                                       
		<schema>
			<url>http://schemas.opengis.net/wms/1.3.0/capabilities_1_3_0.xsd</url>
		</schema>                                                                               
	</schemas>                                                                                      
</configuration>                                                                                        

Compiling a schema from a Maven artifact

<configuration>                                                                                         
	<forceRegenerate>true</forceRegenerate>                                                         
	<schemas>                                                                                       
		<schema>
			<dependencyResource>                                                            
				<groupId>org.jvnet.jaxb2.maven2</groupId>                               
				<artifactId>maven-jaxb2-plugin-tests-po</artifactId>                    
				<!-- Can be defined in project dependencies or dependency management -->
				<version>${project.version}</version>                                   
				<resource>purchaseorder.xsd</resource>                                  
			</dependencyResource>                                                           
		</schema>                                                                               
	</schemas>                                                                                      
</configuration>                                                                                        

Compiling DTDs

If you want to compile DTD, change the schemaLanguage to DTD and set the appropriate schemaIncludes:

<configuration>
  <schemaLanguage>DTD</schemaLanguage>
  <schemaIncludes>
    <include>*.dtd</include>
  </schemaIncludes>
</configuration>

See the [sample DTD project] for example.

Controlling the output

Adding target directory as Maven source directory

By default, target directory (target/generated-sources/xjc) will be added as a compile source root. If your project also generates the episode file, it will be added as a project resource.

In some cases you may need to generate test source instead of main sources - so you'll need to add target directory as a test compile source root. Or you may need to override or avoid automatic source root addition for some other reason. You can accomplish this using the following configuration options:

Controlling the extension and validation modes

Setting the debug options

Using a specific version of JAXB 2.x specification

There are differences between JAXB 2.0, 2.1 and 2.2. By default, maven-jaxb2-plugin uses the latest available version of the latest available specification.

Do not forget to use the appropriate version of jaxb-impl.

If you need to stick to a specific version of the JAXB specification, there are two ways to accomplish this.

Configuring the JAXB specification version

You can specify the version of the JAXB specification using the following configuration option:

Using the version-specific plugin

Alternatively you may also use maven-jaxb20-plugin, maven-jaxb21-plugin or maven-jaxb22-plugin instead of maven-jaxb2-plugin to make your dependency on JAXB specification version more explicit:

			<plugin>
				<groupId>org.jvnet.jaxb2.maven2</groupId>
				<artifactId>maven-jaxb20-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

All these plugins have identical configuration options (except that specVersion is ignored in maven-jaxb2x-plugin}}s, it only makes sense in {{maven-jaxb2-plugin).

Using custom JAXB2 plugins

Example:

<configuration>
	<extension>true</extension>
	<args>
		<arg>-XtoString</arg>
		<arg>-Xequals</arg>
		<arg>-XhashCode</arg>
		<arg>-Xcopyable</arg>
	</args>
	<plugins>
		<plugin>
			<groupId>org.jvnet.jaxb2_commons</groupId>
			<artifactId>jaxb2-basics</artifactId>
			<version><!-- version --></version>
		</plugin>
	</plugins>
</configuration>

See the [sample JAXB2 plugins project] for example.

Using catalogs

Sometimes a schema may refer to another schema document without indicating where the schema file can be found:

<xs:import namespace="http://www.w3.org/1999/xlink"/>

Another case is when the provided schema location is somewhere in the internet but you have you local copy of that schema which you'd like to use for compilation.

Both cases are resolved using the catalog mechanism. See this section in the JAXB guide.

Maven2 JAXB plugin allows you to provide a catalog file using the catalog configuration parameter.

Examples:

<configuration>
  <catalog>src/main/resources/catalog.cat</catalog>
</configuration>

Loading catalog from a Maven artifact resource

<configuration>
	<catalogs>
		<catalog>
			<dependencyResource>                                                            
				<groupId>com.acme.foo</groupId>
				<artifactId>bar</artifactId>
				<!-- Version can be omitted if artifact is already
				configured in dependencies or dependency management. -->
				<version>${project.version}</version>
				<resource>catalog.cat</resource>
			</dependencyResource>
		</catalog>
	</catalogs>
</configuration>

The catalog file looks as follows:

PUBLIC "http://example.org/A" "others/schema_a.xsd"

The URI http://example.org/A is the namespace of the schema to be resolved, others/schema_a.xsd is the local schema
location (relative to the catalog file).

XML catalogs also work:

<!DOCTYPE catalog
	PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"
         prefer="public">

	<rewriteSystem systemIdStartString="http://example.org/schemas" rewritePrefix="."/>
</catalog>

You can also reference resources inside Maven artifacts:

REWRITE_SYSTEM "https://maven-jaxb2-plugin.dev.java.net/svn/maven-jaxb2-plugin/trunk/tests/episodes/a/src/main/resources/a.xsd" "maven:org.jvnet.jaxb2.maven2:maven-jaxb2-plugin-tests-episodes-a!/a.xsd"

The URI syntax for Maven artifact resources is as follows:

maven:groupId:artifactId:type:classifier:version!/resource/path/in/jar/schema.xsd

A few examples:

See the [sample catalog project] for example.

Separate schema compilation

If you're compiling large sets of schemas (like the OGC Schemas) you may probably want to compile the
schemas separately. For instance, if you have two schemas A and B (where B imports A), you may
want to compile them into two artifacts A.jar and B.jar such that:

This task is called the separate or episodic compilation. Kohsuke described it in his blog.

Maven2 JAXB2 plugin supports episodic compilation via the following configuration parameters:

For example, consider that we've built the A schema as com.acme.foo:a-schema:jar:1.0 artifact and want to use it as an episode when we compile the B schema. Here's how we configure it:

<project ...>
	...
	<dependencies>
		...
		<dependency>
			<groupId>com.acme.foo</groupId>
			<artifactId>a-schema</artifactId>
			<version>1.0</version>
		</dependency>
		...
	</dependencies>
	<build>
		<defaultGoal>test</defaultGoal>
		<plugins>
			<plugin>
				<groupId>org.jvnet.jaxb2.maven2</groupId>
				<artifactId>maven-jaxb2-plugin</artifactId>
				<configuration>
					<extension>true</extension>
					<useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
				</configuration>
			</plugin>
		</plugins>
	</build>
	...
</project>

Alternatively you can specify episode artifacts explicitly:

<project ...>
	...
	<dependencies>
		...
		<dependency>
			<groupId>com.acme.foo</groupId>
			<artifactId>a-schema</artifactId>
			<version>1.0</version>
		</dependency>
		...
	</dependencies>
	<build>
		<defaultGoal>test</defaultGoal>
		<plugins>
			<plugin>
				<groupId>org.jvnet.jaxb2.maven2</groupId>
				<artifactId>maven-jaxb2-plugin</artifactId>
				<configuration>
					<extension>true</extension>
					<episodes>
						<episode>
							<groupId>com.acme.foo</groupId>
							<artifactId>a-schema</artifactId>
							<!-- Version is not required if the artifact is
								configured as dependency -->
						</episode>
					</episodes>
				</configuration>
			</plugin>
		</plugins>
	</build>
	...
</project>

In this case JAXB will not generate classes for the imported A schema. The B.jar artifact will only contain classes relevant to the B schema.

Note that JAXB still needs to access BOTH A and B schemas during the compilation. You may use catalogs to provide alternative locations of the imported schemas.

See the [sample episode project] for example.

Integration with M2Eclipse

Since the version 0.8.1, maven-jaxb2-plugin provides "native" integration for M2Eclipse. That is, the plugin provides lifecycle mapping metadata and uses Plexus Build API to check if any relevant files were changes and to refresh the workspace after source code was generated. This should resolve the "plugin execution not covered by lifecycle configuration" problem.

This feature is supported in M2Eclipse since version 1.1.

Miscellaneous Problems

Target directory ignored by M2Eclipse if plugin is executed in generated-test-sources phase

Please see this issue.

The problem appears in Eclipse environments running M2Eclipse.

If maven-jaxb2-plugin is executed in the generate-test-sources phase, target directory is not considered by the M2Eclipse - therefore the project is missing target source folder (ex. target/generated-sources/xjc) in Eclipse.

This is not a bug. By default, M2Eclipse only executes the lifecycle only up to the process-resources phase. The generate-test-sources phase is coming after the process-resources phase and therefore is not executed. Accordingly, target directory is not added to the Maven project source directories.

To resolve, please change the following setting:

Window > Preferences > Maven > Goals to run when updating project configuration: > process-test-resources

Solving "Plugin execution not covered by lifecycle configuration" problem in Eclipse/M2Eclipse

There are following options:


Browse Space

- Pages
- News
- Labels
- Attachments
- Bookmarks
- Mail
- Advanced
- Activity

Explore Confluence

- Popular Labels
- Notation Guide

Your Account

Log In

 

Other Features

Add Content