Igor's Blog

I've been trying to get the maven-jaxb2-plugin to generate a JAXB jar out of XSDs that are stored inside a dependency that I have no control of. This was quite doable for a simple, single XSD Maven dependency that didn't import any other schemas, but when dealing with a more complex schema that did import multiple other XSDs from the same dependency jar, it didn't "just" work. After trying many approaches I found a solution however.

First lets see what the structure of the Maven dependency jar file looked like. This was the jar that contained XSDs that I was interested in.
 Jar Structure
my-dependency.jar
\-- org
\-- some
\-- package
\
|-- ns1.xsd
|-- ns2.xsd
|-- ns3.xsd


So the 'ns1.xsd' was the primary file for the schema. Inside it contained two imports, for both the other XSD files in the same package. That all looked like...
 XSD
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://mynamespace1" ...>
<xs:import namespace="http://mynamespace2" schemaLocation="ns2.xsd"/>
<xs:import namespace="http://mynamespace3" schemaLocation="ns3.xsd"/>
...


Simply referencing the schema as a Maven artifact either with or without using a catalog file produced the following error:
 Error
[ERROR] Error while parsing schema(s).Location [ maven:mygroup1:my-dependency:jar::!/org/some/package/ns1.xsd{30,534}].
org.xml.sax.SAXParseException; systemId: maven:mygroup1:my-dependency:jar::!/org/some/package/ns1.xsd; lineNumber: 30; columnNumber: 534; unknown protocol: maven
at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.resolveRelativeURL(NGCCRuntimeEx.java:229)
at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.importSchema(NGCCRuntimeEx.java:256)




After trying a myriad of different catalog REWRITE_SYSTEM combinations, I was ready to give up and just extract the XSDs to my project, but then I came across this which resolved all the errors that XJC was throwing at me. Incidentally when running as 'mvn -X' I could see that my rewrites were working and both ns2.xsd and ns3.xsd were being resolved to the correct Maven dependency, but for some reason they were still not being picked up.

The solution was to use a URL reference to the primary XSD and then in my catalog file to rewrite that URL to the Maven dependency location.

So my maven-jaxb2-plugin configuration contained the following (the URL didn't have to resolve to any public facing URL, it was just a placeholder in that case)...
 pom.xml
<strict>false</strict>
<catalog>src/main/resources/catalog.cat</catalog>
<schemas>
<schema>
<url>http://someurl/schema/ns1.xsd</url>
</schema>
</schemas>


The catalog file contained a single line that rewrote the URL above to the Maven dependency I was using. Note that the rewriting is done up to the package that the XSDs reside in.
 catalog.cat
REWRITE_SYSTEM "http://someurl/schema/" "maven:mygroup1:my-dependency:jar::!/org/some/package"



That was it! It worked like a charm! The only other place I had to change was in my binding.xml file. Because I had some property renaming to be do, I couldn't reference schemas by their file names and instead had to use the URL from above. For example...
 binding.xml
<jaxb:bindings schemaLocation="http://someurl/schema/ns2.xsd">
<jaxb:bindings node="//xs:attributeGroup[@name='attrGroup1']/xs:attribute[@name='attr1']">
<jaxb:property name="attr1_renamed"/>
</jaxb:bindings>
</jaxb:bindings>


-i

, , , ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Recent Blog Posts

Base64 Encode or Decode on the command line without installing extra tools on Linux, Windows or macOS

How to install Panel Glide Blinds from Blinds Online

Showing whitespace characters in vi

Using Jackson to automatically convert Java POJOs to JSON as output of a Jersey service

Enable quick access to the keyboard and emoji viewer in macOS

How to tell if your site is served via CloudFlare

Googe Cloud Platform doesn't support CloudFlare's free edge certificates

Fixing the invalid private key error in the Google Cloud Platform App Engine app Settings page

Cleaning the sticky PlayStation controller analogue thumb sticks

Invoking interactive shell scripts non-interactively

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again