Offline Resolution of XSD’s using Maven jaxb

There can be scenario’s where the application which is placed in a server that is restricted to download the XSD’s from the target namespace.

This Article will demonstrate how to resolve the XSD and manipulate the JAR to create the Java Beans using Maven Plugin.

Here as an example, we would be using XML Encryption Library and use it in the other projects. Some of the Encrypted contents on the child library will be fetched from this parent library .

Implementation

Here is the simple project structure of the parent library which will be used in other project. The XML Encryption core library is being resolved in this project. We have downloaded the XSD from the official site of the W3 Organization. Some of the important files are described below:

imported-bindings.xsd : They are used to globally expose the logical prefix of the whole xsd. In all the projects using this library, We will be using this prefix for denoting this XSD.

xenc-schema.xsd : This main file is downloaded from the w3 org and will be used offline for the resolving the schema.

catalog.cat : This file is present in the main root path of the project folder and will be used for REWRITE /PUBLIC rule. We will be using this file in this folder to denote the public rules. The Public rule will be exposed while building this maven repository. This rule is to denote the maven compiler while searching for the internet to search the website location , look inside the project itself.

All the above files are important to keep in the project as they form the preliminary layer of the project. Next we will show the main section in the pom where we will using jaxb-maven plugin to integrate all the above files together using some special sections in the plugin.

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <catalog>catalog.cat</catalog>
                            <generateDirectory>${project.basedir}/target/generated</generateDirectory>
                            <extension>true</extension>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <schemaIncludes>
                        <include>*.xsd</include>
                    </schemaIncludes>
                    <bindingDirectory>src/main/resources</bindingDirectory>
                    <bindingIncludes>
                        <include>imported-bindings.xjb</include>
                    </bindingIncludes>
                    <args>
                        <arg>-XtoString</arg>
                        <arg>-Xnamespace-prefix</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.4</version>
                        </plugin>
                    </plugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-namespace-prefix</artifactId>
                        <version>1.1</version>
                    </dependency>
                </dependencies>
            </plugin>

In the above code, We have add the path to catalog file and schema binding file and directories. After adding the proper commands inside the plugin, We will build the project using simple maven install commands. Once the project is successfully, All the newly created java classes will be present under target/generated folder.(Maven built to be done using offline mode)

Now for the interesting part!!! We will be using this maven built library for all the other projects. Following changes would be needed to incorporate the build in the other projects.

In the actual XSD using this library, there will be a section mentioned in the following snippet

Here, The above url denotes to download the root xsd from the internet and resolving the same using building the jar. We will keep the same and make the following modification in the catalog file of this project folder.

Here, We will be using the REWRITE_SYSTEM rule to direct the path of the url to the previously build library and the same will be picked up while maven built of this individual project.

In the pom.xml, We will adding the dependencies of the root folder and also add the plugin in the following format.

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.13.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <catalog>src/main/resources/cvc-solution-catalog.cat</catalog>                       <generateDirectory>${project.basedir}/target/generated</generateDirectory>
                        <extension>true</extension>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <episodes>
                    <episode>
                        <groupId>org.w3._2001._04.xmlenc</groupId>
                        <artifactId>xmlenc</artifactId>
                    </episode>             
                </episodes>
                <args>
                    <arg>-Xequals</arg>
                    <arg>-XtoString</arg>
                    <arg>-Xwildcard</arg>
                    <arg>-Xnamespace-prefix</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.4</version>
                    </plugin>
                </plugins>
                <strict>false</strict>
                <schemaDirectory>src/main/resources</schemaDirectory>
                <schemaIncludes>
                    <include>**/*.xsd</include>
                </schemaIncludes>            
                <bindingDirectory>./</bindingDirectory>
                <bindingIncludes>
                    <include>imported-bindings.xjb</include>
                </bindingIncludes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-namespace-prefix</artifactId>
                    <version>1.1</version>
                </dependency>
            </dependencies>
        </plugin>

We are using episode inside the plugin to avoid adding the already generated classes of the root folder inside this current directory.

Finally, We are done. With this structural hierarchy, We can keep all the parent XSD built offline and use them as per our requirements in the project using the above orchestrations.

Conclusion

This is an example of using JAXB plugins in a structured way to resolve the internet downloadable dependencies offline. There are obviously some pros and cons of using this approach. But the use case in which the system sits inside a much restricted ecosystem, This approach would be a feasible solution to fetch the dependencies without touching the network configurations.

Some of the references which are useful for the tutorials.

https://en.wikipedia.org/wiki/XML_catalog

http://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.4/

https://github.com/highsource/maven-jaxb2-plugin/wiki/Using-Catalogs

Be the first to comment

Leave a Reply

Your email address will not be published.


*