kirill.login
kirill.login

Reputation: 951

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>

Upvotes: 3

Views: 2213

Answers (2)

Ratman
Ratman

Reputation: 166

I add some details to @kirill.login answer.

I managed to configure XSD name with the following steps. First, I annotated my root bean:

package com.mypackage.model.messages;

import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Packet", namespace="https://www.example.it/schemas/packets")
public class Packet {

Then I added in the same package the file package-info.java:

@XmlSchema(namespace = "https://www.example.it/schemas/packets")
package it.magentalab.datalogger.model.messages;

import jakarta.xml.bind.annotation.XmlSchema;

Finally, in my pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <!-- Generate XML Schema from sources -->
                <execution>
                    <id>jxc</id>
                    <goals>
                        <goal>schemagen</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/java/com/mypackage/model/messages</source>
                            <source>src/main/java/com/mypackage/model/messages/package-info.java</source>
                        </sources>
                        <transformSchemas>
                            <transformSchema>
                                <uri>https://www.example.it/schemas/packets</uri>
                                <toFile>packets.xsd</toFile>
                            </transformSchema>
                        </transformSchemas>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Triggering maven generate-sources phase I end up with target/packets.xsd.

Upvotes: 1

kirill.login
kirill.login

Reputation: 951

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>

Upvotes: 5

Related Questions