6

I'm trying to generated Java classes from a set of XSD files using the Maven XJC plugin with a custom binding. The customization is added to prefix JAXB generated classes. When the maven build runs, however, the XJC plugin doesn't seem to recognize the schemaBindings element and throws the exception as under.

[ERROR] Error while parsing schema(s).Location [ file:/C:/blah/bindings.xjb{5,25}].
com.sun.istack.SAXParseException2: The "jaxb:schemaBindings" customization is no
t associated with any schema element.
        at com.sun.tools.xjc.reader.internalizer.Internalizer.reportError(Intern
alizer.java:632)
        at com.sun.tools.xjc.reader.internalizer.Internalizer.reportError(Intern
alizer.java:626)
        at com.sun.tools.xjc.reader.internalizer.Internalizer.move(Internalizer.
java:451)
        at com.sun.tools.xjc.reader.internalizer.Internalizer.transform(Internal
izer.java:160)
        at com.sun.tools.xjc.reader.internalizer.Internalizer.transform(Internal
izer.java:109)
        at com.sun.tools.xjc.reader.internalizer.DOMForest.transform(DOMForest.j
ava:449)
        at com.sun.tools.xjc.ModelLoader.buildDOMForest(ModelLoader.java:345)
        at com.sun.tools.xjc.ModelLoader.loadXMLSchema(ModelLoader.java:377)
        at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:174)
        at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:119)
        at org.jvnet.mjiip.v_2_2.XJC22Mojo.loadModel(XJC22Mojo.java:45)
        at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:35)
        at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:22)
        at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute(RawXJC2Mojo.java:282)
        at org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute(RawXJC2Mojo.java:147)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPlugi
nManager.java:490)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(Defa
ultLifecycleExecutor.java:694)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLi
fecycle(DefaultLifecycleExecutor.java:556)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(Defau
ltLifecycleExecutor.java:535)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHan
dleFailures(DefaultLifecycleExecutor.java:387)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmen
ts(DefaultLifecycleExecutor.java:348)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLi
fecycleExecutor.java:180)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:6
0)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)

        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

The bindings.jxb is as follows.

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
           jaxb:version="2.1" schemaLocation="blah.xsd">      
    <jaxb:schemaBindings>
        <jaxb:nameXmlTransform>
              <jaxb:elementName suffix="Type"/>
        </jaxb:nameXmlTransform> 
    </jaxb:schemaBindings>
</jaxb:bindings>

The following plugins are used for the generating the Java Classes. Pasting the relevant mvn snippet here.

<plugin>   

    <groupId>org.jvnet.jaxb2.maven2</groupId>

    <artifactId>maven-jaxb2-plugin</artifactId>

    <version>0.8.0</version>

        <executions>

            <execution>

                <goals>

                    <goal>generate</goal>

                </goals>
            </execution>

        </executions>

        <dependencies>

            <dependency>

                <groupId>com.sun.xml.bind</groupId>

                <artifactId>jaxb-xjc</artifactId>

                <version>2.2.6</version>

            </dependency>

            <dependency>

                <groupId>com.sun.xml.bind</groupId>

                <artifactId>jaxb-impl</artifactId>

                <version>2.2.6</version>

            </dependency>

        </dependencies>
    <configuration>

                               <schemaDirectory>./src/main/resources</schemaDirectory>

                    <includeBindings>
                        <includeBinding>**/*.xjb</includeBinding>
                    </includeBindings>
                    <extension>true</extension>

                </configuration>

            </plugin>

The only other discussion I could find on this issue revolves about JAX-WS which I don't use as there's no WSDL files involved here. It's all XSD files here.

I don't see why this error has to occur because the JAXB's binding.xsd file indeed defines the schemaBindings element as seen below.

<xs:element name="schemaBindings" substitutionGroup="jaxb:declaration">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" ref="jaxb:package"/>
        <xs:element minOccurs="0" ref="jaxb:nameXmlTransform"/>
      </xs:sequence>
      <xs:attribute name="map" type="xs:boolean" />
    </xs:complexType>
</xs:element>

Any hints on this problem would be very much appreciated. Please let me know if more inputs are needed. Thanks!

EDIT: A lot of people on the Internet seem to have used the schemaBindings without any issues. May be this is an issue with the dependencies mentioned in the question?

Community
  • 1
  • 1
asgs
  • 3,928
  • 6
  • 39
  • 54
  • Are you sure you posted the right binding.xjb? Your schemaBindings element is just empty! – lexicore Mar 10 '13 at 12:51
  • Yes, that's the right binding file. And yes, it's empty. I've tried to add the `` element inside it, but in no vain. – asgs Mar 11 '13 at 00:06
  • 1
    But then it is obvious. Your jaxb:schemaBindings must associate bindings with a certain schema schema. Via schema location or scd. http://jaxb.java.net/guide/Customizing_Java_packages.html – lexicore Mar 12 '13 at 07:20
  • Thanks, I tried it for the first time it didn't work, but forgot it later. But the thing is how do I specify all of the XSD files in that attribute value? I got 6 XSD files right now. Edited the question for the latest snippet I'm using. – asgs Mar 13 '13 at 16:41
  • Wow. I tried adding as many bindings element as the number of XSD files I got and it worked. Didn't realize not specifying the schema caused the problem in question. This still doesn't solve the main goal, but yes, your answer was good enough to remove the error I was getting. Please post an answer so that I could accept it. Thanks again! – asgs Mar 13 '13 at 18:42
  • 1
    Example of context association via SCD https://stackoverflow.com/questions/54565167/xjc-binding-file-override-package-based-on-namespace-instead-of-schemalocation/ – gavenkoa Feb 07 '19 at 11:54
  • 1
    This is stuff others may find useful too (no dtd or xsd - just xml to start with) https://code.google.com/archive/p/jing-trang/downloads https://www.youtube.com/watch?v=j8Fzo55_T-w https://www.solidsyntax.be/2013/12/07/howto-use-jaxb-class-customization-avoid-xsd-conflicts/ https://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html#wp149350 – JGFMK Sep 09 '20 at 17:03

1 Answers1

4

Your jaxb:schemaBindings must associate bindings with a certain schema schema. Via schema location or scd. Please see http://jaxb.java.net/guide/Customizing_Java_packages.html

lexicore
  • 42,748
  • 17
  • 132
  • 221