LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub

Code generation with maven build process

Besides using the java command line to generate microservice API based on the OpenAPI 3.0 specification, light-codegen also can be integrated with the project maven build process.

Sometimes the specification could be changed by different team members frequently. In this case, it could be challenging to re-generate the API project from the Java command line for every specification change.

To serve this purpose a new way is introduced here which could integrate the light-codegen into the maven build process. You can find the reference example at petstore maven codegen example.

Work Steps

  • Initiate the microservice API project. Define one module for the service and another module for specification

  • Create or copy the specification and the config JSON file to the specification module, for example, /petstore-spec/config

  • In the specification module, add maven plugin to run the light-codegen pom example in the petstore project

             <plugin>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>exec-maven-plugin</artifactId>
                 <version>1.4.0</version>
                 <executions>
                     <execution>
                         <id>light4j-codegen</id>
                         <phase>process-resources</phase>
                         <goals>
                             <goal>exec</goal>
                         </goals>
                         <inherited>false</inherited>
                         <configuration>
                             <executable>java</executable>
                             <arguments>
                                 <argument>-jar</argument>
                                 <argument>${settings.localRepository}/com/networknt/codegen-cli/${version.light-4j}/codegen-cli-${version.light-4j}.jar</argument>
                                 <argument>-f</argument>
                                 <argument>openapi</argument>
                                 <argument>-o</argument>
                                 <argument>../petstore-service</argument>
                                 <argument>-m</argument>
                                 <argument>config/openapi.yaml</argument>
                                 <argument>-c</argument>
                                 <argument>config/config.json</argument>
                             </arguments>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>
  • At the first time to run the build process to generate an initial service module, set config value “specChangeCodeReGenOnly” as false from config JSON file:

    {
      "name": "petstore",
      "version": "3.0.1",
      "groupId": "com.networknt",
      "artifactId": "petstore",
      "rootPackage": "com.networknt.petstore",
      "handlerPackage":"com.networknt.petstore.handler",
      "modelPackage":"com.networknt.petstore.model",
      "overwriteHandler": true,
      "overwriteHandlerTest": true,
      "overwriteModel": true,
      "httpPort": 8080,
      "enableHttp": false,
      "httpsPort": 8443,
      "enableHttps": true,
      "enableHttp2": true,
      "enableRegistry": false,
      "supportDb": false,
      "supportH2ForTest": false,
      "supportClient": false,
      "specChangeCodeReGenOnly": false,
      "dockerOrganization": "networknt"
    }
    
  • Run maven install to build the project, and the maven build will trigger the light-codegen to generate the initial service into the service module

       cd ~/networknt/light-example-4j/rest/perstore-with-codegen
    
       mvn clean install
    
    
  • After the service project generated, developers start to work on the service; then change config value “specChangeCodeReGenOnly” to true:

      "specChangeCodeReGenOnly": true,
    
    
  • The light-codegen will be triggered every time maven install be executed; but since the “specChangeCodeReGenOnly” set as true, only specification change related code will be generated to the service project.

    For example, data model, endpoint, handler.yml…

Light-example-4j has servicemesher example servicemesher example which build with maven build process. The servicemesher example includes tow parts:

– server side service which includes multiple services in project module

– [client side service][] which has one service in the project module

  • About Light
    • Overview
    • Testimonials
    • What is Light
    • Features
    • Principles
    • Benefits
    • Roadmap
    • Community
    • Articles
    • Videos
    • License
    • Why Light Platform
  • Getting Started
    • Get Started Overview
    • Environment
    • Light Codegen Tool
    • Light Rest 4j
    • Light Tram 4j
    • Light Graphql 4j
    • Light Hybrid 4j
    • Light Eventuate 4j
    • Light Oauth2
    • Light Portal Service
    • Light Proxy Server
    • Light Router Server
    • Light Config Server
    • Light Saga 4j
    • Light Session 4j
    • Webserver
    • Websocket
    • Spring Boot Servlet
  • Architecture
    • Architecture Overview
    • API Category
    • API Gateway
    • Architecture Patterns
    • CQRS
    • Eco System
    • Event Sourcing
    • Fail Fast vs Fail Slow
    • Integration Patterns
    • JavaEE declining
    • Key Distribution
    • Microservices Architecture
    • Microservices Monitoring
    • Microservices Security
    • Microservices Traceability
    • Modular Monolith
    • Platform Ecosystem
    • Plugin Architecture
    • Scalability and Performance
    • Serverless
    • Service Collaboration
    • Service Mesh
    • SOA
    • Spring is bloated
    • Stages of API Adoption
    • Transaction Management
    • Microservices Cross-cutting Concerns Options
    • Service Mesh Plus
    • Service Discovery
  • Design
    • Design Overview
    • Design First vs Code First
    • Desgin Pattern
    • Service Evolution
    • Consumer Contract and Consumer Driven Contract
    • Handling Partial Failure
    • Idempotency
    • Server Life Cycle
    • Environment Segregation
    • Database
    • Decomposition Patterns
    • Http2
    • Test Driven
    • Multi-Tenancy
    • Why check token expiration
    • WebServices to Microservices
  • Cross-Cutting Concerns
    • Concerns Overview
  • API Styles
    • Light-4j for absolute performance
    • Style Overview
    • Distributed session on IMDG
    • Hybrid Serverless Modularized Monolithic
    • Kafka - Event Sourcing and CQRS
    • REST - Representational state transfer
    • Web Server with Light
    • Websocket with Light
    • Spring Boot Integration
    • Single Page Application
    • GraphQL - A query language for your API
    • Light IBM MQ
    • Light AWS Lambda
    • Chaos Monkey
  • Infrastructure Services
    • Service Overview
    • Light Proxy
    • Light Mesh
    • Light Router
    • Light Portal
    • Messaging Infrastructure
    • Centralized Logging
    • COVID-19
    • Light OAuth2
    • Metrics and Alerts
    • Config Server
    • Tokenization
    • Light Controller
  • Tool Chain
    • Tool Chain Overview
  • Utility Library
  • Service Consumer
    • Service Consumer
  • Development
    • Development Overview
  • Deployment
    • Deployment Overview
    • Frontend Backend
    • Linux Service
    • Windows Service
    • Install Eventuate on Windows
    • Secure API
    • Client vs light-router
    • Memory Limit
    • Deploy to Kubernetes
  • Benchmark
    • Benchmark Overview
  • Tutorial
    • Tutorial Overview
  • Troubleshooting
    • Troubleshoot
  • FAQ
    • FAQ Overview
  • Milestones
  • Contribute
    • Contribute to Light
    • Development
    • Documentation
    • Example
    • Tutorial
“Code generation with maven build process” was last updated: July 5, 2021: fixes #275 checked and corrected grammar/spelling for majority of pages (#276) (b3bbb7b)
Improve this page
  • News
  • Docs
  • Community
  • Reddit
  • GitHub
  • About Light
  • Getting Started
  • Architecture
  • Design
  • Cross-Cutting Concerns
  • API Styles
  • Infrastructure Services
  • Tool Chain
  • Utility Library
  • Service Consumer
  • Development
  • Deployment
  • Benchmark
  • Tutorial
  • Troubleshooting
  • FAQ
  • Milestones
  • Contribute