Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

This is a quick guide to show how to use the maven-jaxb2-plugin to compile a schema that is dependent on another schema/module within the same set of Maven child projects while being able to build this schema separately as another Maven artefact.

Below is the layout of the Maven projects:
 Maven projects
schemas/
+-- pom.xml
+-- schema1/
| +-- pom.xml
| +-- src/main/resources/
| +-- schema1.xsd
+-- schema2/
+-- pom.xml
+-- src/main/resources/
+-- schema2.xsd


For this example, schema1 is completely stand-alone whereas schema2 depends on schema1. The requirement is to be able to build both and deploy into a Maven repository.

The parent pom.xml file will have entries like this (omitting all the boilerplate stuff)...
 schemas/pom.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
...
<modules>
<module>schema1</module>
<module>schema2</module>
</modules>
...
</project>


The schema1 pom.xml will have the following entries (again no boilerplate code shown)...
 schemas/schema1/pom.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
...
<parent>
...
</parent>
<artifactId>schema1</artifactId>
<version>1.0</version>
<name>${project.artifactId}</name>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<extension>true</extension>
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>schema1.xsd</include>
</includes>
</fileset>
</schema>
</schemas>
</configuration>
</plugin>
</plugins>
</build>
</project>




Now we need to come up with a catalog file. This requires looking into the namespace definition of schema1.xsd. Lets assume, it is something like this...
 schemas/schema1/src/main/resources/schema1.xsd
<xsd:schema xmlns="http://www.igorkromin.net/xjcexample/schema1"
...
</xsd:schema>


Using this information, a catalog.cat file needs to be created in the same directory as the schema1.xsd. In this case its contents will look like this...
 schemas/schema1/src/main/resources/catalog.cat
PUBLIC "http://www.igorkromin.net/xjcexample/schema1" "schema1.xsd"


What that does is map the namespace to the actual schema XSD file. This allows us to import the schema1.xsd into schema2.xsd without actually specifying its location. So inside schema2.xsd there will be an entry like this...
 schemas/schema2/src/main/resources/schema2.xsd
<xs:schema ...
xmlns:s1="http://www.igorkromin.net/xjcexample/schema1"
...
>
<xs:import namespace="http://www.igorkromin.net/xjcexample/schema1"/>
...



Now the schema2 pom.xml file will have entries like this...
 schemas/schema2/pom.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
...
<parent>
...
</parent>
<artifactId>schema2</artifactId>
<version>1.0</version>
<name>${project.artifactId}</name>
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>schema1</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<extension>true</extension>
<useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
<catalogs>
<catalog>
<dependencyResource>
<groupId>...</groupId>
<artifactId>schema1</artifactId>
<resource>catalog.cat</resource>
</dependencyResource>
</catalog>
</catalogs>
<schemas>
<schema>
<fileset>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>schema2.xsd</include>
</includes>
</fileset>
</schema>
</schemas>
</configuration>
</plugin>
</plugins>
</build>
</project>


That's it! The schema2 project will correctly reference schema1 via its catalog file and if you build the parent pom.xml file you will get two JARs, one for each schema.

-i

Did you like this post or found it useful? Considering supporting this Blog to keep its web servers running, any amount helps! Thanks!
Have comments or feedback on what I wrote? Please share them below!
comments powered by Disqus
Other posts you may like...