Skip to content

Commit

Permalink
Use Quarkus 3.10.0 to implement the REST app
Browse files Browse the repository at this point in the history
  • Loading branch information
miguno committed May 2, 2024
1 parent 6113958 commit c49bedf
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 193 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ WORKDIR $APP_DIR
COPY --from=builder /minimal-jre $JAVA_HOME

# Copy packaged app from builder image.
COPY --from=builder --chown=$USER_NAME:$GROUP_NAME /usr/src/myapp/target/app.jar .
COPY --from=builder --chown=$USER_NAME:$GROUP_NAME /usr/src/myapp/target/app-runner.jar ./app.jar

# Run the application.
USER $USER_NAME:$GROUP_NAME
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

A template project to create a Docker image for a Java application.
The example application uses Spring Boot to expose an HTTP endpoint.
The example application uses Quarkus to expose an HTTP endpoint.

> **Golang developer?** Check out https://github.com/miguno/golang-docker-build-tutorial
Expand Down Expand Up @@ -71,8 +71,8 @@ $ docker run -p 8123:8123 miguno/java-docker-build-tutorial:latest
running container.

```shell
$ curl http://localhost:8123/greeting
{"id":1,"name":"Hello, World!"}
$ curl http://localhost:8123/status
{"status":"idle"}
```

# Usage with just
Expand Down Expand Up @@ -111,10 +111,10 @@ on Windows).
$ ./mvnw clean package

# Run the application locally.
$ java -jar target/app.jar
$ java -jar target/app-runner.jar

# Alternatively, you can run the application via Maven.
$ ./mvnw spring-boot:run
$ ./mvnw quarkus:dev
```

# References
Expand Down
21 changes: 17 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ system-info:
@echo "os: {{os()}}"
@echo "os family: {{os_family()}}"

# build the native application locally (requires GraalVM)
build-native:
@./mvnw install -Dnative

# run the application locally (in Quarkus development mode) with hot reload
dev:
@./mvnw quarkus:dev

# create a docker image (requires Docker)
docker-image-create:
@echo "Creating a docker image ..."
Expand All @@ -31,10 +39,15 @@ docker-image-run:
@echo "Running container from docker image ..."
@./start_container.sh

# package the application to create an uber jar
package:
@./mvnw package

# run the application locally.
run: package
@java -jar target/app-runner.jar

# send request to the app's HTTP endpoint (requires running container)
send-request-to-app:
curl http://localhost:8123/greeting
curl http://localhost:8123/status

# Run the application locally
run:
@./mvnw spring-boot:run
151 changes: 78 additions & 73 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.miguno</groupId>
<artifactId>java-docker-build</artifactId>
<packaging>jar</packaging>
Expand All @@ -19,30 +14,35 @@
<licenses>
<license>
<name>Apache License 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
<url>https://www.apache.org/licenses/LICENSE-2.0.html</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<compiler-plugin.version>3.13.0</compiler-plugin.version>
<java.version>17</java.version>
<jersey.version>3.1.6</jersey.version>
<junit.version>5.10.2</junit.version>
<jakarta.activation-api.version>2.1.3</jakarta.activation-api.version>
<jaxb-api.version>2.3.1</jaxb-api.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.release>${java.version}</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<quarkus.analytics.disabled>true</quarkus.analytics.disabled>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>3.10.0</quarkus.platform.version>
<surefire-plugin.version>3.2.5</surefire-plugin.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
Expand All @@ -54,73 +54,48 @@
</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jackson</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<exclusions>
<exclusion>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>${jakarta.activation-api.version}</version>
</dependency>

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb-api.version}</version>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>

<!--
For `mvn package`: Hardcode the output jar's filename to `app.jar`,
rather than something like `java-docker-build-1.0.0-SNAPSHOT.jar`.
For `mvn package`: Set the name of the packaged (uber / fat) jar.
Normally, you would set this to "app.jar". But with Quarkus, do not add
the ".jar" filename suffix. If fileName is "app", Quarkus will by default
use the output filename "app-runner.jar". For Quarkus, you must also
set `quarkus.package.jar.type=uber-jar` in
`src/main/resources/application.properties`.
https://quarkus.io/guides/maven-tooling#uber-jar-maven
-->
<finalName>app</finalName>

<plugins>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<version>${compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
Expand All @@ -130,7 +105,13 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -140,33 +121,57 @@
</dependencies>
</plugin>

<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<finalName>app</finalName>
</configuration>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
<goal>build</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.miguno.javadockerbuild.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
-->

</plugins>
</build>

<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
13 changes: 0 additions & 13 deletions src/main/java/com/miguno/javadockerbuild/App.java

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/java/com/miguno/javadockerbuild/Greeting.java

This file was deleted.

24 changes: 0 additions & 24 deletions src/main/java/com/miguno/javadockerbuild/GreetingController.java

This file was deleted.

32 changes: 32 additions & 0 deletions src/main/java/com/miguno/javadockerbuild/LoggingFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.miguno.javadockerbuild;

import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.ext.Provider;

import org.jboss.logging.Logger;

import io.vertx.core.http.HttpServerRequest;

@Provider
public class LoggingFilter implements ContainerRequestFilter {

private static final Logger LOG = Logger.getLogger(LoggingFilter.class);

@Context
UriInfo info;

@Context
HttpServerRequest request;

@Override
public void filter(ContainerRequestContext context) {
// Uncomment to enable HTTP request logging.
//final String method = context.getMethod();
//final String path = info.getPath();
//final String address = request.remoteAddress().toString();
//LOG.infof("Request %s %s from IP %s", method, path, address);
}
}
Loading

0 comments on commit c49bedf

Please sign in to comment.