新規でMicronautプロジェクト作成する

Micronautとは

  • JVM言語向けのフレームワーク。
  • ビルド時にDIを行う + GraalVMネイティブなので起動が早いらしい。
  • AWS lambdaにAPI作りたかったので、起動の速さを重視して採用。

インストール手順(macOS)

  • javaが必要だけど、macにはデフォルトでjavaが入っていない。homebrewでjava入れるところからやっていく。

javaインストール

$ brew install openjdk 
$ sudo ln -sfn 
$(brew --prefix)/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk 
$ java -version 
openjdk version "25.0.2" 2026-01-20 OpenJDK Runtime Environment Homebrew (build 25.0.2) OpenJDK 64-Bit Server VM Homebrew (build 25.0.2, mixed mode, sharing)

micronaut CLIインストール

  • これもbrewにある。mnコマンドで使用する。
$ brew install micronaut 
$ mn --version 
Micronaut Version: 4.10.7

プロジェクト作成

  • mnコマンドで入れる。
  • ※注意点
    • create-appに続くプロジェクト名にはディレクトリ構造を入れない( ❌services/api)
    • jdkはマシンのjava —versionに合わせておく。
$ mkdir services
$ mn create-app api \
  --lang=kotlin \
  --build=gradle_kotlin \
  --jdk=25 \
  --features=serialization-jackson,validation

./gradlew runで起動(エラーになる)

$ ./gradlew run
Kotlin does not yet support 25 JDK target, falling back to Kotlin JVM_21 JVM target
Kotlin does not yet support 25 JDK target, falling back to Kotlin JVM_21 JVM target

> Task :kspKotlin
Kotlin does not yet support 25 JDK target, falling back to Kotlin JVM_21 JVM target

> Task :kspKotlin FAILED

[Incubating] Problems report is available at: file:///Users/wakabayashitoshiyuu/develop/study/reading-insight-log/frontend/services/api/build/reports/problems/problems-report.html

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':kspKotlin'.
> Inconsistent JVM-target compatibility detected for tasks 'compileJava' (25) and 'kspKotlin' (21).
  
  Consider using JVM Toolchain: https://kotl.in/gradle/jvm/toolchain
  Learn more about JVM-target validation: https://kotl.in/gradle/jvm/target-validation 

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to generate a Build Scan (Powered by Develocity).
> Get more help at https://help.gradle.org.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 10.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/9.1.0/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 2m 17s
2 actionable tasks: 2 executed

javaのバージョンを下げてやりなおし

  • kotlinのバージョンを上げて他の場所でエラー出てもいやなので、エラーメッセージに従いjavaのバージョンを21に変更する

java21入れる

$ brew install openjdk@21
$ sudo ln -sfn $(brew --prefix)/opt/openjdk@21/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
$ java --version
openjdk 21.0.10 2026-01-20
OpenJDK Runtime Environment Homebrew (build 21.0.10)
OpenJDK 64-Bit Server VM Homebrew (build 21.0.10, mixed mode, sharing)

jdk=21でmicronautプロジェクトを作り直す

$ mn create-app reading-insight-api \
  --lang=kotlin \
  --build=gradle_kotlin \
  --jdk=21 \
  --features=serialization-jackson,validation

起動確認

  • 成功
./gradlew run
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details

> Task :run
 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
01:03:33.333 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 226ms. Server Running: http://localhost:8080
<===========--> 87% EXECUTING [12m 47s]
> IDLE
> IDLE
> :run
> IDLE
> IDLE
> IDLE
> IDLE
> IDLE

これで準備が整ったので、API実装を進めていく。