Java
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.redhat.io/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 container image registry. Additionally, you can create an image stream that points to the image, either in your container image 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:
Starts a container from the builder image.
Downloads the application source.
Streams the scripts and application sources into the builder image container.
Runs the assemble script (from the builder image).
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:
Variable name | Description |
---|---|
| The relative path to the target where JAR files are created for multi-module builds. |
| 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). |
| 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.redhat.io/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.redhat.io/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.redhat.io/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.redhat.io/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
- Find additional information and examples in the Red Hat JBoss Middleware documentation.