简体   繁体   中英

Maven JAXB - How to specify root node for generating from XSD

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 :)

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM