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?
With the desired XML document opened in the active editor tab, choose Tools | XML Actions | Generate XSD Schema from XML File on the main menu. The Generate Schema From Instance Document dialog box opens. and select the desired file in the dialog that opens.
XMLBeans is a tool that allows access to the full power of XML in a Java friendly way. The idea is to take advantage of the richness and features of XML and XML Schema and have these features mapped as naturally as possible to the equivalent Java language and typing constructs.
xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With