Jaxb2-annotate-plugin不适用于剧集

时间:2018-02-01 11:13:55

标签: jaxb jaxb2 xjc jaxb2-maven-plugin jaxb2-annotate-plugin

我正在尝试对生成的JAXB类强制执行枚举验证,但是我遇到了一些限制它们的问题。

基本的XSD设置是:

-enumsXSD
-2 other XSDs that import this XSD

为了防止我的课程重复,我正在使用剧集,但在enumsXSD中添加注释时看起来这并不好看

com.sun.istack.SAXParseException2: compiler was unable to honor this annox:annotateEnumValueMethod customization. It is attached to a wrong place, or its inconsistent with other bindings.

[ERROR] Error while generating code.Location [ file:somewhere/generic.episode{64,99}].
com.sun.istack.SAXParseException2: (the above customization is attached to the following location in the schema) 

代码:

<plugins>
      <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.13.3</version>
        <configuration>
          <extension>true</extension>
          <args>
            <arg>-Xannotate</arg>
          </args>
          <plugins>
            <plugin>
              <groupId>org.jvnet.jaxb2_commons</groupId>
              <artifactId>jaxb2-basics-annotate</artifactId>
              <version>1.0.4</version>
            </plugin>
          </plugins>
        </configuration>
        <executions>
          <!--GENERIC ENUMS -->
          <execution>
            <id>ENUMS</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <extension>true</extension>
            <generatePackage>com.foo.generic.enums</generatePackage>
              <generateDirectory>${project.build.directory}/generated-sources/xjc1/generic</generateDirectory>
              <!-- Define the directory where we should find the XSD files -->
              <schemaDirectory>
                src/main/resources/dtd/generic
              </schemaDirectory>
              <schemaIncludes>
                <source>enums.xsd</source>
              </schemaIncludes>
              <episodeFile>
                ${project.build.directory}/generated-sources/xjc1/generic/META-INF/generic.episode
              </episodeFile>
            </configuration>
          </execution>
          <execution>
            <id>A_XSD</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <extension>true</extension>
              <bindingDirectory>${project.build.directory}/generated-sources/xjc1/generic/META-INF</bindingDirectory>
              <bindingIncludes>
                <include>generic.episode</include>
              </bindingIncludes>

              <!-- Set the package of the generated code -->
              <generatePackage>com.foo.something</generatePackage>
              <generateDirectory>${project.build.directory}/generated-sources/xjc1/a_something</generateDirectory>

              <!-- Define the directory where we should find the XSD files -->
              <schemaDirectory>
                src/main/resources/dtd/someplace/a/
              </schemaDirectory>
              <schemaIncludes>
                <source>*.xsd</source>
              </schemaIncludes>
            </configuration>
          </execution>
     </executions>
   <plugin>
</plugins>

并在枚举XSD中

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" id="commonEnums"
           targetNamespace="http://foo.com/xsd/commons/enum"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           jaxb:version="2.1"
           xmlns:annox="http://annox.dev.java.net"
           jaxb:extensionBindingPrefixes="annox">
<xs:simpleType name="bulletinCategory">
    <xs:annotation>
      <xs:appinfo>
        <annox:annotateEnumValueMethod>@java.lang.Deprecated</annox:annotateEnumValueMethod>
      </xs:appinfo>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:enumeration value="valueA" />
      <xs:enumeration value="valueB" />
      <xs:enumeration value="valueC" />
    </xs:restriction>
  </xs:simpleType>
</xs>

理想情况下@deprecated应该是@XmlJavaTypeAdapter(FooAdapter.class),但我认为让我们从小做起。

1 个答案:

答案 0 :(得分:0)

问题可能是您已将自定义项直接包含在enums.xsd中。因此,当您编译其他模式时,也会考虑这些自定义。

但是,由于您似乎也使用enums.xsd作为剧集,这有效地防止了枚举类的生成。如果没有枚举类,则无法应用自定义项,因此未将其标记为&#34;已识别&#34;这会产生你得到的错误。

要解决此问题,在编译enums.xsd时,只需将自定义项移动到单独的绑定文件中,并在编译期间使用它。

然后,在编译其他模式时,使用enums.xsd作为剧集,但使用该绑定文件和enums.xsd的自定义。