2010/11/22

Maven - Generating Code with JAXB Plugin

We wanted to generate sources from multiple scattered XSDs. These schema definition files described classes in different packages with the same prefix. The setting for such configuration is not so straightforward  so I put together this small working example.

Possible sources of problems:
  1. Plugin uses lock file to determine when schema files are newer than generated files. Using the the same or default lock file name causes only first xjc plugin execution is run.
  2. By default the destination directory is cleared. With different XSDs targeting the sub-directories of the same directory you likely get that later executions deleting previously generated sources.

<build>
  <plugins>
    <plugin>

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

      <configuration>
        <quiet>true</quiet>
        <verbose>false</verbose>
        <clearOutputDir>false</clearOutputDir>
        <readOnly>true</readOnly>
        <arguments>-mark-generated</arguments>
      </configuration>

      <executions>

        <execution>
          <id>snmp</id>
          <goals><goal>xjc</goal></goals>
          <configuration>
            <packageName>org.bithill.testing..config</packageName>
            <schemaDirectory>${schema.dir}/config</schemaDirectory>
            <schemaFiles>config.xsd</schemaFiles>
            <staleFile>${build.directory}/generated-sources/.jaxb-staleFlag-config</staleFile>
          </configuration>
        </execution>

        <execution>
          <id>app</id>
          <goals><goal>xjc</goal></goals>
          <configuration>
            <packageName>org.bithill.testing.app</packageName>
            <schemaDirectory>${schema.dir}/app</schemaDirectory>
            <schemaFiles>app.xsd</schemaFiles>
            <staleFile>${project.build.directory}/generated-sources/.jaxb-staleFlag-app</staleFile>
          </configuration>
        </execution>

      </executions>
    </plugin>
  </plugins>
</build>

No comments:

Post a Comment