Warning: A non-numeric value encountered in /home/ricston2/public_html/blogarchive/wp-content/themes/Divi/functions.php on line 5766

When generating Java classes from an XSD using the XJC tool, we sometimes need more control over the generated classes, for example; to include particular fields or methods in some of the generated classes (of which there is no information in XSD). Unfortunately, when I tried creating an XJC plugin to achieve this kind of control, I couldn’t find a simple tutorial for this and instead ended up using valuable time figuring things out for myself. Hopefully, this post will help anyone with the same problem to get going a lot faster.

I will go through the process using the tools that I’m comfortable using – feel free to use whatever tools you prefer.

First of all, start by creating a simple maven project in Eclipse using the m2e Eclipse plugin (you can skip archetype selection), and put the following dependency in your pom:

That’s all you need in your pom.xml; your project coordinates and creates a single dependency in your dependencies (we’ll be using the coordinates to refer to this plugin in projects).

Next, create the directories META-INF/services inside your src/main/resources. Then inside the services directory, create the file com.sun.tools.xjc.Plugin and within it write the fully qualified name of the class implementing your XJC plugin (in this example it is: com.ricston.XJCPlugin).

Here is the implementation of the plugin:

All I’m doing here is adding the private id field to each generated class that is using this plugin. It’s type will be long and I’m adding a getter/setter for it too.

It’s important to take note of the string you return in the getOptionName() method as you’ll be using it in any project using this plugin.

Okay so that’s it for this simple plugin, so go ahead and install it in your local repo with mvn install.

To use this plugin in any of your projects simply include the following configuration in your pom.xml:

Note that the lifecycle-mapping plugin is there just to keep Eclipse happy. What we’re really interested in is the maven-jaxb2-plugin plugin. We set the schemaDirectory which is where our XSD will be, and the generatePackage to configure the package for the classes we’ll be generating. Notice that we are using the string returned by our getOptionName() as an argument (prefixed by a dash “-“). Finally, we need to specify the groupId, artifactId and version of the plugin we created.

That’s it – you can run mvn package on the project using our plugin and it will generate Java classes out of the XSD you provide in ${project.basedir}/target/generated-sources/xjc. To re-generate the classes, you can delete the ${project.basedir}/target/generated-sources/xjc and run mvn package on the project again.

Feel free to explore the possibilities in the public boolean run(Outline model, Options opt, ErrorHandler errorHandler) method in your custom plugin. If you find that using the JAXB API to create the customizations for your generated classes is too time consuming (I couldn’t find any documentation on this so you might end up having to stay looking into the code), you can always inject text into the generated classes using: classOutline.implClass.direct(injectedCode); where injectedCode is the string containing your custom code.

Of course this is just scratching the surface of what you can do in your custom plugins. Experiment and remember to share what you think may be useful to others 🙂