简体   繁体   中英

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM