Skip to content

Commit e28d02d

Browse files
committed
Initial implementation of a Jlink Module in the container sources.
Copies the scripts into the S2I artifacts directory Resolves OPENJDK-2178
1 parent 8938ae2 commit e28d02d

File tree

11 files changed

+155
-0
lines changed

11 files changed

+155
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
2+
3+
# binutils: objcopy
4+
RUN microdnf --setopt=install_weak_deps=0 --setopt=tsflags=nodocs install -y \
5+
java-17-openjdk-headless java-17-openjdk-devel java-17-openjdk-jmods \
6+
binutils \
7+
&& microdnf clean all \
8+
&& rpm -q \
9+
java-17-openjdk-headless java-17-openjdk-devel java-17-openjdk-jmods
10+
11+
ENV JAVA_HOME=/usr/lib/jvm/jre
12+
WORKDIR /tmp
13+
COPY generatejdkdeps.sh mkdeps.sh mkjreimage.sh mkstrippeddeps.sh runall.sh /tmp/
14+
15+
# ?
16+
USER 1000
17+
RUN mkdir -p /tmp/run/lib
18+
19+
CMD ./runall.sh
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# jlink-openshift App builder
2+
3+
This container bundles together scripts to analyse a Java application
4+
with `jdeps` and generate a stripped `jre` with only the required Java
5+
modules to run that application.
6+
7+
## Usage
8+
9+
### building
10+
11+
podman build -t jlink ./
12+
13+
### running
14+
15+
Assuming you have the following defined locally:
16+
17+
* `$jarfile`, a local web application JAR
18+
* `$libdir`, directory containing auxillary Java libraries (JARs) for the above
19+
* `$outputjre`, where to write the stripped JRE
20+
21+
Invoke as follows
22+
23+
podman run --rm -ti \
24+
-v "$jarfile":/tmp/run/app.jar \
25+
-v "$libdir":/tmp/run/lib \
26+
-v "$outputjre":/tmp/run/out \
27+
jlink
28+
29+
## Script details
30+
31+
Most of the scripts require `$JAVA_HOME` to be defined.
32+
33+
* `mkdeps.sh`: application → jdeps → deps.txt
34+
* `mkstrippeddeps.sh`: deps.txt → filtering → stripped-deps.txt
35+
* `generatejdkdeps.sh`: stripped-deps.txt → filtering → module-deps.txt
36+
* `mkjreimage.sh`: module-deps.txt → jlink → custom JDK
37+
* `runall.sh`: runs all of the above
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
$JAVA_HOME/bin/java --list-modules > java-modules.txt
4+
< java-modules.txt sed "s/\\@.*//" > modules.txt
5+
grep -Fx -f stripped-deps.txt modules.txt | tr '\n' ',' | tr -d "[:space:]" > module-deps.txt
6+
echo "jdk.zipfs" >> module-deps.txt
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
shopt -s globstar
4+
5+
project="${project-spring-boot-sample-simple}"
6+
jarfile="${jarfile-$project/target/spring-boot-sample-simple-1.5.0.BUILD-SNAPSHOT.jar}"
7+
libdir="${libdir-$project/target/lib}"
8+
9+
test -f "$jarfile"
10+
test -d "$libdir"
11+
12+
# Create a temporary directory for a module path
13+
# This works around "Module java.xml.bind not found, required by java.ws.rs"
14+
mkdir dependencies
15+
find $libdir -type f -name '*.jar' -print0 | xargs -r0 cp -vt dependencies
16+
17+
$JAVA_HOME/bin/jdeps --multi-release 11 -R -s \
18+
--module-path dependencies \
19+
"$jarfile" \
20+
"$libdir"/**/*.jar \
21+
> deps.txt
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
outdir="${outdir-spring-boot-jre}"
5+
depsfile="module-deps.txt"
6+
7+
test -f $depsfile
8+
modules="$(cat $depsfile)"
9+
10+
$JAVA_HOME/bin/jlink --output "${outdir}/jre" \
11+
--add-modules "$modules" \
12+
--strip-debug --no-header-files --no-man-pages \
13+
--compress=2
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
test -f deps.txt
5+
6+
<deps.txt \
7+
grep 'java\|jdk\.' | # mostly removes target/, but also jdk8internals
8+
sed -E "s/Warning: .*//" | #remove extraneous warnings
9+
sed -E "s/.*-> //" | # remove src of src -> dep
10+
sed -E "s/.*\.jar//" | # remove extraneous dependencies
11+
sed "s#/.*##" | # delete anything after a slash. in practice target/..
12+
sort | uniq |
13+
tee stripped-deps.txt
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
set -euo pipefail
3+
set -x
4+
5+
jarfile="/tmp/run/app.jar"
6+
libdir="/tmp/run/lib"
7+
outdir="/tmp/run/out"
8+
9+
test -e "$JAVA_HOME"
10+
11+
export project jarfile libdir outdir
12+
13+
bash -x ./mkdeps.sh
14+
bash -x ./mkstrippeddeps.sh
15+
bash -x ./generatejdkdeps.sh
16+
bash -x ./mkjreimage.sh

modules/jlink/configure.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
# Configure module
3+
set -e
4+
5+
SCRIPT_DIR=$(dirname $0)
6+
ARTIFACTS_DIR=${SCRIPT_DIR}/artifacts
7+
8+
chown -R default:root $SCRIPT_DIR
9+
chmod -R ug+rwX $SCRIPT_DIR
10+
chmod ug+x ${ARTIFACTS_DIR}/opt/jboss/container/java/s2i/*
11+
12+
pushd ${ARTIFACTS_DIR}
13+
cp -pr * /
14+
popd

modules/jlink/module.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
schema_version: 1
2+
3+
name: "jboss.container.java.jlink"
4+
version: "2.0"
5+
description: ^
6+
"Provides support for building custom JREs with a slimmed
7+
down set of modules by making use of Jdeps and Jlink"
8+
9+
envs:
10+
- name: JBOSS_CONTAINER_JAVA_JLINK_MODULE
11+
value: /opt/jboss/container/java/jlink
12+
13+
execute:
14+
- script: configure.sh

ubi9-openjdk-11.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ modules:
4949
- name: jboss.container.maven
5050
version: "3.8.11"
5151
- name: jboss.container.java.s2i.bash
52+
- name: jboss.container.java.jlink
5253

5354
help:
5455
add: true

0 commit comments

Comments
 (0)