【问题标题】:Generate Java Classes from XSD with default package name and XSD namespace使用默认包名和 XSD 命名空间从 XSD 生成 Java 类
【发布时间】:2018-09-01 05:12:09
【问题描述】:

我有 2 套 XSD,一套用于入站操作,另一套用于出站操作。两个 XSD 集具有相似的命名空间,但由于来自不同的来源,因此需要在同一代码集中单独维护。每个 XSD 集都有深度嵌套的类并生成大约 650 个类。 我正在使用 Maven JAXB 插件来生成 Java 类。

如果我指定<outputDirectory>,文件生成会因为出站和入站操作之间的命名空间冲突而失败。如果我指定<packageName>,所有类都在同一个包中生成,这会导致入站操作中的命名空间冲突。

有没有办法让 Maven 遵循 XSD 命名空间提供的包名,并在包名前加上“入站”或“出站”来分隔生成的包名。例如,由 XSD 创建的包是

com.example.operation
com.example.operation.v1

可以修改成

inbound.com.example.operation
inbound.com.example.operation.v1

我的 Maven pom.xml 文件

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<groupId>com.example</groupId>
<modelVersion>4.0.0</modelVersion>

<artifactId>sample-integration-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sample-integration-model</name>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>2.4</version>
    </dependency>
</dependencies>
<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>1.8</source>
                <target>1.8</target>
                <showWarnings>true</showWarnings>
            </configuration>
        </plugin>



        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>xjc-outbound</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>

                        <laxSchemaValidation>true</laxSchemaValidation>
                        <outputDirectory>target/jaxb2/outbound</outputDirectory>
                        <sources>
                            <source>src/main/xsd/OutboundXsd/RequestMessagesDictionary</source>
                            <source>src/main/xsd/OutboundXsd/ResponseMessagesDictionary</source>
                        </sources>
                    </configuration>
                </execution>

                <execution>
                    <id>xjc-inbound</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                    <configuration>

                        <laxSchemaValidation>true</laxSchemaValidation>
                        <outputDirectory>target/jaxb2/inbound</outputDirectory>
                        <sources>
                            <source>src/main/xsd/InboundXsd/RequestMessagesDictionary</source>
                            <source>src/main/xsd/InboundXsd/ResponseMessagesDictionary</source>
                        </sources>
                        <clearOutputDir>false</clearOutputDir>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>    

【问题讨论】:

    标签: jaxb jaxb2-maven-plugin


    【解决方案1】:

    您可以告诉 Maven 在给定的包中设置生成的类。
    execution 标签内使用&lt;generatePackage&gt;desired.package&lt;/generatePackage&gt;

    这是有据可查的here

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 2014-08-10
      • 2020-10-02
      • 1970-01-01
      • 2010-12-23
      • 2012-01-08
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      相关资源
      最近更新 更多