简体   繁体   中英

xjc send different classes to different packages

I am using JAXB to create classes from an xsd schema. Is there a way to tag individual complexTypes so that they are sent to different destination packages rather than have them all sent to the same package?

The reason I need this is that a lot of the resulting java classes will be replaced with other classes of the same names from a different source. It is a pain to manually remove all the unused classes from one destination folder. It would be easier to just put all the unwanted classes in a different folder, which I could then ignore when I import the output into eclipse. When I tried to simply exclude the complexType definitions from the unwanted classes from the xsd file, xjc threw errors each time it encountered a property of one of the unwanted types. I need to keep the property references, but I want the eventual java references to those properties to point to classes of the same name that have already been generated by other means.

Here is my build.xml method for compiling the classes. How would I change it?

  <!--compile Java source files-->
  <target name="compile" description="Compile all Java source files">
    <echo message="Compiling the schema..." />
    <mkdir dir="gen-src" />
    <mkdir dir="gen-src/primer/po" />
    <xjc schema="somefilename.xsd" package="primer.po" destdir="gen-src" binding="rename.xjb">
      <produces dir="gen-src/primer/po" includes="**/*.java" />
    </xjc>
    <echo message="Compiling the java source files..." />
    <mkdir dir="classes" />
    <javac destdir="classes" debug="on">
      <src path="src" />
      <src path="gen-src" />
      <classpath refid="classpath" />
    </javac>
  </target>

I think two things may help here:

  • Customize your schemas with schemaBindings to put classes from different namespaces in different packages.
  • Use jaxb:class/@ref to map types to existing classes.

schemaBindings

I generally do not recommend using the package or generatePackage options to configure the output package. Use the schemaBindings binding element.

Example:

<jaxb:bindings 
    schemaLocation="ogc/wps/1.0.0/wpsAll.xsd" 
    node="/xs:schema">

    <jaxb:schemaBindings>
        <jaxb:package name="net.opengis.wps.v_1_0_0"/>
    </jaxb:schemaBindings>
</jaxb:bindings>

jaxb:class/@ref

If you do

 <xs:complexType name="vehicle">
    <xs:annotation><xs:appinfo>
      <jaxb:class ref="com.acme.foo.Vehicle" />
    </xs:appinfo></xs:annotation>
  </xs:complexType>

Then XJC should generate no new class for Vehicle but use com.acme.foo.Vehicle instead.

This in combination with schemaBindings may saolve your problem. Not directly, but it does.

Docs:

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