英文:
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:
<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>
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文档:
<?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模式(以下示例源自插件文档中的代码):
<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>
<!-- 添加尽可能多的参数 -->
<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不应该是一个问题。
英文:
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:
<?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>
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):
<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>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论