JAXB 2 Maven plugin - use of episode - general project for xsd

1.1k views Asked by At

I want to have a project A with some common used XML schema files and some java classes dependent on the generated classes (generated from jaxb), using namespace a and package a, and I want to have a project B dependent on project A.

in project B i want to have a XML schema file using some XML types from project A, project b has namespace b, then JAXB needs to generate java classes from schema b into the package b.

I know that episodes and catalogs might help, but i can not prevent jaxb to create the java classes comming from schema a twice in project b.

here is my maven configuration:

project A:

<build>
<plugins>
  <plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <configuration>
      <schemaIncludes>
        <include>wsdl/WebServiceBaseRequestResponse.xsd</include>
      </schemaIncludes>
      <generatePackage>a</generatePackage>
      <episode>true</episode>
    </configuration>
  </plugin>

</plugins>
  </build>

Project B :

<!-- Used to pull XSD files from the JAR -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>unpack-xsd-files</id>
        <phase>initialize</phase>
        <goals>
          <goal>unpack</goal>
        </goals>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>a</groupId>
              <artifactId>a-xsd</artifactId>
              <version>${project.version}</version>
              <type>jar</type>
            </artifactItem>
          </artifactItems>
          <includes>wsdl/*.xsd</includes>
          <outputDirectory>${project.basedir}/target/xsd-includes</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <configuration>
      <extension>true</extension>

      <episodes>
        <episode>
          <groupId>a</groupId>
          <artifactId>sca-xsd</artifactId>
          <version>${project.version}</version>
        </episode>
      </episodes>

      <episode>false</episode>

      <schemaIncludes>
            <include>wsdl/b.xsd</include>
      </schemaIncludes>
      <generatePackage>b</generatePackage>
    </configuration>
  </plugin>

b.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1" targetNamespace="a"
  xmlns:tns="a" xmlns:com="b"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:import namespace="a" schemaLocation="..relPathTo/target/xsd-includes/a.xsd>

  <xs:element name="addShipmentOrderResult">
    <xs:annotation>
  <xs:documentation>
    Result object for addShipmentOrder.
  </xs:documentation>
    </xs:annotation>
    <xs:complexType>
  <xs:complexContent>
    <xs:extension base="com:baseResult">

    </xs:extension>
  </xs:complexContent>
</xs:complexType>
  </xs:element>
</xs:schema>
1

There are 1 answers

0
Xstian On

In this way you're not using the episodes, because you unpack the xsd within B project, so regenerates the classes from A project.

You should using the following configuration

    <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.8.1</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <args>
                <arg>-Xannotate</arg>
                <arg>-Xnamespace-prefix</arg>
                <arg>-nv</arg>
            </args>
            <extension>true</extension>
            <forceRegenerate>true</forceRegenerate>
            <schemas>
                <schema>
                    <fileset>
                        <directory>${basedir}/src/main/resources/schema/project/b</directory>
                        <includes>
                            <include>*.xsd</include>
                        </includes>
                    </fileset>
                </schema>
                <schema>
                    <dependencyResource>
                        <groupId>com.project.a</groupId>
                        <artifactId>artifact.a</artifactId>
                        <resource>schema/a.xsd</resource>
                    </dependencyResource>
                </schema>
            </schemas>
            <episodes>
                <episode>
                    <groupId>com.project.a</groupId>
                    <artifactId>artifact.a</artifactId>
                </episode>
            </episodes>
            <debug>true</debug>
            <verbose>true</verbose>
            <plugins>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.6.2</version>
                </plugin>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics-annotate</artifactId>
                    <version>0.6.2</version>
                </plugin>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-namespace-prefix</artifactId>
                    <version>1.1</version>
                </plugin>
            </plugins>
        </configuration>
    </plugin>

Remember to add as dependency the project A.