javamavenxsdjaxbjaxb2-maven-plugin

How to predefine XSD file name with jaxb2-maven-plugin


I use a code below to generate an XSD from annotated java-classes. Default name of the XSD is always "schema1.xsd". How should I pre-define it using only that plugin? At the moment I use maven-antrun-plugin for file renaming. Plugin manual doesn't contain relevant information.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>2.3</version>
  <executions>
    <execution>
      <goals>
        <goal>schemagen</goal>
      </goals>
      <phase>generate-sources</phase>
      <configuration>
        <sources>
          <source>src/main/java/***some package***</source>
        </sources>
        <outputDirectory>${project.build.directory}/generated-sources/schemas</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

Solution

  • Answer has been found. It is not enough to only annotate your java-classes with the JAXB annotations. In the DTO package should exist file "package-info.java" with the following content:

    @XmlSchema(namespace = "http://your-namespace")
    package com.your.package;
    
    import javax.xml.bind.annotation.XmlSchema;
    

    And the plugin declaration should look like this:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>jaxb2-maven-plugin</artifactId>
      <version>2.3</version>
      <executions>
        <execution>
          <goals>
            <goal>schemagen</goal>
          </goals>
          <phase>generate-sources</phase>
        </execution>
      </executions>
      <configuration>
        <sources>
          <source>src/main/java/com/your/package</source>
        </sources>
        <outputDirectory>${project.build.directory}/generated-sources/schemas</outputDirectory>
        <transformSchemas>
          <transformSchema>
            <uri>http://your-namespace</uri>
            <toFile>your-namespace.xsd</toFile>
          </transformSchema>
        </transformSchemas>
      </configuration>
    </plugin>