liveBook · Manning

Code generation

As explained in Quarkus in Action

Generated code is source code produced automatically by a software tool from project settings, templates, or other inputs.

Code generation produces source code and project resources automatically from application settings, extensions, templates, or schema definitions. In Quarkus, creating an application with sample generation can produce REST endpoints, configuration, Dockerfiles, and tests alongside the project structure. section 2.2.2

Generated artifacts commonly support several development tasks:

  • Create a sample JAX-RS resource such as a GET /hello endpoint from the selected REST extension. section 2.2.2
  • Provide configuration, Dockerfiles for packaging types, and Quarkus test sources in the application layout. section 2.2.2
  • Compile a Protocol Buffers schema into Java message classes, builders, and serialization methods. section 4.6.2
  • Generate gRPC client and server stubs from service definitions, allowing implementations and callers to use typed Java classes. section 4.7

Generating gRPC code from a service contract

Place inventory.proto under src/main/proto and define messages plus InventoryService methods. Add the quarkus-grpc extension, then run ./mvnw clean package. The extension detects the schema and generates Java sources under target/generated-sources/grpc, including message classes, builders, service bases, and clients. section 4.7

Listing 2.4 GreetingResource class: generated sample JAX-RS endpoint
package org.acme;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")                       #1
public class GreetingResource {

    @GET                                  #2
    @Produces(MediaType.TEXT_PLAIN)       #3
    public String hello() {
        return "Hello from Quarkus REST";    #4
    }
}

Inspect the generated GreetingResource to see how annotations define the /hello path, HTTP method, response type, and returned content. section 2.2.2

Listing 4.36 The files generated from the proto file definition
target/generated-sources/grpc
└── org
    └── acme
        └── inventory
            └── model
                ├── CarResponse.java
                ├── CarResponseOrBuilder.java
                ├── InsertCarRequest.java
                ├── InsertCarRequestOrBuilder.java
                ├── InventoryProtos.java
                ├── InventoryServiceBean.java
                ├── InventoryServiceClient.java
                ├── InventoryServiceGrpc.java
                ├── InventoryService.java
                ├── MutinyInventoryServiceGrpc.java
                ├── RemoveCarRequest.java
                └── RemoveCarRequestOrBuilder.java

Inspect the generated gRPC source tree to distinguish message classes, builders, service implementations, and client stubs. section 4.7

How Quarkus in Action covers it

How deep it goes
Explains generated artifacts and triggering commands, but stops short of detailing generator internals.
Examples
Uses a generated GreetingResource, Quarkus tests, a Car protobuf model, and InventoryService gRPC stubs.
What it assumes
Assumes basic Quarkus and Java project structure while defining schemas and generated API artifacts incrementally.
What you can do after
Build generated REST, protobuf, and gRPC artifacts using Quarkus extensions and Maven compilation.
How it connects
Connects REST extensions, configuration, Docker packaging, testing, Protocol Buffers, serialization, and gRPC services.

Also explained in

Compare how 17 books explain code generation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quarkus in Action. The section links point to the parts of the book it draws on.