Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Maven JAXB-2 plugin multiple schemes with different configurations doesn't generate classes

We are moving our existing project from Ant + Eclipse to Maven + IntelliJ IDEA. I am currently using JAXB to generate classes from xsd files. I want to continue the current project structure so i want jaxb2-maven-plugin to generate the classes in a specific location. I have multiple schemes and want to generate the classes in different locations. I'm using multiple plugin execution bindings in order to do that as instructed in the JAXB-2 Maven plugin site. My problem is that only the first execution is performed. None of the classes in the second execution are generated. Here is my POM.xml file relevant part:

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <id>schema1</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
                        <schemaFiles>myschema1.xsd</schemaFiles>
                        <packageName>xml</packageName>
                        <outputDirectory>${basedir}/src/main/java/com/example/dor/a</outputDirectory>
                        <arguments>-extension -Xcloneable -Xdefault-value -Xsetters -Xannotate</arguments>
                        <staleFile>${build.directory}/.jaxb-staleFlag-1</staleFile>
                        <clearOutputDir>false</clearOutputDir>
                    </configuration>
                </execution>
                <execution>
                    <id>schema2</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>
                        <schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
                        <schemaFiles>myschema2.xsd</schemaFiles>
                        <packageName>xml</packageName>
                        <outputDirectory>${basedir}/src/main/java/com/example/dor/b</outputDirectory>
                        <arguments>-extension -Xcloneable -Xdefault-value -Xsetters -Xannotate</arguments>
                        <staleFile>${build.directory}/.jaxb-staleFlag-1</staleFile>
                        <clearOutputDir>false</clearOutputDir>
                    </configuration>
                </execution>
            </executions>
</plugin>
like image 432
gamlieldor Avatar asked Feb 09 '26 17:02

gamlieldor


2 Answers

An update answer using version 2.5.0 of the plugin. This would be the configuration:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>2.5.0</version>
  <executions>

    <execution>
      <id>schema1</id>
      <goals>
        <goal>xjc</goal>
      </goals>
      <configuration>
        <packageName>com.your.package</packageName>
        <sources>
          <source>${project.basedir}/src/main/resources/xsd/sample1/sample1.xsd</source>
        </sources>
        <clearOutputDir>false</clearOutputDir>
      </configuration>
    </execution>

    <execution>
      <id>schema2</id>
      <goals>
        <goal>xjc</goal>
      </goals>
      <configuration>
        <packageName>com.your.package</packageName>
        <sources>
          <source>${project.basedir}/src/main/resources/xsd/sample2/sample2.xsd</source>
        </sources>
        <clearOutputDir>false</clearOutputDir>
      </configuration>
    </execution>

  </executions>
</plugin>

Hope it helps for newer configurations.

like image 115
Simone Colnaghi Avatar answered Feb 12 '26 08:02

Simone Colnaghi


I would upgrade to 1.6, and you will have to put the 2 schemas in different packages to stop a conflict in the generated ObjectFactory. Below works for me:

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>schema1</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
                            <schemaFiles>myschema1.xsd</schemaFiles>
                            <packageName>xml.a</packageName>
                            <outputDirectory>${basedir}/src/main/generated1</outputDirectory>
                            <clearOutputDir>true</clearOutputDir>
                        </configuration>
                    </execution>
                    <execution>
                        <id>schema2</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <schemaDirectory>${basedir}/src/main/resources/schemes</schemaDirectory>
                            <schemaFiles>myschema2.xsd</schemaFiles>
                            <packageName>xml.b</packageName>
                            <outputDirectory>${basedir}/src/main/generated2</outputDirectory>
                            <clearOutputDir>true</clearOutputDir>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
like image 39
Essex Boy Avatar answered Feb 12 '26 06:02

Essex Boy