This is a cache of https://docs.okd.io/3.9/using_images/s2i_images/java.html. It is a snapshot of the page at 2024-11-22T04:08:27.626+0000.
Java - Source-to-Image (<strong>s2i</strong>) | Using Images | OKD 3.9
×

Overview

OKD provides an s2i builder image for building Java applications. This builder image takes your application source or binary artifacts, builds the source using Maven (if source was provided), and assembles the artifacts with any required dependencies to create a new, ready-to-run image containing your Java application. This resulting image can be run on OKD or run directly with Docker.

The builder image is intended for use with Maven-based Java standalone projects that are run via main class.

Versions

The current version of the Java s2i builder image supports OpenJDK 1.8, Jolokia 1.3.5, and Maven 3.3.9-2.8.

Images

The RHEL 7 image is available through the Red Hat Registry:

$ docker pull registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift

To use this image on OKD, you can either access it directly from the Red Hat Registry or push it into your OKD Docker registry. Additionally, you can create an image stream that points to the image, either in your Docker registry or at the external location. Your OKD resources can then reference the image stream definition.

Build Process

s2i produces ready-to-run images by injecting source code into a container and letting the container prepare that source code for execution. It performs the following steps:

  1. Starts a container from the builder image.

  2. Downloads the application source.

  3. Streams the scripts and application sources into the builder image container.

  4. Runs the assemble script (from the builder image).

  5. Saves the final image.

See s2i Build Process for a detailed overview of the build process.

Configuration

By default, the Java s2i builder image uses Maven to build the project with the following goals and options:

mvn -Dmaven.repo.local=/tmp/artifacts/m2 -s /tmp/artifacts/configuration/settings.xml -e -Popenshift -DskipTests -Dcom.redhat.xpaas.repo.redhatga -Dfabric8.skip=true package -Djava.net.preferIPv4Stack=true

Based on these defaults, the builder image compiles the project and copies all the transitive dependencies into the output directory without running tests. Additionally, if the project has a profile named openshift, then it is activated for the build.

You can override these default goals and options by specifying the following environment variables:

Table 1. Java Environment Variables
Variable name Description

ARTIFACT_DIR

The relative path to the target where JAR files are created for multi-module builds.

JAVA_MAIN_CLASS

The main class to use as the argument to Java. This can also be specified in the .s2i/environment file as a Maven property inside the project (docker.env.Main).

MAVEN_ARGS

The arguments that are passed to the mvn command.

Building and Deploying Java Applications

The same s2i builder image can be used to build a Java application from source or from binary artifacts.

Building and Deploying from Source

The Java s2i builder image can be used to build an application from source by running oc new-app against a source repository:

$ oc new-app registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift~https://github.com/jboss-openshift/openshift-quickstarts --context-dir=undertow-servlet

By default, tests are not run. To build an application and run tests as part of the build, override the default MAVEN_ARGS, as in the following command:

$ oc new-app registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift~<git_repo_URL> --context-dir=<context-dir> --build-env='MAVEN_ARGS=-e -Popenshift -Dcom.redhat.xpaas.repo.redhatga package'

If a Java project consists of multiple Maven modules, it can be useful to explicitly specify the artifact output directory. Specifying the directory where the Maven project outputs the artifacts enables the s2i build to pick them up.

To specify the modules to build and the artifact output directory, use the following command:

$ oc new-app registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift~<git_repo_URL> --context-dir=<context-dir> --build-env='ARTIFACT_DIR=relative/path/to/artifacts/dir' --build-env='MAVEN_ARGS=install -pl <groupId>:<artifactId> -am'

Building and Deploying from Binary Artifacts

The Java s2i builder image can be used to build an application using binary artifacts that you provide. To do so, first create a new binary build using the oc new-build command:

$ oc new-build --name=<application-name> registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift --binary=true

Next, start a build using the oc start-build command, specifying the path to the binary artifacts on your local machine:

$ oc start-build <application-name> --from-dir=/path/to/artifacts --follow

Finally, use the oc new-app command to create an application:

$ oc new-app <application-name>

Additional Information and Examples