Revisiting the jaxb-visitor

I created the jaxb-visitor plugin about 10 years ago for a project working with XML schemas from the Open Geospatial Consortium and recently updated it to work with Java 11 and JAXB 3. duke, the Java mascot, shown on a badge with a large V and labeled jaxb-visitor. The years 2012-2022 are shown over the label 10 years. There is a barcode beneath the V.

The plugin generates interfaces and classes as well as decorates the XJC generated Java beans in order to implement the Visitor pattern.

Update Area Rationale
Use Apache Maven Invoker Plugin for the tests. tests The existing tests provided good coverage for one plugin using their JUnit based test framework, but didn’t support other plugins.
Change source to Java 8 code quality Java 7 and earlier are no longer supported. Use of streams API offers better organization.
Support Java 8 and 11 at build time and runtime code generation Java 8 and Java 11 are both Long Term Support (LTS) versions of Java.
JAXB 3 support code generation JAXB version 3.0 is the latest version of the spec available.
Maintain JAXB 2 support code generation Lots of legacy JAXB projects out there.

Migrating Unit Tests to Maven Invoker

The Apache Maven Invoker Plugin was the first change to the project. It required reorganizing a lot of files, but it was worth it to be able to have coverage in place before making any changes.

Pros:

  • well documented and supported strategy for testing a maven plugin.
  • each test case is an isolated maven project
  • easy to test other plugins

Cons:

  • longer build
  • more files

Example test directory

src/it
├── basic
│  ├── expected           // dir with .java files to use as expected values
│  ├── pom.xml            
│  ├── src                
│  │  └── main
│  │    └── resources     // schemas for the test are here
│  └── verify.groovy      // asserts the expected files match the actual files

The structure provided by the Maven Invoker plugin keeps your test cases isolated which is what allows for easier testing with multiple configurations.

The output for each test is found under target/it. The build.log file has the console output for the test’s build.

Project Layout

The code was always organized by the specific file it generated or named according to the decoration it performed. One change for this update was to organize the code into packages by function.

src/main/java
└── com
  └── massfords
  └── jaxb              // contains the VisitorPlugin
    └── codegen         // options and utils for code models
    ├── creators        
    │   ├── classes     // creates implementations of the generated interfaces
    │   ├── decorators  // decorates an existing bean
    │   └── interfaces  // creates new interfaces for the Visitor pattern
    └── states          // models state in the generation pipeline

Conclusion

The new test layout and better code organization will make it much easier to incorporate tests and pull requests. The initial benefit of having a JUnit test framework for a maven plugin faded quickly after needing to expand test coverage.

The latest version is published to Maven Central. I’ll expand the test coverage with new plugins as issues arise.

<dependency>
  <groupId>com.massfords</groupId>
  <artifactId>jaxb-visitor</artifactId>
  <version>3.0.0</version>
</dependency>
 Date: July 16, 2022
 Tags: 

Previous
⏪ Validating Path Expressions In Step Functions