We are trying to avoid duplicate code in a project where Java and Python are used together. The majority of the code base is in Java, with Python now being added due to the prevalence in the machine learning environment.
In a green-field scenario, we'd start with sth. like swagger or protobuf and derive the models from the generated code. But this doesn't work now.
The J classes are annotated with some annotations and they are targeting Java 8.
While researching, I found the following possible route to turn the structure (without the methods) of the classes into Python class structures:
- Generate XML schemas from the Java classes
- Generate Python classes from the xml schema files
The added benefit: The two languages actually communicate via XML in our project so the schema files are helpful for other use cases. We're using maven to build Java, therefore it would be nice to include it in the maven process.
I included this in the pom.xml:
<!-- https://mvnrepository.com/artifact/org.codehaus.mojo/jaxb2-maven-plugin -->
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3.1</version>
</dependency>
as well as the default plugin configuration
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<id>schemagen</id>
<goals>
<goal>schemagen</goal>
</goals>
</execution>
</executions>
<!--
Use default configuration, implying that sources are read
from the directory src/main/java below the project basedir.
(i.e. getProject().getCompileSourceRoots() in Maven-speak).
-->
</plugin>
But I get an error
[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:schemagen (default-cli) on project common: JAXB errors arose while SchemaGen compiled sources to XML. -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:schemagen (default-cli) on project common: JAXB errors arose while SchemaGen compiled sources to XML.
I then looked into JSON Schemas as an intermediary but that also doesn't really cut it because it's not easily possible to create Python class source code from JSON schemas.
So is there any way to generate simple "Pojo" Python classes from Java code? No methods, no complex cross-compile but a simple structural conversion. I can generate UML diagrams from the Java files in IntelliJ so all the information is there, I just need a tool that helps converting