我有一个XML文件,我想使用xmlbeans,特别是inst2xsd从中生成XSD架构。我想打包脚本,以便可以通过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>
通过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
现在,通过Maven安装xmlbeans之后,如何运行inst2xsd的可执行文件?

最佳答案

您可以使用Exec Maven Plugin来调用Inst2Xsd类。此类是从inst2xsd shell脚本实际调用的类。
如果您的项目中不需要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>
在示例中,我们将其命名为food-menu.xml并将其保存在src/main/resources中。
您可以按如下方式生成XML模式(以下示例是从可在插件documentation中找到的代码派生的):
<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>
只需从终端或命令行运行mvn exec:java,就会根据传递给Inst2Xsd的参数生成模式。
使用docker应该不是问题。

09-11 20:29