Micronaut?
Road to Micronaut 1.0-JVM-based full stack framework
Recently, I feel like I've been hearing the names here and there, so let's give it a try.
The usage environment this time is here.
$ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
Creating an application seems to start with the mn
command (CLI).
Download from the Download Page or install with SDKMAN.
This time, let's install using SDKMAN.
$ sdk install micronaut
This time, Micronaut 1.0.4 is installed.
$ mn -V
| Micronaut Version: 1.0.4
| JVM Version: 1.8.0_191
Now, let's create an application template using the mn
command.
Gradle seems to be used for the project created by mn create-app
, but Apache Maven is personally good, so specify --build maven
to use Maven.
$ mn create-app hello-world --build maven
This will create the hello-world
directory.
$ cd hello-world
Let's list the generated files.
$ tree
.
├── Dockerfile
├── micronaut-cli.yml
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── hello
│ │ └── world
│ │ └── Application.java
│ └── resources
│ ├── application.yml
│ └── logback.xml
└── test
└── java
└── hello
└── world
10 directories, 8 files
I have a project using Maven Wrapper.
You can even create a Dockerfile
.
Looking at the generated pom.xml
, the main class, maven-shade-plugin
and ʻexec-maven-plugin` are set, and it seems that it can be used as it is.
Click here for a class that has a main method.
src/main/java/hello/world/Application.java
package hello.world;
import io.micronaut.runtime.Micronaut;
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class);
}
}
Build and packaging.
$ ./mvnw package
Now that the JAR file has been created, let's start it.
$ java -jar target/hello-world-0.1.jar
12:56:59.043 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 1108ms. Server Running: http://localhost:8080
Start up in 1 second!
Try accessing with curl
.
$ curl localhost:8080
{"_links":{"self":{"href":"/","templated":false}},"message":"Page Not Found"}
Not Found, but a response was returned.
Now, let's learn from the document and create a Controller.
src/main/java/hello/world/HelloController.java
package hello.world;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;
@Controller("/hello")
public class HelloController {
@Get(produces = MediaType.TEXT_PLAIN)
public String index() {
return "Hello World";
}
}
Build and start.
$ ./mvnw package
$ java -jar target/hello-world-0.1.jar
Verification.
$ curl localhost:8080/hello
Hello World
It worked.
When developing with IntelliJ IDEA, it seems that Annotation Processors need to be enabled.
The atmosphere is like this.
It seems to adjust with --features
at the time of mvn create-app
.
$ mn create-app hello-world-groovy --build maven --features=groovy
$ mn create-app hello-world-kotlin --build maven --features=kotlin
I'll put it on.
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hello.world</groupId>
<artifactId>hello-world</artifactId>
<version>0.1</version>
<properties>
<micronaut.version>1.0.4</micronaut.version>
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<exec.mainClass>hello.world.Application</exec.mainClass>
</properties>
<repositories>
<repository>
<id>jcenter.bintray.com</id>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-bom</artifactId>
<version>${micronaut.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${exec.mainClass}</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-noverify</argument>
<argument>-XX:TieredStopAtLevel=1</argument>
<argument>-classpath</argument>
<classpath/>
<argument>${exec.mainClass}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>${micronaut.version}</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>${micronaut.version}</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Dockerfile
FROM openjdk:8u171-alpine3.7
RUN apk --no-cache add curl
COPY target/hello-world*.jar hello-world.jar
CMD java ${JAVA_OPTS} -jar hello-world.jar
micronaut-cli.yml
profile: service
defaultPackage: hello.world
---
testFramework: junit
sourceLanguage: java
src/main/resources/application.yml
micronaut:
application:
name: hello-world
src/main/resources/logback.xml
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Recommended Posts