JAXB 2 Maven插件 - 使用剧集 - xsd的一般项目

时间:2013-11-29 16:16:33

标签: xsd maven-jaxb2-plugin

我希望项目A包含一些常用的XML模式文件和一些依赖于生成的类(从jaxb生成)的java类,使用命名空间a和包a,我希望项目B依赖于项目甲

项目B中的

我希望使用项目A中的某些XML类型的XML模式文件,项目b具有名称空间b,然后JAXB需要从模式b生成java类到包b中。

我知道剧集和目录可能有所帮助,但是我无法阻止jaxb在项目b中创建两次从模式中提交的java类。

这是我的maven配置:

项目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>

项目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 个答案:

答案 0 :(得分:0)

这样你就不会使用剧集了,因为你在B项目中解压缩xsd,所以从A项目中重新生成类。

您应该使用以下配置

    <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>

请记住将项目A添加为依赖项。