jaxb2-maven-plugin icon indicating copy to clipboard operation
jaxb2-maven-plugin copied to clipboard

JAXB Maven plugin won't generate Java Enum

Open oliverunger opened this issue 1 year ago • 4 comments

I have the following snippet in my xsd file. I would expect that it generates a Java Enum with the two elements STANDALONE and CONNECTED.

<xs:attribute name="Sample">
  <xs:annotation>
    <xs:documentation>A sample</xs:documentation>
  </xs:annotation>
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Standalone"/>
      <xs:enumeration value="Connected"/>
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>

I'm using jaxb2-maven-plugin version 3.1.0

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>gen-infrastructure</id>
      <goals>
        <goal>xjc</goal> <!-- Generates Java sources from XML Schema(s). -->
      </goals>
      <configuration>
        <locale>en,US</locale>
        <xjbSources>
          <xjbSource>src/main/resources/schema/xsd/sample.xjb</xjbSource>
        </xjbSources>
        <sources>
          <source>src/main/resources/schema/xsd/sample.xsd</source>
        </sources>
        <!-- The package of your generated sources -->
        <packageName>com.company.sample.model.pojos</packageName>
      </configuration>
    </execution>
  </executions>
  <configuration>
    <noGeneratedHeaderComments>true</noGeneratedHeaderComments>
    <clearOutputDir>false</clearOutputDir>
  </configuration>
</plugin>

oliverunger avatar Sep 05 '23 13:09 oliverunger

I faced the same issue. The following schema changes solved my issue:

  • xmlns:jaxb="http//java.sun.com/xml/ns/jaxb" to xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
  • jaxb:version=2.0 to jaxb:version=3.0

tstuber avatar Dec 18 '23 07:12 tstuber

@tstuber Does not change anything for me

oliverunger avatar Jan 03 '24 07:01 oliverunger

@oliverunger I have similar issue, it looks like this is the default behaviour of xjc (if I am wrong here, please correct me).

You can still configure xjc to generate enums through binding configuration (general info). Maybe this post would be helpful?

lwiechec avatar Jun 18 '24 11:06 lwiechec

I was able to narrow down the problem further:

JAXB will create an enum class for this:

 <xs:simpleType name="workingSample">
        <xs:annotation>
            <xs:documentation>Working sample</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:enumeration value="Hello"/>
            <xs:enumeration value="World"/>
        </xs:restriction>
    </xs:simpleType>

But will fail on this:

 <xs:attribute name="failingSample">
        <xs:annotation>
            <xs:documentation>Failing sample</xs:documentation>
        </xs:annotation>
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="Lorem"/>
                <xs:enumeration value="Ipsum"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>

oliverunger avatar Jul 07 '24 21:07 oliverunger