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

NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ for more details.
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

Skip down to comments...
Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
 
comments powered by Disqus
Other posts you may like...