This is simple Spring Boot REST application which can be built to native Java
, nothing but a native
executable produced using Spring ahead-of-time (AOT) compilation.
A native image can run standalone without relying on a JVM.
The following are the dependencies chosen from
Project : Maven 3.9.6
Languge : Java 21
Spring Boot : 3.2.1
Packaging : Jar
Dependencies : Lombok, GraalVM Native Support, Spring Web, Testcontainers, Docker Compose, JPA
For further reference, please consider the following sections:
- Official Apache Maven documentation
- Spring Boot Maven Plugin Reference Guide
- Create an OCI image
- GraalVM Native Image Support
- Spring Web
The following guides illustrate how to use some features concretely:
- Building a RESTful Web Service
- Serving Web Content with Spring MVC
- Building REST services with Spring
These additional references should also help you:
NOTE: GraalVM 22.3+ is required.
Make sure that your
JAVA_HOME points
to GraalVM JDK distribution.e.g. Using SDKMAN
$ sdk install java 21-graal $ sdk use java 21-graal $ java -version java version "21" 2023-09-19 Java(TM) SE Runtime Environment Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15) Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15, mixed mode, sharing) $
This project has been configured to let you generate either a lightweight container or a native executable. It is also possible to run your tests in a native image.
If you're already familiar with Spring Boot container images support, this is the easiest way to get started. Docker should be installed and configured on your machine prior to creating the image.
To create the image, run the following goal:
$ ./mvnw spring-boot:build-image -Pnative
Then, you can run the app like any other container:
$ docker run --rm -p 8080:8080 boot-graalvm:0.0.1-SNAPSHOT
Use this option if you want to explore more options such as running your tests in a native image.
The GraalVM native-image
compiler should be installed and configured on your machine.
To create the executable, run the following goal:
$ ./mvnw native:compile -Pnative
Then, you can run the app as follows for local profile:
$ target/boot-graalvm -Dspring.profiles.active=local
Once the app is running, run the following curl
command from another terminal window:
curl get localhost:8080
You can also run your existing tests suite in a native image. This is an efficient way to validate the compatibility of your application.
To run your existing tests in a native image, run the following goal:
$ ./mvnw test -PnativeTest
With added Spock support (spock-core and spock-spring), test cases can be written as JUnit 5 or Spock specifications in both Java and Groovy.
Maven Suerfire and Failsafe plugins are configured to run both Java JUnit 5 Tests and Groovy Spock Specifications.
-DskipTests
skips all tests
-DskipUnitTests
skips all unit tests but not integration tests
-DskipIntegrationTests
skips all integration tests, but not unit tests
# Run all unit tests
./mvnw test
# Run specific unit test
./mvnw test -Dtest=HelloControllerTest
#TODO Run specific unit tet method
# Run all integration tests, skip unit tests
./mvnw integration-test -DskipUnitTests
# Run specific integration test, skip unit tests
./mvnw integration-test -Dit.test=AccountServiceIT -DskipUnitTests
# Run specific integration test, and unit tests
./mvnw integration-test -Dit.test=HelloControllerIS
#TODO Run specific integration tet method
Run one of the following and open index.html
in browser from <project-home>/target/site/index.html
for JaCoCo Code Coverage Reports.:
$ ./mvnw clen install
$ ./mvnw clean test
$ ./mvnw native:compile -Pnative
Once the app is up and running, just go to http://localhost:8080/swagger-ui/index.html in the browser to test end-points.