Getting Started with Micronaut

Setup project

CLI

  • brew update
  • brew install micronaut
  • Post successful installed.
  • Verify Micronaut installed successfully, type mn in terminal
  • Known Issue with Mac
  • To fix the issue, use the command mentioned below.
sudo chmod +a "${USER} allow list,add_file,search,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity" /usr/local/{bin,lib}\
  • Navigate to privacy and settings and allow mn.

Micronaut Launch

Another way to launch Micronaut Project is using Micronaut Launch (link).

  • Home page
  • Choose the java version, for this example we have selected java 17, you can choose test framework, build tool (maven). Post that you can either download or push it to github
  • Click Generate Project and it will provide to download or publish to git.

Intellij

We can also create Micronaut project using intellij ultimate edition.

Below are the steps to create Micronaut project.

  • File->Open->Project→Left hand side select Micronaut
  • Choose path, choose build as Maven/Gradle, chose all library and click create project
  • Micronaut4.0.X works only with Java 17 and above
  • Ensure to have new settings.xml or update existing settings.xml for micronaut project. Below is sample micronaut-settings.xml which can places under .m2 folder.
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  
  <profiles>
    <profile>
      <id>micronaut-app-profile</id>
	  <repositories>
	      <repository>
	        <id>central</id>
	        <url>https://repo.maven.apache.org/maven2</url>
	      </repository>
	    </repositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>micronaut-app-profile</activeProfile>
  </activeProfiles>
</settings>
  • Use jEnv to choose different JDK home. Refer jEnv blog for same
  • Java Version with Gradle distribution
  • Follow the process to install JDK with GraalVM distribution. GraalVM MAC OS
  • Quick Video below to show the entire process

Running and Building Project

  • Open Maven settings in Intellij
  •  Update and override settings xml, download setting settings xml from the doc and place in .m2 folder
  • Run the below command to build the project

mvn -s  /Users/alokmazumdar/.m2/micronaut-settings.xml -gs /Users/alokmazumdar/.m2/micronaut-settings.xml  clean install -X // path to your micronaut-settings.xml

    • MYSQL Config (optional)
    micronaut:
      application:
        name: app-hello-world
      router:
        static-resources:
          swagger:
            paths: classpath:META-INF/swagger
            mapping: /swagger/**
          swagger-ui:
            paths: classpath:META-INF/swagger/views/swagger-ui
            mapping: /swagger-ui/**
    datasources:
      default:
        url: jdbc:mysql://localhost:3306/reminderapp
        username: sample_user
        password: 'sample_user_XXX'
        dialect: MYSQL
        driverClassName: com.mysql.cj.jdbc.Driver
    • Build and run the project
    • Running the application use command ./mvnw mn:run (for maven)
    • Application is up and running in port 8080

    Leave a Reply