Code Generation
About
Code Generation plugins in Maven are used to automate the creation of source code during the build process. These plugins reduce manual coding effort by generating boilerplate code, stubs, client/server APIs, proxies, or model classes from various specifications (e.g., OpenAPI, WSDL, XSD, Protocol Buffers, etc.).
Automated code generation ensures:
Consistency between contract (specs) and implementation.
Productivity by eliminating repetitive tasks.
Synchronization of APIs across services and clients.
Common Scenarios for Code Generation
Generating Java classes from XML schemas (XSD).
Generating SOAP client stubs from WSDL files.
Generating REST clients or servers from OpenAPI/Swagger specs.
Generating gRPC or Protocol Buffers classes.
Creating JPA entities, DTOs, mappers, or boilerplate code.
JAXB2 Maven Plugin
Generates Java classes from XSD schemas using JAXB 2. It's useful in projects where XML configuration or communication is required.
Common Goals
xjc
Compile .xsd
into Java classes
Basic Configuration
Useful Config Options
schemaDirectory
Path to XSD files
outputDirectory
Directory to generate Java classes
clearOutputDir
Clean output before generation
packageName
Override package name
OpenAPI Generator Maven Plugin
Generates client/server code from OpenAPI (Swagger) definitions. Supports many languages and frameworks.
Common Goals
generate
Generates code from openapi.yaml
Basic Configuration
Popular Generator Types:
java
(Spring, JAX-RS)typescript
,python
,go
,kotlin
, etc.
wsimport (JAX-WS Maven Plugin)
Generates Java classes from WSDL for SOAP-based web service clients.
Basic Configuration
protobuf-maven-plugin
Used to compile .proto
files into Java or other languages using the Protocol Buffers compiler.
Basic Configuration
Generated Output
By default, .java
classes will be generated from .proto
files under:
mapstruct-processor (for Annotation Processing)
Used for annotation-based generation of Java bean mappers.
Basic Configuration
Additionally, enable annotation processing in your build tool (IDE or compiler).
Last updated
Was this helpful?