Generate JAXB classes with Maven based on multiple schema’s

Earlier, we blogged about generating JAXB classes based on XSD files. However, sometimes we need multiple configurations with multiple XSD files. Previous blog posts did not cover this topic, so I thought I’d cover it in a seperate post.

The solution is to add multiple executions for the maven-jaxb2-plugin plugin. Each execution is bound to the generate-resources phase of the Maven build process. Now, each time the project is build, the executions kick in and the JAXB classes are generated.

Behold the following configuration:

<plugin>
	<groupId>org.jvnet.jaxb2.maven2</groupId>
	<artifactId>maven-jaxb2-plugin</artifactId>
	<version>0.7.0</version>
	<executions>
		<execution>
			<id>One</id>
			<phase>generate-resources</phase>
			<goals>
				<goal>generate</goal>
			</goals>
			<configuration>
				<schemaDirectory>${xsd.path}</schemaDirectory>
				<schemaIncludes>
					<include>schema-1.xsd</include>
					<include>schema-2.xsd</include>
				</schemaIncludes>
				<readOnly>true</readOnly>
				<removeOldOutput>true</removeOldOutput>
				<verbose>true</verbose>
				<extension>true</extension>
				<bindingDirectory>${jaxb.path}/</bindingDirectory>
				<bindingIncludes>
					<bindingInclude>jaxb-bindings-one.xml</bindingInclude>
				</bindingIncludes>
			</configuration>
		</execution>
		<execution>
			<id>Two</id>
			<phase>generate-resources</phase>
			<goals>
				<goal>generate</goal>
			</goals>
			<configuration>
				<schemaDirectory>${xsd.path}</schemaDirectory>
				<schemaExcludes>
					<exclude>schema-1.xsd</exclude>
					<exclude>schema-2.xsd</exclude>
				</schemaExcludes>
				<readOnly>true</readOnly>
				<removeOldOutput>false</removeOldOutput>
				<forceRegenerate>true</forceRegenerate>
				<verbose>true</verbose>
				<extension>true</extension>
				<bindingDirectory>${jaxb.path}/</bindingDirectory>
				<bindingIncludes>
					<bindingInclude>jaxb-bindings-two.xml</bindingInclude>
				</bindingIncludes>
			</configuration>
		</execution>
	</executions>
</plugin>	

Execution “One” will generate the JAXB classed for schema-1.xsd and schema-2.xsd (all other schema’s in path ${xsd.path} will not be processed) and uses jaxb-bindings-one.xml as bindingfile. Execution “Two” uses all other XSD’s in directory ${xsd.path} and uses jaxb-bindings-two.xml.

The trick is to use removeOldOutput properly. By default, this has the value true and will cause the generateDirectory to be deleted before the XJC binding compiler recompiles the source files. So, by default, the second execution will delete the result of the first execution before recompiling. Setting this to value false in the second execution solves our problem. Now, both configurations are executed successfully, each with its own binding file.

Advertisements

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in General, JAXB, Maven and tagged . Bookmark the permalink.