3

We have a common set of XSDs (datatypes, vocabulary, etc.) we're generating with the jaxb2-maven-plugin in its own Maven project. In a second project, I need to refer to one or more of those XSDs at compile time but don't want them included in the resulting artifact. I've created a catalog file, which works fine except I get everything in it.

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
    <execution>
        <goals>
            <goal>xjc</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <outputDirectory>${basedir}/src/main/java</outputDirectory>
    <target>2.1</target>
    <catalog>catalog.cat</catalog>
</configuration>

I've pored over the plugin docs, but they're woefully light on detail. Is there any way to get reuse out of common schemas without every project having to take a copy of them?

Thanks

lexicore
  • 42,748
  • 17
  • 132
  • 221
MolonLabe
  • 168
  • 2
  • 8

1 Answers1

1

This is something my maven-jaxb2-plugin can do:

Compiling schema from Maven Artifact

The documentation site is currently very unstable so here's snippets of the documentation.

<configuration>                                                                                         
    <forceRegenerate>true</forceRegenerate>                                                         
    <schemas>                                                                                       
        <schema>
            <dependencyResource>                                                            
                <groupId>org.jvnet.jaxb2.maven2</groupId>                               
                <artifactId>maven-jaxb2-plugin-tests-po</artifactId>                    
                <!-- Can be defined in project dependencies or dependency management -->
                <version>${project.version}</version>                                   
                <resource>purchaseorder.xsd</resource>                                  
            </dependencyResource>                                                           
        </schema>                                                                               
    </schemas>                                                                                      
</configuration>

Here's a sample project.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks for the response, lexicore. I tried the example but it led me to the very same spot: The XSD I'm trying to generate and compile can't find the XSD it depends on: org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'common.xsd' common.xsd is one of 4 XSDs in my global-xsd artifact. – MolonLabe Apr 07 '14 at 20:05
  • @MolonLabe I think you have to try more, this is known to work. See also the "episodes" test: https://svn.java.net/svn/maven-jaxb2-plugin~svn/trunk/tests/episodes/. The solution is probably a combination of `dependencyResource` and an episode or a catalog file which is remapping URLs/namespaces to Maven coordinates (see this catalog for instance: https://svn.java.net/svn/maven-jaxb2-plugin~svn/trunk/tests/episodes/e/src/main/resources/catalog.cat). This is tricky but this works. I think episodes is what you need. – lexicore Apr 08 '14 at 07:25
  • You were exactly right, lexicore. There were two problems: The reference in the XSD was out of sync with the catalog file and my dependency had a classifier, which I'd left out initially. It compiles now but it continues to include the dependencyResource's generated types. Because my artifact will be included it yet another project, I'd like to have exactly one copy of these generated resources on the classpath. Is there a way to exclude them, within the plugin config, or is this just done with in the maven-jar-plugin? Thanks again for the solution, this is very close. – MolonLabe Apr 08 '14 at 13:25
  • @MolonLabe Have you actually tried episodes? http://confluence.highsource.org/display/MJIIP/User+Guide#UserGuide-Separateschemacompilation – lexicore Apr 09 '14 at 14:14
  • I'm trying to use your plugin to run xjc on all .xsd files in a maven dependency, and the .xsd files are in different directories in the maven dependency .jar file. Is there a way to specify all .xsd file in a maven dependency? http://stackoverflow.com/questions/32533346/how-do-i-tell-the-jaxb2-maven-plugin-to-run-xjc-on-all-xsd-files-in-a-maven-dep – Dean Schulze Sep 11 '15 at 23:09