0

i have a problem. I have a really big company XSD file which contains definition of many objects across many systems. I wan't to generate some java classes in my separate project from this XSD but I don't want to generate all classes defined in the XSD because I don't simply need them.

Is it possible to specify something like XSD root node for generating java classes using JAXB in the Maven?

I hope my question is clear :)

Jiri Vlasimsky
  • 684
  • 6
  • 13

2 Answers2

0

Your question is something I see very often, I would say typical for large bodies of XSD.

Unfortunately, I am not aware of a JAXB-way to control the generation process, not the way you want.

An alternate solution I've developed for this, hence my bias from this point forward, relies on automatic XML Schema Refactoring (XSR). It basically takes in your XSD, and from a set of XSD objects (in your case probably a couple of global elements and maybe some types), it'll generate a subset of XSDs that would only contain the necessary items, no fluff. Having those XSDs put through JAXB, it'll give you exactly what you want. This involves QTAssistant, and its XSR functionality. The highlevel steps are:

  • build a new XSR file;
  • refer to your source XSDs in an XML Schema Collection
  • create a "release": a graphical editor helps you with it. Basically, you match the top level XSD objects you want, and the new XSD file locations.
  • Generate the new XSDs.
  • Use the new XSDs with your artifacts.

QTAssistant supports command line integration with Maven through the Exec Maven Plugin, but only on Windows.

Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • Thanks for your response. Unfortunatelly I cannot run external program from my application. It's part of big business application deployed across many environments. – Jiri Vlasimsky Jun 12 '12 at 12:36
  • You're already said you want to run JAXB through Maven... Your comment is then confusing in the context of the question. I would assume that your application is being built as you try to generate the JAXB classes. – Petru Gardea Jun 12 '12 at 13:00
  • I am not able to install any external program on all our machines. So Exec Maven Plugin is good progress on my development machine (where I can install whatever I want) but on the production is useless for me because I can't install any external program. – Jiri Vlasimsky Jun 13 '12 at 08:30
0

There is a plugin for generating Java classes that can take XJC arguments, that might be a hook inside more advanced configurations. But I am not familiar with these.

Taken from the source of the plugin:

/**
 * Space separated string of extra arguments,
 * for instance <code>-Xfluent-api -episode somefile</code>;
 * These will be passed on to XJC as
 * <code>"-Xfluent-api" "-episode" "somefile"</code> options.
 * 
 * @parameter expression="${xjc.arguments}"
 */
protected String arguments;

pom.xml example of the plugin configuration:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.3.1</version>
<executions>
    <execution>
        <id>xjc</id>
        <phase>process-resources</phase>
        <goals>
            <goal>xjc</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <packageName>package.name</packageName>
    <schemaDirectory>${basedir}/src/main/webapp/WEB-INF/xsd</schemaDirectory>
    <bindingDirectory>${basedir}/src/main/java</bindingDirectory>
</configuration>
</plugin>
GallifreyanCode
  • 201
  • 1
  • 2
  • Yes this is probably correct plugin for me. I am little bit confused from many variations of jaxb2 maven plugin. It looks that everyone works similar. But using this approach combined with the binding file looks it can solve my problem. Only issue what I have is that this jaxb2 plugin doesn't respect namespaces as xjc command line command. Xjc command normally creates folders for every namespace and sources are generated into these folders according to their namespace. This plugin generates every java class into one folder and it causes duplicate errors. Any hint ? :) – Jiri Vlasimsky Jun 13 '12 at 08:50
  • Are you able to configure the plugin with the command line config instead or does it only work with additional configurations not covered by the plugin? A solution that I found a lot on the internet is using different xsds in different execution/configuration blocks like mentioned in [the answer here](http://stackoverflow.com/questions/2857081/how-can-i-tell-jaxb-maven-to-genereate-multiple-schema-packages)... – GallifreyanCode Jun 14 '12 at 07:31
  • ...With this approach it seems that another jaxb maven plugin offers a more elegant configuration [see answer about CXF XJC Maven Plugin](http://stackoverflow.com/questions/2432859/difference-of-maven-jaxb-plugins). I know that they deal with different Maven plugins which makes it more confusing. I mentioned the one that works for me, I have no experience with the others. I hope the linked answers will quickly provide a working configuration. (it didn't fit in 1 comment) – GallifreyanCode Jun 14 '12 at 07:31