Joe Crivello
Joe Crivello

Reputation: 102

maven-jaxb2-plugin generates uncompilable code when using episode from org.jvnet.ogc/gml-v_3_2_1

I am trying to generate JAXB classes for the AIXM XSDs, which unfortunately are quite complicated and reference the even more complicated OGC GML 3.2.1, xlink and ISO 19139 schemas. I would like this code generation to occur during our Maven build, and I am using the maven-jaxb2-plugin for this purpose.

The OGC schema files require numerous patches in order to be compiled into JAXB classes by XJC. Luckily, the original developer of the maven-jaxb2-plugin coincidentally also created the ogc-schemas project which contains pre-built JAXB bindings and episode files for many OGC schema files, including GML 3.2.1. Unfortunately from the repository issues list, it sounds like he tragically passed away in 2021.

By using the episode file from org.jvnet.ogc:gml-v_3_2_1 together with some custom bindings for one of the AIXM schema files, I was finally able to get maven-jaxb2-plugin to generate code. However, the code it generates does not compile. I do not do much Java development, so I may be doing something obviously wrong here.

Many of the classes it generates fail to compile with an error like this:

aixm.ApronElementTimeSliceType is not abstract and does not override abstract method createNewInstance() in org.jvnet.jaxb2_commons.lang.CopyTo2

In this case, the abstract method is declared in net.opengis.gml.v_3_2_1.AbstractTimeSliceType and the inheritance tree looks like this:

aixm.ApronElementTimeSliceType (generated in my project)
 extends aixm.AbstractAIXMTimeSliceType (generated in my project)
  extends aixm.AbstractAIXMTimeSliceBaseType (generated in my project)
   extends net.opengis.gml.v_3_2_1.AbstractTimeSliceType (imported from org.jvnet.ogc:gml-v_3_2_1)

It is not clear to me how I can get maven-jaxb2-plugin to generate an implementation for createNewInstance() in each of these generated non-abstract classes. It looks like the needed implementation is pretty simple... pretty much new CLASS_NAME_HERE().

Here is my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId><!-- SNIP --></groupId>
    <artifactId><!-- SNIP --></artifactId>
    <version><!-- SNIP --></version>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.7</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.jvnet.ogc</groupId>
            <artifactId>gml-v_3_2_1</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.hisrc.w3c</groupId>
            <artifactId>xlink-v_1_0</artifactId>
            <version>1.4.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.15.2</version>
                <executions>
                    <execution>
                        <id>fixm</id>
                        <!-- SNIP... this execution is not relevant to this question... -->
                    </execution>
                    <execution>
                        <id>aixm</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <extension>true</extension>
                            <generatePackage>aixm</generatePackage>
                            <generateDirectory>${project.basedir}/src/main/generated-sources-aixm</generateDirectory>
                            <removeOldOutput>true</removeOldOutput>
                            <schemaDirectory>src/main/xsd/aixm</schemaDirectory>
                            <schemaIncludes>
                                <include>**/*.xsd</include>
                            </schemaIncludes>
                            <bindingDirectory>src/main/xjb/aixm</bindingDirectory>
                            <bindingIncludes>
                                <include>**/*.xjb</include>
                            </bindingIncludes>
                            <episodes>
                                <episode>
                                    <groupId>org.jvnet.ogc</groupId>
                                    <artifactId>gml-v_3_2_1</artifactId>
                                </episode>
                                <episode>
                                    <groupId>org.hisrc.w3c</groupId>
                                    <artifactId>xlink-v_1_0</artifactId>
                                </episode>
                            </episodes>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.glassfish.jaxb</groupId>
                        <artifactId>jaxb-runtime</artifactId>
                        <version>2.3.6</version>
                    </dependency>
                    <dependency>
                        <groupId>com.sun.xml.bind</groupId>
                        <artifactId>jaxb-core</artifactId>
                        <version>2.3.0.1</version>
                    </dependency>
                    <dependency>
                        <groupId>javax.activation</groupId>
                        <artifactId>activation</artifactId>
                        <version>1.1.1</version>
                    </dependency>
                    <dependency>
                        <groupId>javax.persistence</groupId>
                        <artifactId>javax.persistence-api</artifactId>
                        <version>2.2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

And just for the sake of completeness here is the bindings file I created to resolve some ObjectFactory collisions in AIXM_Features.xsd:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.1" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxb:bindings schemaLocation="../../xsd/aixm/AIXM_Features.xsd">
        <jaxb:bindings node="/xs:schema/xs:element[@name='Curve']">
            <jaxb:factoryMethod name="CurveAixm"/>
        </jaxb:bindings>
        <jaxb:bindings node="/xs:schema/xs:element[@name='Point']">
            <jaxb:factoryMethod name="PointAixm"/>
        </jaxb:bindings>
        <jaxb:bindings node="/xs:schema/xs:element[@name='Surface']">
            <jaxb:factoryMethod name="SurfaceAixm"/>
        </jaxb:bindings>
    </jaxb:bindings>
    <jaxb:bindings schemaLocation="http://schemas.opengis.net/iso/19139/20070417/gco/gcoBase.xsd">
        <jaxb:bindings node="/xs:schema/xs:element[@name='AbstractObject']">
            <jaxb:factoryMethod name="AbstractObjectIso19139"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

Upvotes: 0

Views: 235

Answers (1)

Joe Crivello
Joe Crivello

Reputation: 102

I was able to resolve this by adding org.jvnet.jaxb2_commons:jaxb2-basics plugin into the maven-jaxb2-plugin configuration. Only version 0.11.0 seemed to work.

This is what I added to the pom.xml (only showing changed XML elements):

<project>
    <build>
        <plugins>
            <plugin>
                <executions>
                    <execution>
                        <configuration>
                            <args>
                                <arg>-Xcopyable</arg>
                            </args>
                            <plugins>
                                <plugin>
                                    <groupId>org.jvnet.jaxb2_commons</groupId>
                                    <artifactId>jaxb2-basics</artifactId>
                                    <version>0.11.0</version>
                                </plugin>
                            </plugins>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Upvotes: 0

Related Questions