Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
///////////////////////////////////////////////////////////////////////////////
Copyright (c) 2021, 2023 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
///////////////////////////////////////////////////////////////////////////////
ifndef::rootdir[:rootdir: {docdir}/../..]
:guidesdir: {rootdir}/{flavor-lc}/guides
== Introduction
JDK 9 introduced the link:{jdk-doc-url}/docs/specs/man/jlink.html[`jlink`]
command that supports assembling a set of modules and their dependencies into a custom
runtime image. The `helidon-maven-plugin` has support for easily creating a custom runtime image for your
Helidon application resulting in a smaller, better performing runtime.
In this guide you will learn how to build a custom runtime image locally on your machine,
as well as how to build it in a Docker image.
== What You Need
For this 10 minute tutorial, you will need the following:
include::{rootdir}/includes/prerequisites.adoc[tag=prerequisites]
== Verify JDK
As noted in the prerequisites above, JDK 21 or newer is required.
[source,bash]
----
$JAVA_HOME/bin/java --version
----
Creating a custom runtime image requires that the JDK modules are present as `*.jmod` files, and some distributions
do not provide them by default. Check the `jmods` directory to ensure they are present:
[source,bash]
----
ls $JAVA_HOME/jmods
----
[TIP]
.OpenJDK on Linux
https://en.wikipedia.org/wiki/List_of_Linux_distributions#RPM-based[RPM based] distributions provide `\*.jmod` files in separate
`java-*-openjdk-jmods` packages.
https://en.wikipedia.org/wiki/List_of_Linux_distributions#Debian-based[Debian based] distributions provide `\*.jmod` files only
in the `openjdk-*-jdk-headless` packages.
== Generate the Project
Generate the project using the Helidon {flavor-uc} Quickstart Maven archetype.
[source,bash,subs="attributes+"]
----
mvn -U archetype:generate -DinteractiveMode=false \
-DarchetypeGroupId=io.helidon.archetypes \
-DarchetypeArtifactId=helidon-quickstart-{flavor-lc} \
-DarchetypeVersion={helidon-version} \
-DgroupId=io.helidon.examples \
-DartifactId=helidon-quickstart-{flavor-lc} \
-Dpackage=io.helidon.examples.quickstart.{flavor-lc}
----
The archetype generates a Maven project in your current directory
(for example, `helidon-quickstart-{flavor-lc}`). Change into this directory and build.
[source,bash,subs="attributes+"]
----
cd helidon-quickstart-{flavor-lc}
mvn package
----
At this point you can run the application using the JVM:
[source,bash,subs="attributes+"]
----
java -jar target/helidon-quickstart-{flavor-lc}.jar
----
In another shell test an endpoint:
[source,bash]
----
curl -X GET http://localhost:8080/greet
----
The application should respond with `{"message":"Hello World!"}`
Now stop the running application (by pressing Ctrl+C).
For more information about the Quickstart application and other endpoints it supports see the
xref:{guidesdir}/quickstart.adoc[Helidon {flavor-uc} quickstart Guide].
== Building a Custom Runtime Image
You can build a custom runtime image in 2 different ways:
* Locally, on your desktop
* Using Docker
=== Local Build
Build the custom runtime image using the jlink image profile:
[source,bash]
----
mvn package -Pjlink-image
----
[TIP]
This uses the `helidon-maven-plugin` to perform the custom image generation.
After the build completes it will report some statistics about the build including
the reduction in image size.
The `target/helidon-quickstart-{flavor-lc}-jri` directory is a self contained
custom image of your application. It contains your application, its runtime
dependencies and the JDK modules it depends on. You can start your application
using the provide
`start` script:
[source,bash,subs="attributes+"]
----
./target/helidon-quickstart-{flavor-lc}-jri/bin/start
----
=== Class Data Sharing (CDS) Archive
Also included in the custom image is a Class Data Sharing (CDS) archive that
improves your application's startup performance and in-memory footprint.
You can learn more about Class Data Sharing in the link:{jdk-doc-url}/vm/class-data-sharing.html[JDK documentation].
The CDS archive increases your image size to get these performance optimizations.
It can be of significant size (tens of MB). The size of the CDS archive is
reported at the end of the build output.
If you'd rather have a smaller image size (with a slightly increased startup time) you
can skip the creation of the CDS archive by executing your build like this:
[source,bash]
----
mvn package -Pjlink-image -Djlink.image.addClassDataSharingArchive=false
----
For more information on available configuration options see the
link:{helidon-maven-plugin-doc-url}#goal-jlink-image[`helidon-maven-plugin` documentation].
=== Multi-Stage Docker Build
To build a Docker image with a custom Java runtime image use the jlink
Dockerfile included with the quickstart.
[source,bash,subs="attributes+"]
----
docker build -t helidon-quickstart-{flavor-lc}-jri -f Dockerfile.jlink .
----
[TIP]
This does a full build inside the Docker container. The first
time you run it, it will take a while because it is downloading all
of the Maven dependencies and caching them in a Docker layer.
Subsequent builds will be much faster as long as you don't change
the `pom.xml` file. If the pom is modified then the dependencies
will be re-downloaded.
Start the application:
[source,bash,subs="attributes+"]
----
docker run --rm -p 8080:8080 helidon-quickstart-{flavor-lc}-jri:latest
----
You can exercise the application's endpoints as before.
== Using Custom Runtime Images
Custom runtime images are ideal for use when you want all of the runtime performance of
the JDK JVM in a reasonably compact form.
For cases where absolute minimal startup time and image size are required, then
consider using xref:{guidesdir}/graalnative.adoc[GraalVM Native Images].