Skip to main content

Migrate to Micronaut 4 from Micronaut 3

In this guide, we'll look at using OpenRewrite to perform an automated migration from Micronaut 3.x to Micronaut 4.x

If you want to learn more about the process that went into developing these recipes, check out the Micronaut framework 4.0 release blog post.

Configuration

This recipe has no required configuration options. It can be activated by adding a dependency on `org.openrewrite.recipe:rewrite-micronaut` in your build file or by running a shell command (in which case no build changes are needed):

  1. Add the following to your build.gradle file:
    build.gradle
    plugins {
    id("org.openrewrite.rewrite") version("latest.release")
    }

    rewrite {
    activeRecipe("org.openrewrite.java.micronaut.Micronaut3to4Migration")
    }

    repositories {
    mavenCentral()
    }

    dependencies {
    rewrite("org.openrewrite.recipe:rewrite-micronaut:2.33.0")
    }
  2. Run gradle rewriteRun to run the recipe.

For the full list of changes this recipe will make, see the recipe reference page.

Once you've configured your project, you're ready to execute the migration by running mvn rewrite:run or gradlew rewriteRun. After running the migration you can inspect the results with git diff (or equivalent), manually fix anything that wasn't able to be migrated automatically, and commit the results.

Before and After

Many javax packages have been updated to jakarta

These include:

  • javax.annotation.*
  • javax.persistence.*
  • javax.validation.*
import javax.annotation.PostConstruct;

import javax.persistence.GenerationType;

import javax.validation.constraints.NotBlank;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

// ...

Interface parameter updates

An HttpRequest type parameter has been added to some interfaces.

import io.micronaut.http.HttpRequest;
import io.micronaut.security.authentication.AuthenticationProvider;
import io.micronaut.security.authentication.AuthenticationRequest;
import io.micronaut.security.authentication.AuthenticationResponse;
import org.reactivestreams.Publisher;

import java.io.Serializable;

public class AuthProvPass implements AuthenticationProvider, Serializable {

@Override
public Publisher<AuthenticationResponse> authenticate(HttpRequest<?> httpRequest,
AuthenticationRequest<?, ?> authenticationRequest) {
return null;
}
}

Various dependencies may be added or updated

These include:

  • Micronaut Retry
  • Micronaut Session
  • Micronaut Websocket
  • SnakeYaml

Before

build.gradle
plugins {
id("io.micronaut.application") version "%s"
}

repositories {
mavenCentral()
}

dependencies {
implementation("io.micronaut:micronaut-session")
}

After

plugins {
id("io.micronaut.application") version "%s"
}

repositories {
mavenCentral()
}

dependencies {
implementation "io.micronaut:micronaut-retry"
implementation "io.micronaut:micronaut-websocket"

implementation("io.micronaut.session:micronaut-session")

runtimeOnly "org.yaml:snakeyaml"
}

withJansi configuration tag has been removed from logback.xml

<configuration>  
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<withJansi>true</withJansi>
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

See how this recipe works across multiple open-source repositories

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.