Skip to main content

Getting started with Micronaut using Gradle

Getting started

In this tutorial, we will create a Micronaut application using Gradle.

Github Link

What you will need

  • Java installed on your machine
  • Gradle installed on your machine
  • Text Editor

Steps

First create your project folder, we will make a folder called "micronaut-project" for this tutorial.

The second step is to add the build.gradle file inside the project folder.

This will contain a script to build the Micronaut application.

Now update the build.gradle file.


1. Add following plugin
plugins {
id("com.github.johnrengelman.shadow") version "7.1.1"
id("io.micronaut.application") version "3.2.0"

}

This will help to build application executable jars.
     
2. Next, add dependencies.

dependencies {
annotationProcessor("io.micronaut:micronaut-http-validation")
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-jackson-databind")
implementation("io.micronaut:micronaut-runtime")
implementation("jakarta.annotation:jakarta.annotation-api")
runtimeOnly("ch.qos.logback:logback-classic")
implementation("io.micronaut:micronaut-validation")
implementation("io.micronaut:micronaut-http-server-netty")
}


3. To download and install these dependencies, you need to configure the repository.

repositories {
mavenCentral()
}

4. Include a micronaut task to configure the application's runtime and Micronaut annotations.

micronaut {
runtime("netty")

processing {
incremental(true)
annotations("com.tutorial.project.*")
}
}

NOTE: com.tutorial.project is the project package name. Replace it with your project package name.


5. 
To enable the Micornaut application starter, add the following configuration:

application {
mainClass.set("com.tutorial.project.Application")
}

NOTE: com.tutorial.project is the project package name. Replace it with your project package name. 

6. Now create Application class and add following code

package com.tutorial.project;

import io.micronaut.runtime.Micronaut;

public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}

NOTE: location of this file should be in micronaut-project/src/main/java/com/tutorial/project

 
7. 
The last step is to create a gradle.properties file and add the following configuration.


micronautVersion=3.3.0


 8. Now run the following command to build the application and create executable jars.

gradle build

It will create application jars in build/lib folder.

Use the following command to launch the application

java -jar build/libs/micronaut-project-all.jar 


Github Link








Comments

Subscribe for latest tutorial updates

* indicates required

Popular posts from this blog

AJAX - How To Retrieve JSON Response from PHP script

Hello Friends, This is my first tutorial post. Today I'll explain you how to retrieve JSON response via Ajax call. First we build a PHP script to send response in JSON format, then we will gather this response from an Ajax call through jQuery script. Let’s first create a PHP script  Create a PHP script, I’ll name it as “json_response.php” And add following code in it json_response.php <?php //set header for JSON response header("Content-Type: application/json"); //render array in JSON format echo json_encode(array('status' => 200, 'message' => 'Response from PHP script in JSON format.')); This is a basic example to send JSON response. PHP script will send response with status code and message. Now create a HTML file to fetch JSON response and add it in header tag. Create a HTML file, I’ll name it as “json_response.html” And add following code. json_response.html <html>           ...

JSON Tutorials : Getting Started

JSON is widely accepted text formatted structured data. JSON stands for " JavaScript Object Notation ". In general JSON can represent 1. Object of database record. 2. Object to represent a list of HTML elements. 3. Result of search query. 4. Response of an Ajax call. Here you can see JSON is used in many different areas and for many different scenarios. This means it has simple data structure. most of programming languages adopt it and it can flow easily from one connection to another. You can find JSON office definition here JSON Official Site . JSON is represented by two structural types, which includes two primitive types. Structural types Array : A sequential list of primitive data types between square brackets [ ] Object : Collection of key, value pair stored inside curly braces { }, where value would be primitive data type Primitive types : There are two primitive types key and value. " key " should be string and " value (data type)...

Twig Tutorials: Install and Configure

In this tutorial, we'll look at how to install Twig within your PHP project and thereafter configure it so that we can create and use a Twig template inside our PHP web application. Later on, we'll see a simple Twig template example that displays a "Welcome to Twig template" message. Let's take it one step at a time. Install The Twig template can be installed using Composer, Git, or PEAR. In this tutorial, we will use Composer to install Twig. To do so, we'll need to make a "composer.json" file. { "require" : { "twig/twig" : "1.*" } } From the console, run the following command . $ php composer.phar install This command will install Twig library. Configuration To use the Twig template, we should first configure it. To achieve this, we will write an index.php file which will configure the Twig autoloader and generate the Twig environment. We will render the Twig temp...