使用 xmlbeans、inst2xsd 和 Maven 从 XML 生成 XSD

Ing*_*her 6 java xml xsd maven

我有一个 XML 文件,我想从中生成 XSD 架构,使用xmlbeans,特别是inst2xsd。我想打包脚本,以便它可以通过 Maven 运行。在使用 Maven 安装 xmlbeans 时,我找不到任何有关如何运行 inst2xsd 的文档。这是我pom.xml到目前为止:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.wolkenarchitekt</groupId>
    <artifactId>xml-to-xsd</artifactId>
    <version>1</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

通过mvn install作品安装它。仅供参考 - 对于答案并不重要 - 我通过 Docker 构建它,所以我使用的是 OpenJDK14:

FROM maven:3.6.3-openjdk-14-slim
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace
COPY pom.xml .
RUN mvn install
Run Code Online (Sandbox Code Playgroud)

现在如何inst2xsd在通过 Maven 安装 xmlbeans 后运行可执行文件?

jcc*_*ero 6

您可以使用Exec Maven 插件来调用该类Inst2Xsd。这个类是从inst2xsdshell 脚本中实际调用的类。

如果您xmlbeans的项目中不需要- 一旦您的 XSD 生成 - 您可以仅为该任务定义此依赖项。

考虑以下 XML 文档:

<?xml version="1.0" encoding="UTF-8" ?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
    <calories>650</calories>
  </food>
  <food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>Light Belgian waffles covered with strawberries and whipped cream</description>
    <calories>900</calories>
  </food>
  <food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
    <calories>900</calories>
  </food>
  <food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>Thick slices made from our homemade sourdough bread</description>
    <calories>600</calories>
  </food>
  <food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
    <calories>950</calories>
  </food>
</breakfast_menu>
Run Code Online (Sandbox Code Playgroud)

在示例中,我们将命名它food-menu.xml并将其保存在src/main/resources.

您可以按如下方式生成 XML 模式(以下示例源自您可以在插件文档中找到的代码):

<project>
  <!-- ... -->
    <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <includeProjectDependencies>false</includeProjectDependencies>
          <includePluginDependencies>true</includePluginDependencies>
          <mainClass>org.apache.xmlbeans.impl.inst2xsd.Inst2Xsd</mainClass>
          <arguments>
            <!-- Add as many arguments as you need -->
            <argument>-outDir</argument>
            <argument>${project.build.outputDirectory}</argument>
            <argument>-validate</argument>
            <argument>${project.basedir}/src/main/resources/food-menu.xml</argument>
          </arguments>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.1.0</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  <!-- ... -->
</project>
Run Code Online (Sandbox Code Playgroud)

只需mvn exec:java从终端或命令行运行,就会根据传递给 的参数生成架构Inst2Xsd

使用docker应该没有问题。