Micronaut application in heroku

Irfan Ali Shaik
4 min readFeb 6, 2022

We are going to build a simple micronaut application using gradle and deploy in heroku.

Micronaut is a modern, jvm-based, full-stack framework for building modular, easily testable microservice and serverless applications.

Gradle is an open-source build automation tool that is designed to be flexible enough to build almost any type of software.

Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.

Create a new micronaut application

Lets create an application using micronaut launch

Micronaut Launch is a web application that allows you to create Micronaut projects through an interface instead of using the console CLI.

Micronaut Launch
Application type : micronaut application, version: latest ( 3.3.0) when writing this postLanguage: java, version: 17Build tool: gradle ( 7.3.1) when writing this post.test framework: junitselect features you would like
we are going with basic features: lombok, logback
update name and base package: micronaut-heroku-demo and com.micronaut.demo

generate project .

1. push to your github account
2. download locally as zip
3. use micronaut cli commands to create locally

we are going with option 2 .

Unzip the archive ( mac,linux)
unzip micronaut-heroku-demo.zip -d micronaut-heroku-demo
cd micronaut-heroku-demoBuild:./gradlew clean build
Launch:./gradlew run

./gradlew clean build
BUILD SUCCESSFUL in 28s
./gradlew run
Startup completed in 342ms. Server Running: http://localhost:8080

Now lets create our our first controller, create a new package : resource

Create a new class DemoResource which is a controller

Add a get method which takes name n returns string

package com.micronaut.demo.resource;


import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotNull;

@Controller
@Slf4j
public class DemoResource {

@Get
public String getGreetings(@QueryValue @NotNull String name){

log.info("sending greetings to {}",name);

return "Hello %s from demo application".formatted(name);
}
}

Once the class is added , let’s build and run the application and test our endpoint

curl -- location -- request GET 'http://localhost:8080/?name=irfan'
Hello irfan from demo application

Now our simple micronaut application in ready and working as expected

Deploy the application to heroku

If you don’t have a Heroku account yet you can create your free account by going to the Heroku signup page .

Now lets make our application ready for deployment in heroku

Java Version

By default heroku use jdk 8 .

If you are using any other version , need to create a file system.properties

and have property a property defining java version to be used

java.runtime.version=17.0.2

check https://devcenter.heroku.com/articles/java-support#supported-java-versions

build.gradle

By default heroku runs a gradle task “stage” which is clean build and need a manifest file

jar {
manifest {
attributes(
'Main-Class': 'com.micronaut.demo.Application'
)
}
}


task stage(dependsOn: ['build', 'clean'])

build.mustRunAfter clean

application.yml

micronaut:
application:
name: micronautHerokuDemo

server:
port: ${PORT:8080}
cors:
enabled: true

Heroku needs a file known a Procfile to run the application. Create the file and make an entry.

web: java $JAVA_OPTS -Dmicronaut.environments=heroku -Dserver.port=$PORT -jar build/libs/micronaut-heroku-demo-0.1-all.jar

Heroku runs the application on a random port. So pass the value in the proc file. $PORT is auto replaced heroku during deploy.

Now our application is ready to be deployed to Heroku.

Deploy the application to heroku

We will communicate to heroku using command line. We have have to install the Heroku cli. The installation guide for the Heroku cli https://devcenter.heroku.com/articles/heroku-cli

brew tap heroku/brew && brew install heroku

Once installed, verify installation

heroku --version
heroku/7.59.2 darwin-x64 node-v12.21.0

With the Heroku cli you can login to Heroku with the following command.

heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/*************
Logging in... done
Logged in as *****@****.com

Now lets create our application in heroku

heroku apps:create micronaut-heroku-demo
Creating ⬢ micronaut-heroku-demo... done
https://micronaut-heroku-demo.herokuapp.com/ | https://git.heroku.com/micronaut-heroku-demo.git
git init

heroku git:remote -a micronaut-heroku-demo

git add .

git commit -am "make it better"

git push heroku master

We are using heroku git here. But we can connect to github.

Once pushed, the application is deployed

.....
remote: BUILD SUCCESSFUL in 48s
remote: 15 actionable tasks: 15 executed
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 137.7M
remote: -----> Launching...
remote: Released v3
remote: https://micronaut-heroku-demo.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.

Now let check our application logs to verify if its running

heroku logs --app micronaut-heroku-demo --tail[main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [cloud, heroku]
app[web.1]: 16:45:22.854 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 3305ms. Server Running: http://localhost:56410
heroku[web.1]: State changed from starting to up
Build succeeded

Now lets try our endpoint

curl --location --request GET 'https://micronaut-heroku-demo.herokuapp.com/?name=irfan'
Hello irfan from demo application

There we have our micronaut application deployed in heroku.

In the next post lets add postgres database as an add-on to the demo project.

Happy Learning.

--

--