javamavenxmlbeansxmlbeans-maven-plugin

How to Use Custom Extensions with the XMLBeans maven Plugin?


I'm trying to use a custom extension with the Apache XMLBeans plugin, but I'm encountering errors during the generation process. Here is the custom extension class:

package com.chepseskaf.tools;

import org.apache.xmlbeans.SchemaTypeSystem;
import org.apache.xmlbeans.impl.tool.SchemaCompilerExtension;

import java.util.Map;

public class MyExtension implements SchemaCompilerExtension {

    @Override
    public void schemaCompilerExtension(SchemaTypeSystem schemaTypeSystem, Map map) {
        // Implement me
    }

    @Override
    public String getExtensionName() {
        return this.getClass().getSimpleName();
    }
}

And here is my plugin configuration in pom.xml:

<plugin>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>5.3.0</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <repackage>com.chepseskaf.driver.metadata</repackage>
        <name>model</name>
        <partialMethods>ALL</partialMethods>
        <sourceDir>${project.basedir}/xsd</sourceDir>
        <extensions>
            <extension>com.chepseskaf.tools.MyExtension</extension>
        </extensions>
        <verbose>true</verbose>
        <quiet>false</quiet>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>3.0.0-beta3</version>
        </dependency>
    </dependencies>
</plugin>

However, the generation crashes with the following error:

[ERROR] Failed to execute goal org.apache.xmlbeans:xmlbeans:5.3.0:compile (default) on project model-driver: Unable to parse configuration of mojo org.apache.xmlbeans:xmlbeans:5.3.0:compile for parameter className: Cannot set 'className' in class org.apache.xmlbeans.impl.tool.Extension: InvocationTargetException: com.chepseskaf.tools.MyExtension -> [Help 1]

Caused by: java.lang.ClassNotFoundException: com.chepseskaf.tools.MyExtension
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass (SelfFirstStrategy.java:42)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass (ClassRealm.java:225)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass (ClassRealm.java:210)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass (ClassRealm.java:205)

It seems like the plugin is unable to find the com.chepseskaf.tools.MyExtension class. How can I properly configure and use my custom extension with the XMLBeans plugin?

Any help would be greatly appreciated.


Solution

  • I managed to get it working (at least in 2 to 3 out of 5 builds), and that's exactly where the problem lies. Unfortunately, this plugin is very unstable and seems to be severely affected by timing issues. Therefore, I would suggest seriously considering another solution for your specific problem. To get it working (at least in about 2 to 3 out of 5 builds), you can try the following:

    <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">
      
      ...
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>5.3.0</version>
            <executions>
              <execution>
                <goals>
                  <goal>compile</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <repackage>com.chepseskaf.driver.metadata</repackage>
              <name>model</name>
              <partialMethods>ALL</partialMethods>
              <sourceDir>${project.basedir}/xsd</sourceDir>
              <extensions>
                <extension>
                  <className>com.chepseskaf.tools.MyExtension</className> <!-- added the className here -->
                </extension>
              </extensions>
              <verbose>true</verbose>
              <quiet>false</quiet>
            </configuration>
            <dependencies>
              <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>3.0.0-beta3</version>
              </dependency>
              <dependency>
                <groupId>com.chepseskaf</groupId>          <!-- please adjust the dependency in the following lines (where your MyExtension.class is located) -->
                <artifactId>your-artifact-id</artifactId>
                <version>your-version</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </build>
    
      <dependencies>
        <dependency>
          <groupId>org.apache.xmlbeans</groupId>
          <artifactId>xmlbeans</artifactId>
          <version>5.3.0</version>
        </dependency>
      </dependencies>
    
    </project>
    

    However, keep in mind that it will be highly unstable if it works. According to this problem, the project is currently not very viable (see the first comment). So I would really suggest looking for alternatives depending on the concrete use case.