Custom setting.xml in OpenJDK container build
Environment
- Red Hat OpenShift Container Platform (OCP)
- 4.X
- Red Hat build of podman
Issue
- How to use a custom setting.xml in OpenJDK build?
- Env variable to use a custom setting.xml in OpenJDK build
- How to use a custom setting.xml in OpenJDK build?
Resolution
The MAVEN_* environment variables include MAVEN_SETTINGS_XML and MAVEN_OPTS.
For a custom setting.xml use via MAVEN_SETTINGS_XML or the complete settings can be overwritten via MAVEN_OPTS.
For Maven settings see the solution Using Maven parameters on Buildconfig in OCP 4.
If what you want to do is extend the options passed to maven, you can pass additional options (without overriding the ones generated by the S2i script) with MAVEN_ARGS_APPEND.
Auto-generated docs for the environments vars are published here: https://rh-openjdk.github.io/redhat-openjdk-containers/.
Note that the reason that the resulting image, will have the ~/.m2/settings.xml unaffected by MAVEN_SETTINGS_XML due to how S2I works: it runs a build (within which the settings.xml is affected by MAVEN_SETTINGS_XML) and then copies the resulting artifacts out of the builder container instance and into a new image, layered on top of the original builder image. It does not copy ~/.m2/settings.xml as part of that process.
Example
$ s2i --loglevel 5 build SessionReplication/ --environment-file=/path/SessionReplication/.s2i/environment openjdk-pre-build:latest openjdk-session:example
I0204 12:46:47.774396 92895 build.go:52] Running S2I version "unknown"
I0204 12:46:47.774540 92895 util.go:70] Getting docker credentials for openjdk-pre-build:latest
W0204 12:46:48.352851 92895 build.go:167] could not inspect the builder image for labels: reading manifest latest in docker.io/library/openjdk-pre-build: requested access to the resource is denied
I0204 12:46:48.356415 92895 docker.go:491] Using locally available image "docker.io/library/openjdk-pre-build:latest"
I0204 12:46:48.357808 92895 build.go:170]
Builder Name: Java Applications
Builder Image: docker.io/library/openjdk-pre-build:latest
Source: SessionReplication/
Output Image Tag: openjdk-session:example
Environment:
MAVEN_SETTINGS_XML=/opt/settings.xml
MAVEN_OPTS=-XX:MaxRAMPercentage=80.0 -XX:+UseG1GC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:+ExitOnOutOfMemoryError -XX:MaxRAMPercentage=47.3
Environment File: /home/fdemeloj/Downloads/RedHat/openjdk/SessionReplication/.s2i/environment
...
I0204 12:52:34.364996 94088 sti.go:717] INFO Using MAVEN_OPTS -XX:MaxRAMPercentage=80.0 -XX:+UseG1GC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:+ExitOnOutOfMemoryError -XX:MaxRAMPercentage=47.3
...
I0204 12:53:03.525305 94088 docker.go:1104] Committing container with dockerOpts: {Reference:openjdk-session:example Comment: Author: Changes:[] Pause:false Config:0xc00032c780}, config: {Hostname: Domainname: User:185 AttachStdin:false AttachStdout:false AttachStderr:false ExposedPorts:map[] Tty:false OpenStdin:false StdinOnce:false Env:[MAVEN_OPTS=-XX:MaxRAMPercentage=80.0 -XX:+UseG1GC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:+ExitOnOutOfMemoryError -XX:MaxRAMPercentage=47.3 MAVEN_SETTINGS_XML=/opt/settings.xml]
...
...
...
I0204 12:47:17.706033 92895 postexecutorstep.go:393] Executing step: report success
I0204 12:47:17.706046 92895 postexecutorstep.go:398] Successfully built openjdk-session:example <------------
I0204 12:47:17.706051 92895 postexecutorstep.go:93] Skipping step: remove previous image
...
I0204 12:47:17.732728 92895 fs.go:307] Removing directory '/tmp/s2i1583933938'
I0204 12:47:17.733243 92895 build.go:182] Build completed successfully
Result:
$ docker run --entrypoint=/bin/bash --rm -it openjdk-session:example
$ cat ./.m2/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- ### configured local repository ### --> <servers>
<!-- ### configured servers ### -->
</servers> <mirrors>
<!-- ### configured mirrors ### -->
</mirrors> <proxies>
<!-- ### configured http proxy ### -->
</proxies> <profiles>
The example above is not supported (s2i stand-alone toolkit is not supported) and the resulting output container image will have the default (from original container image) m2.settings.xml.
Diagnostic Steps
~/.m2/settings.xmlwill be the same on the resulting image than the original OpenJDK image.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.