'jaxb2 When using multiple local schemas that depend on one another classes are generated multiple times

I am trying to generate the java classes from some xsd files, the files are all in the same maven project and do have some interdependencies.

I have tried merging all the execution steps into one, which does solve one problem, but I want to have separate visitor classes generated for each type (i.e. a visitor class for types in schema A and visitor class for types in schema B etc.)

I initially tried to generate the source code files in the same directory, with the hope that the newer files would overwrite the old files, however, that did not work (setting forceRegenerate to true, resulted in all the old files being deleted and setting forceRegenerate to false resulted in only the files from the first execution being generated)

These are my current schemas


<!-- d.xsd  !-->
<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="a">    
           
            <xs:complexType name="AType">
               <xs:attribute name="name" type="xs:string"/>
            </xs:complexType>
          
</xs:schema>

<!-- b.xsd  !-->
<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="b">   
                  
            <complexType name="BType">
               <attribute name="name" type="xs:string"/>
            </complexType>
          
</xs:schema>


<!-- c.xsd  !-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:a="a">
    <xs:import namespace="a" schemaLocation="a.xsd"/>

    <xs:complexType name="c">
        <xs:element name="name" type="a:AType"/>
    </xs:complexType>
    
</xs:schema>

<!-- d.xsd  !-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:a="a"
           xmlns:b="b">
    <xs:import namespace="a" schemaLocation="a.xsd"/>
    <xs:import namespace="b" schemaLocation="b.xsd"/>

    <xs:complexType name="d">
        <xs:element name="name" type="a:AType"/>
    </xs:complexType>

    <xs:complexType name="e">
        <xs:element name="name" type="b:BType"/>
    </xs:complexType>
    
</xs:schema>

This is my current maven-jaxb2-plugin configuration

<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <version>0.14.0</version>
    <executions>
    <execution>
       <id>1</id>
       <goals>
           <goal>generate</goal>
       </goals>
       <configuration>
           <generatePackage>com.generated</generatePackage>
           <generateDirectory>${basedir}/generated/</generateDirectory>
           <schemaDirectory>${basedir}/src/main/resources/xsd/types</schemaDirectory>
           <schemaIncludes>
               <include>a.xsd</include>
           </schemaIncludes>
           <debug>true</debug>
           <extension>true</extension>
           <removeOldOutput>true</removeOldOutput>
           <plugins>
               <plugin>
                   <groupId>com.massfords</groupId>
                   <artifactId>jaxb-visitor</artifactId>
                   <version>2.7</version>
               </plugin>
           </plugins>
       </configuration>
    </execution>

    <execution>
       <id>2</id>
       <goals>
           <goal>generate</goal>
       </goals>
       <configuration>
           <generatePackage>com.generated</generatePackage>
           <generateDirectory>${basedir}/generated/b/</generateDirectory>
           <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
           <schemaIncludes>
               <include>b.xsd</include>
           </schemaIncludes>
           <extension>true</extension>
           <useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
           <args>
               <arg>-Xvisitor</arg>
               <arg>-Xvisitor-package:com.generated.visitor.b</arg>
           </args>
           <plugins>
               <plugin>
                   <groupId>com.massfords</groupId>
                   <artifactId>jaxb-visitor</artifactId>
                   <version>2.7</version>
               </plugin>
           </plugins>
       </configuration>
    </execution>

    <execution>
       <id>3</id>
       <goals>
           <goal>generate</goal>
       </goals>
       <configuration>
           <generatePackage>com.generated</generatePackage>
           <generateDirectory>${basedir}/generated/c/</generateDirectory>
           <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
           <schemaIncludes>
               <include>c.xsd</include>
           </schemaIncludes>
           <extension>true</extension>
           <useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
           <args>
               <arg>-Xvisitor</arg>
               <arg>-Xvisitor-package:com.generated.visitor.c</arg>
           </args>
           <plugins>
               <plugin>
                   <groupId>com.massfords</groupId>
                   <artifactId>jaxb-visitor</artifactId>
                   <version>2.7</version>
               </plugin>
           </plugins>
       </configuration>
    </execution>
  </executions>
</plugin>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source