17 Micronaut CLI

The Micronaut CLI is the recommended way to create new Micronaut projects. The CLI includes commands for generating specific categories of projects, allowing you to choose between build tools, test frameworks, and even pick the language you wish to use in your application. The CLI also provides commands for generating artifacts such as controllers, client interfaces, and serverless functions.

When Micronaut is installed on your computer, you can call the CLI with the mn command.

  1. $ mn create-app my-app

A Micronaut CLI project can be identified by the micronaut-cli.yml file, which will be included at the root of the project (if it was generated via the CLI). This file will include the project’s profile, default package, and other variables. The project’s default package is evaluated based on the project name, for example:

  1. $ mn create-app my-demo-app

Will result in the following micronaut-cli-yml.

micronaut-cli.yml

  1. profile: service
  2. defaultPackage: my.demo.app
  3. ---
  4. testFramework: junit
  5. sourceLanguage: java

You can supply your own default package when creating the application by prefixing the application name with the package:

  1. $ mn create-app example.my-demo-app

Will result in the following micronaut-cli-yml.

micronaut-cli.yml

  1. profile: service
  2. defaultPackage: example
  3. ---
  4. testFramework: junit
  5. sourceLanguage: java
Throughout the user guide, references to CLI features/commands have been provided as applicable.

Definitions

Projects created with the CLI are based on one of several profiles, which consist of a project template (or skeleton), optional features, and profile-specific commands. Commands from a profile typically are specific to the profile application type; for example, the service profile (designed for creation of web service applications) provides the create-controller and create-client commands.

CLI commands typically accept at least one argument, such as the name of the project or controller to generate.

CLI commands can accept optional flags to control their behavior. Some flags accept multiple arguments, which are separated by commas.

Interactive Mode

If you run mn without any arguments, the Micronaut CLI will launch in interactive mode. This is a shell-like mode which allows you to run multiple CLI commands without re-initializing the CLI runtime, and is especially suitable when you are making use of code-generation commands (such as create-controller), creating multiple projects, or simply exploring the features included in the CLI. Tab-completion is enabled in the CLI, enabling you to hit the TAB key to see possible options for a given command or flag.

  1. $ mn
  2. | Starting interactive mode...
  3. | Enter a command name to run. Use TAB for completion:
  4. mn>

Help and Info

General usage information can be viewed using the help command.

  1. mn> help create-app
  2. Usage: mn create-app [-hinvVx] [-b=BUILD-TOOL] [-l=LANG] [-p=PROFILE] [-f=FEATURE[,FEATURE...]]...
  3. [NAME]
  4. Creates an application
  5. [NAME] The name of the application to create.
  6. -b, --build=BUILD-TOOL Which build tool to configure. Possible values: gradle, maven.
  7. -f, --features=FEATURE[,FEATURE...]
  8. The features to use
  9. -h, --help Show this help message and exit.
  10. -i, --inplace Create a service using the current directory
  11. -l, --lang=LANG Which language to use. Possible values: java, groovy, kotlin.
  12. ...

For details about a specific command, supply the command name after the help command.

  1. mn> help create-app
  2. | Command: create-app
  3. | Description:
  4. Creates an application
  5. | Usage:
  6. create-app [NAME]
  7. ...

A list of available profiles can be viewed using the list-profiles command.

  1. mn> list-profiles
  2. | Available Profiles
  3. --------------------
  4. base The base profile
  5. cli The cli profile
  6. federation The federation profile
  7. function The function profile
  8. function-aws The function profile for AWS Lambda
  9. kafka The Kafka messaging profile
  10. profile A profile for creating new Micronaut profiles
  11. service The service profile

To view details on a specific profile, use the profile-info command, followed by the profile name.

  1. mn> profile-info service
  2. | Profile: service
  3. --------------------
  4. The service profile
  5. | Provided Commands:
  6. --------------------
  7. create-bean Creates a singleton bean
  8. create-client Creates a client interface
  9. create-controller Creates a controller and associated test
  10. create-job Creates a job with scheduled method
  11. help Prints help information for a specific command
  12. | Provided Features:
  13. --------------------
  14. annotation-api Adds Java annotation API
  15. config-consul Adds support for Distributed Configuration with Consul (https://www.consul.io)
  16. discovery-consul Adds support for Service Discovery with Consul (https://www.consul.io)
  17. discovery-eureka Adds support for Service Discovery with Eureka
  18. groovy Creates a Groovy application
  19. ...