:::: MENU ::::

JAXB2


JAXB2 Maven Plugin is the plugin that JAXB uses to create Java Beans from XSD schemas.

Generate a new Maven project

In Eclipse, we create a new Maven project.

Dependency inclusion

We add to the pom.xml this dependency:

<dependency>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>jaxb2-maven-plugin</artifactId>
	<version>2.2</version>
	<type>maven-plugin</type>
</dependency>

We save it and do a Maven Update Project.

Execution configuration

Now, we create an execution type, a “xjc” one (in order to generate beans from schemas). In JAXB there are more execution types as, i.e., to create schemas from Java Beans.

<execution>
	<id>xjc</id>
	<goals>
		<goal>xjc</goal>
	</goals>
</execution>

After, we create a configuration (in this case it will be common to every execution). Here we specify where we will take the schemas and in what package we will create the beans.

<configuration>
	<sources>
		<source>${basedir}/src/main/resources</source>
	</sources>
	<packageName>com.example.myschema</packageName>
</configuration>

In this case it is possible that an error will appear on the pom.xml file “null (org.codehaus.mojo:jaxb2-maven-plugin:2.2:xjc:xjc:generate-sources)”. The solution is to execute the plugin, as we will see now.

Plugin execution

To do this, we will right clic on the project and then “Run as…/Maven generate-sources”.

Success review

Java Beans will be in the specified folder at the pom.xml file (in this case, target/generated-sources/jaxb).

This example code is available at https://github.com/luisgomezcaballero/JAXB2-Maven-Plugin.


So, what do you think ?