liveBook · Manning

QuarkusMain annotation

As explained in Quarkus in Action

The QuarkusMain annotation identifies the main entry point of a Quarkus command-mode application.

The @QuarkusMain annotation identifies the entry point for a Quarkus command-mode application. Apply it to a class implementing QuarkusApplication, then implement run(String... args) to handle command-line invocation. section 4.10 section 4.8.2

A practical command-line application combines the annotation with:

  • A class annotated with @QuarkusMain. section 4.8.2
  • The QuarkusApplication interface and its run method, which receives command-line arguments. section 4.8.2
  • Application logic such as a gRPC client, with asynchronous calls awaited before CLI invocation returns. section 4.8.2

Inventory command

An inventory CLI can annotate InventoryCommand with @QuarkusMain, implement QuarkusApplication, and invoke the configured inventory gRPC service. After packaging with ./mvnw clean package, run java -jar target/quarkus-app/quarkus-run.jar add KNIGHT Pontiac TransAM to insert a car. section 4.8.2

Listing 4.43 Consuming the gRPC service from the command line
package org.acme.inventory.client;

import io.quarkus.grpc.GrpcClient;

import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.QuarkusMain;
import org.acme.inventory.model.InsertCarRequest;
import org.acme.inventory.model.InventoryService;
import org.acme.inventory.model.RemoveCarRequest;

@QuarkusMain                                           #1
public class InventoryCommand
    implements QuarkusApplication {            #2

    private static final String USAGE =
        "Usage: inventory <add>|<remove> " +
            "<license plate number> <manufacturer> <model>";

    @GrpcClient("inventory")
    InventoryService inventory;

    @Override
    public int run(String... args) {        #3
        String action =
            args.length > 0 ? args[0] : null;
        if ("add".equals(action) && args.length >= 4) {
            add(args[1], args[2], args[3]);
            return 0;
        } else if ("remove".equals(action) && args.length >= 2) {
            remove(args[1]);
            return 0;
        }

        System.err.println(USAGE);
        return 1;
    }
    public void add(String licensePlateNumber, String manufacturer,
                    String model) {
        inventory.add(InsertCarRequest.newBuilder()
            .setLicensePlateNumber(licensePlateNumber)
            .setManufacturer(manufacturer)
            .setModel(model)
            .build())
            .onItem().invoke(carResponse ->
                System.out.println("Inserted new car " + carResponse))
            .await().indefinitely();
    }

    public void remove(String licensePlateNumber) {
        inventory.remove(RemoveCarRequest.newBuilder()
            .setLicensePlateNumber(licensePlateNumber)
            .build())
            .onItem().invoke(carResponse ->
                System.out.println("Removed car " + carResponse))
            .await().indefinitely();
    }
}

View the complete InventoryCommand listing, including the annotation, interface implementation, gRPC client, and argument handling. section 4.8.2

How Quarkus in Action covers it

How deep it goes
Shows annotation, interface, run method, packaging, and invocation, but defers broader CLI design.
Examples
Uses an inventory CLI that adds a car through a configured gRPC service.
What it assumes
Assumes familiarity with Quarkus services and gRPC clients while showing the required entry-point structure.
What you can do after
Build a Quarkus command-line application that invokes services and runs as a packaged executable.
How it connects
Connects QuarkusApplication, command-line applications, gRPC clients, and asynchronous computations.

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.