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

huangapple go评论60阅读模式
英文:

Generate XSD from XML using xmlbeans, inst2xsd and Maven

问题

我有一个XML文件,我想使用xmlbeans生成一个XSD模式,具体使用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>

通过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可执行文件呢?

英文:

I have an XML file I want to generate an XSD schema from, using xmlbeans, specifically inst2xsd. I'd like to package the script so it can be run via Maven.
I could not find any documentation how to run inst2xsd when installing xmlbeans using Maven.
This is my pom.xml so far:

&lt;project&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;de.wolkenarchitekt&lt;/groupId&gt;
    &lt;artifactId&gt;xml-to-xsd&lt;/artifactId&gt;
    &lt;version&gt;1&lt;/version&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.xmlbeans&lt;/groupId&gt;
            &lt;artifactId&gt;xmlbeans&lt;/artifactId&gt;
            &lt;version&gt;3.1.0&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

Installing this via mvn install works. Just for the reference - not important for the answer - I'm building it via Docker, so I'm using OpenJDK14:

FROM maven:3.6.3-openjdk-14-slim
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace
COPY pom.xml .
RUN mvn install

Now how do I run the executable for inst2xsd after installing xmlbeans via Maven?

答案1

得分: 6

你可以使用Exec Maven Plugin来调用类Inst2Xsd。这个类实际上是从inst2xsd shell 脚本中调用的。

如果你的项目中不需要xmlbeans,一旦生成了你的XSD,你甚至可以只为这个任务定义这个依赖项。

考虑以下XML文档:

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

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

你可以按照以下方式生成XML模式(以下示例源自插件文档中的代码):

&lt;project&gt;
  &lt;!-- ... --&gt;
  	&lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
        &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.0.0&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;goals&gt;
              &lt;goal&gt;java&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
        &lt;configuration&gt;
          &lt;includeProjectDependencies&gt;false&lt;/includeProjectDependencies&gt;
          &lt;includePluginDependencies&gt;true&lt;/includePluginDependencies&gt;
          &lt;mainClass&gt;org.apache.xmlbeans.impl.inst2xsd.Inst2Xsd&lt;/mainClass&gt;
          &lt;arguments&gt;
            &lt;!-- 添加尽可能多的参数 --&gt;
            &lt;argument&gt;-outDir&lt;/argument&gt;
            &lt;argument&gt;${project.build.outputDirectory}&lt;/argument&gt;
            &lt;argument&gt;-validate&lt;/argument&gt;
            &lt;argument&gt;${project.basedir}/src/main/resources/food-menu.xml&lt;/argument&gt;
          &lt;/arguments&gt;
        &lt;/configuration&gt;
        &lt;dependencies&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.apache.xmlbeans&lt;/groupId&gt;
            &lt;artifactId&gt;xmlbeans&lt;/artifactId&gt;
            &lt;version&gt;3.1.0&lt;/version&gt;
          &lt;/dependency&gt;
        &lt;/dependencies&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
  &lt;!-- ... --&gt;
&lt;/project&gt;

只需从终端或命令行运行mvn exec:java,根据传递给Inst2Xsd的参数生成模式。

使用Docker不应该是一个问题。

英文:

You can use the Exec Maven Plugin to invoke the class Inst2Xsd. This class is the one actually called from the inst2xsd shell script.

If you do not need xmlbeans in your project - once your XSD is generated - you can event define this dependency only for that task.

Consider the following XML document:

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

In the example we will name it food-menu.xml and save it in src/main/resources.

You can generate the XML schema as follows (the following example is derived from the code that you can find in the plugin documentation):

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

Just run mvn exec:java from your terminal or command line and the schema will be generated according to the arguments passed to Inst2Xsd.

The use of docker should not be a problem.

huangapple
  • 本文由 发表于 2020年7月29日 03:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63141810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定