How to use jaxb_commons plugins from maven

10,361

Solution 1

Unfortunately, it looks like the interface injection plugin is no longer well supported. In fact, I'm having trouble finding the JAR for download.

Thankfully, the JAXB2 Basics Plugins provides a similar mechanism for adding an interface to the generated JAXB stubs (see the Inheritance plugin).

The JAXB2 Basics plugin is available in the java.net Maven repository.

Using the Inheritance plugin, your POM will look like:

<build>
  <plugins>
    <plugin>
      <groupId>org.jvnet.jaxb2.maven2</groupId>
      <artifactId>maven-jaxb2-plugin</artifactId>
      <version>0.6.2</version>
      <executions>
        <execution>
          <goals>
            <goal>generate</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <extension>true</extension>
        <args>
          <arg>-Xinheritance</arg>
        </args>
        <plugins>
          <plugin>
            <groupId>org.jvnet.jaxb2_commons</groupId>
               <artifactId>jaxb2-basics</artifactId>
               <version>0.5.3</version>
           </plugin>
        </plugins>
      </configuration>
    </plugin>
    ...
  </plugins>
  ...
</build>

The Inheritance plugin documentation has an example for what your JAXB Bindings would look like. For your convenience, I've reproduced the example below:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    jaxb:version="2.1"
    jaxb:extensionBindingPrefixes="inheritance">

    <!-- ... -->

    <xs:complexType name="WillBeMadeCloneableType">
        <xs:annotation>
            <xs:appinfo>
                <inheritance:implements>java.lang.Cloneable</inheritance:implements>
            </xs:appinfo>
        </xs:annotation>
        <!-- ... -->
    </xs:complexType>
    <!-- ... -->
</xs:schema>

Solution 2

I'm really not sure this is the "right" way to solve this but, this is what I did. First, download the Interface Insertion Plugin xjc-if-ins.jar from https://jaxb2-commons.dev.java.net/interface-insertion/xjc-if-ins.jar (couldn't find a jar containing IfInsertPluginImpl.class in the java.net maven repository).

Then, install the jar in the local repository:

mvn install:install-file -DgroupId=org.jvnet.jaxb2_commons \
                         -DartifactId=xjc-if-ins \
                         -Dversion=1.0-SNAPSHOT \
                         -Dpackaging=jar \
                         -Dfile=xjc-if-ins.jar

Finally, add the jar as a dependency of the maven-jaxb2-plugin in the plugin section:

<build>
  <plugins>
    <plugin>
      <groupId>org.jvnet.jaxb2.maven2</groupId>
      <artifactId>maven-jaxb2-plugin</artifactId>
      <version>0.6.2</version>
      <executions>
        <execution>
          <goals>
            <goal>generate</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <extension>true</extension>
        <args>
          <arg>-Xifins</arg>
        </args>
        <plugins>
          <plugin>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>basic</artifactId>
            <version>0.4.1.5</version>
          </plugin>
        </plugins>
      </configuration>
      <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>xjc-if-ins</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
      </dependencies>
    </plugin>
    ...
  </plugins>
  ...
</build>

As I said, this is maybe not the cleanest way to configure the jaxb2 plugin to use the Interface Insertion Plugin but, with this setup, the generate goal doesn't complain about the -Xifins extension.

Share:
10,361
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to use a jaxb plugin to insert a interface into a choice element generating the classes from maven. The problem is that I can't seem to figure out how to do so from maven, the repository isn't clear from the documentation and the only example (bellow) doesn't work, it seems to ignore the plugin (maven reports no error about not finding it) or the plugin doesn't have all the adds-ons currently listed in the project documentation:

    <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.6.1</version>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <generatePackage>br.com.wonder.nfe.xml</generatePackage>
            <args>
                <arg>-Xifins</arg>
            </args>
            <plugins>
                <plugin>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>basic</artifactId>
                    <version>0.4.1.5</version>
                </plugin>
            </plugins>
        </configuration>
    </plugin>
    

    I have these in the root pom:

    <pluginRepositories>
        <pluginRepository>
            <id>maven2-repository.dev.java.net</id>
            <url>http://download.java.net/maven/2</url>
        </pluginRepository>
        <pluginRepository>
            <id>maven-repository.dev.java.net</id>
            <name>Java.net Maven 1 Repository (legacy)</name>
            <url>http://download.java.net/maven/1</url>
            <layout>legacy</layout>
        </pluginRepository>
    </pluginRepositories>
    

    Running that gives:

    Error while setting CmdLine options '[-Xifins, -episode, /home/administrador/JavaApp/wnfe3/wnfe-ejb/target/generated-sources/xjc/META-INF/sun-jaxb.episode]'!

    Embedded error: unrecognized parameter -Xifins