Sometimes one needs to derive a Google Protocol Buffers schema from an XML schema .. e.g. from an Enterprise Architect Export.
Tool used here: schema2proto (GitLab project)
Steps
-
Download
schema2proto-lib
from the global Maven repository: https://search.maven.org/search?q=schema2proto -
Run
Schema2Proto
against a give XSD schema file and with a given output directory:
java -jar schema2proto-lib-1.53.jar Schema2Proto --outputDirectory=src/main/protobuf input.xsd
A yaml config file may be given instead of cli parameters:
java -jar schema2proto-lib-1.53.jar Schema2Proto ----configFile=config.yaml input.xsd
A sample config can be found here
Using as a Maven plugin is also an option and worked for me better:
<plugin>
<groupId>no.entur</groupId>
<artifactId>schema2proto-maven-plugin</artifactId>
<version>1.53</version>
<configuration>
<configFile>config.yaml</configFile>
<xsdFile>schema.xsd</xsdFile>
</configuration>
<executions>
<execution>
<id>generate-resources</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
Alternative
Not maintained and pretty old: https://github.com/tranchis/xsd2thrift
As the download link is dead, we need to clone the sources and build them by ourselves:
-
Edit the
pom.xml
and change the source/target level to 1.7 (when using an environment where source level 6 is not supported any more) -
Build with
maven package
.. that’s it
Now we may generate our protobuf schemata like this:
java xsd2thrift-1.0.jar --protobuf --output=out.proto filename.xsd