Generate Plain Old Java Objects (POJOs) from XML Schema Definitions with Gradle
My blog post Generate Plain Old Java Objects (POJOs) from XML Schema Definitions with xjc
is pretty popular, and I've enjoyed getting use out of it myself. But the problem with this is that it introduces a number of source files into your repo, which you then need to maintain in terms of your own code style standards, maybe manage Javadoc, and generally add a lot of code to your codebase that isn't yours.
One thing we can do instead is utilise Gradle XJC Plugin to automagically generate these files each time we do a build.
To make this work, we need to do two things. Firstly, we need to put the XML Schema Definition (XSD) in our project under the src/main/xsd
directory. The plugin will iterate over all XSDs it can find, generating them as necessary.
Next we need to add the Gradle configuration to hook in the plugin:
plugins {
id 'org.unbroken-dome.xjc' version '2.0.0'
}
xjc {
srcDirName = 'xsd'
}
It's that simple! You can now reference the classes from your regular code, but you may need to compile the project first if the classes aren't showing up in your IDE.