I am trying to generate Java code for two XSDs (let's say request.xsd and response.xsd) using JAXB
Request.xsd includes A.xsd which defines a complex type "FOO"
Response.xsd includes B.xsd which defines a complex type "FOO" too (different type with the same name).
Initially, I thought of adding to both XSDs something like in order to fix the name clash.
<xsd:annotation>
<xsd:appinfo>
<jaxb:class name="AFOO"/> (and BFOO)
</xsd:appinfo>
</xsd:annotation>
However, I still get the error that "FOO" is already defined. If I try to generate code only for request.xsd or only for response.xsd the code is generated correctly.
My initial pom.xml looks like:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<debug>true</debug>
<schemaDirectory>src/main/resources/xsds</schemaDirectory>
<generateDirectory>src/main/generated</generateDirectory>
<generatePackage>com.mycompany.generated</generatePackage>
<extension>true</extension>
<markGenerated>true</markGenerated>
<args>
<arg>-camelcase-always</arg>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-Xsetters</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</plugin>
<plugin>
<groupId>org.andromda.thirdparty.jaxb2_commons</groupId>
<artifactId>camelcase-always</artifactId>
<version>1.0</version>
</plugin>
</plugins>
</configuration>
</plugin>
Eventually, I managed to get it to work using different executions for request and response.
<executions>
<execution>
<id>Output</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<forceRegenerate>true</forceRegenerate>
<debug>true</debug>
<schemaDirectory>src/main/resources/xsds</schemaDirectory>
<generateDirectory>src/main/generated</generateDirectory>
<schemaIncludes>
<include>
response.xsd
</include>
</schemaIncludes>
<generatePackage>com.mycompany.generated.reply</generatePackage>
....
</configuration>
</execution>
<execution>
<id>Input</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<forceRegenerate>true</forceRegenerate>
<debug>true</debug>
<schemaDirectory>src/main/resources/xsds</schemaDirectory>
<generateDirectory>src/main/generated</generateDirectory>
<schemaIncludes>
<include>
request.xsd
</include>
</schemaIncludes>
<generatePackage>com.mycopnay.generated.query</generatePackage>
...
</configuration>
</execution>
</executions>
In this way, it works. However I have the following questions:
- Is there any better way to make it work without modifying the xsd? A lot of classes are shared between request and response are generated twice (under different java packages). Is there any way to reuse them?
- Why overriding the class name (my initial solution) does not work?