Skip to content

Commit b8de112

Browse files
committed
Merge remote-tracking branch 'Josh-Matsuoka/ubi9-jlink' into jlink-dev
2 parents b702622 + 9433d1a commit b8de112

File tree

20 files changed

+321
-85
lines changed

20 files changed

+321
-85
lines changed

modules/jdk/11/module.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ envs:
2525
packages:
2626
install:
2727
- java-11-openjdk-devel
28+
- java-11-openjdk-jmods
2829
- tzdata-java
2930

3031
modules:

modules/jdk/17/module.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ envs:
2525
packages:
2626
install:
2727
- java-17-openjdk-devel
28+
- java-17-openjdk-jmods
2829

2930
modules:
3031
install:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
function generatejdkdeps() {
4+
echo "Generating JDK deps"
5+
$JAVA_HOME/bin/java --list-modules > java-modules.txt
6+
< java-modules.txt sed "s/\\@.*//" > modules.txt
7+
grep -Fx -f stripped-deps.txt modules.txt | tr '\n' ',' | tr -d "[:space:]" > module-deps.txt
8+
echo "jdk.zipfs" >> module-deps.txt
9+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
shopt -s globstar
4+
5+
function generate_deps() {
6+
# Create a temporary directory for a module path
7+
# This works around "Module java.xml.bind not found, required by java.ws.rs"
8+
mkdir dependencies
9+
10+
if [[ -v JAVA_LIB_DIR ]]; then
11+
# Serially copy all library JARsinto a flat directory. Serially as we may
12+
# have multiple libs with the same name; in which case, we clobber all but
13+
# one rather than fail the script
14+
find $JAVA_LIB_DIR -type f -name '*.jar' -exec cp -vt dependencies {} \;
15+
16+
echo "Working with: "
17+
echo $JAVA_APP_JAR
18+
echo $JAVA_LIB_DIR
19+
# generate the dependency list
20+
$JAVA_HOME/bin/jdeps --multi-release $JAVA_VERSION -R -s \
21+
--module-path dependencies \
22+
"$JAVA_APP_JAR" \
23+
"$JAVA_LIB_DIR"/**/*.jar \
24+
> deps.txt || {
25+
echo "jdeps failed: return code $?"
26+
exit $?
27+
}
28+
else
29+
$JAVA_HOME/bin/jdeps --multi-release $JAVA_VERSION -R -s \
30+
--module-path dependencies \
31+
"$JAVA_APP_JAR" \
32+
> deps.txt
33+
cat deps.txt
34+
fi
35+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
# TODO: Still Needed?
3+
set -euo pipefail
4+
5+
depsfile="module-deps.txt"
6+
7+
function generate_jre_image() {
8+
test -f $depsfile
9+
modules="$(cat $depsfile)"
10+
11+
$JAVA_HOME/bin/jlink --output "$S2I_JLINK_OUTPUT_PATH" \
12+
--add-modules "$modules" \
13+
--strip-debug --no-header-files --no-man-pages \
14+
--compress=2
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
function mkstrippeddeps() {
5+
if [ -f "deps.txt" ]; then
6+
echo "deps exists, filtering"
7+
<deps.txt \
8+
grep 'java\|jdk\.' | # mostly removes target/, but also jdk8internals
9+
sed -E "s/Warning: .*//" | #remove extraneous warnings
10+
sed -E "s/.*-> //" | # remove src of src -> dep
11+
sed -E "s/.*\.jar//" | # remove extraneous dependencies
12+
sed "s#/.*##" | # delete anything after a slash. in practice target/..
13+
sort | uniq |
14+
tee stripped-deps.txt
15+
echo "Stripping dependencies complete"
16+
else
17+
echo "deps does not exist"
18+
fi
19+
}

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/jlink/*
11+
12+
pushd ${ARTIFACTS_DIR}
13+
cp -pr * /
14+
popd

modules/jlink/module.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
execute:
10+
- script: configure.sh
11+
12+
envs:
13+
- name: JBOSS_CONTAINER_JAVA_JLINK_MODULE
14+
value: /opt/jboss/container/java/jlink
15+
- name: S2I_JLINK_OUTPUT_PATH
16+
value: /tmp/jre
17+
18+
modules:
19+
install:
20+
- name: jboss.container.java.run
21+
- name: jboss.container.util.pathfinder
22+
23+
packages:
24+
install:
25+
- binutils # for objcopy

modules/run/artifacts/opt/jboss/container/java/run/run-java.sh

Lines changed: 5 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -30,96 +30,19 @@ check_error() {
3030
fi
3131
}
3232

33-
# detect Quarkus fast-jar package type (OPENJDK-631)
34-
is_quarkus_fast_jar() {
35-
if test -f quarkus-app/quarkus-run.jar; then
36-
log_info "quarkus fast-jar package type detected"
37-
echo quarkus-app/quarkus-run.jar
38-
return 0
39-
else
40-
return 1
41-
fi
42-
}
43-
44-
# Try hard to find a sane default jar-file
45-
auto_detect_jar_file() {
46-
local dir=$1
47-
48-
# Filter out temporary jars from the shade plugin which start with 'original-'
49-
local old_dir=$(pwd)
50-
cd ${dir}
51-
if [ $? = 0 ]; then
52-
53-
if quarkus="$(is_quarkus_fast_jar)"; then
54-
echo "$quarkus"
55-
return
56-
fi
57-
58-
local nr_jars=`ls *.jar 2>/dev/null | grep -v '^original-' | wc -l | tr -d '[[:space:]]'`
59-
if [ ${nr_jars} = 1 ]; then
60-
ls *.jar | grep -v '^original-'
61-
exit 0
62-
fi
63-
64-
log_error "Neither \$JAVA_MAIN_CLASS nor \$JAVA_APP_JAR is set and ${nr_jars} JARs found in ${dir} (1 expected)"
65-
cd ${old_dir}
66-
else
67-
log_error "No directory ${dir} found for auto detection"
68-
fi
69-
}
70-
71-
# Check directories (arg 2...n) for a jar file (arg 1)
72-
get_jar_file() {
73-
local jar=$1
74-
shift;
75-
76-
if [ "${jar:0:1}" = "/" ]; then
77-
if [ -f "${jar}" ]; then
78-
echo "${jar}"
79-
else
80-
log_error "No such file ${jar}"
81-
fi
82-
else
83-
for dir in $*; do
84-
if [ -f "${dir}/$jar" ]; then
85-
echo "${dir}/$jar"
86-
return
87-
fi
88-
done
89-
log_error "No ${jar} found in $*"
90-
fi
91-
}
92-
9333
load_env() {
9434
# Configuration stuff is read from this file
9535
local run_env_sh="run-env.sh"
96-
36+
9737
# Load default default config
9838
if [ -f "${JBOSS_CONTAINER_JAVA_RUN_MODULE}/${run_env_sh}" ]; then
9939
source "${JBOSS_CONTAINER_JAVA_RUN_MODULE}/${run_env_sh}"
10040
fi
10141

102-
# Check also $JAVA_APP_DIR. Overrides other defaults
103-
# It's valid to set the app dir in the default script
104-
if [ -f "${JAVA_APP_DIR}/${run_env_sh}" ]; then
105-
source "${JAVA_APP_DIR}/${run_env_sh}"
106-
fi
107-
108-
export JAVA_APP_DIR
109-
110-
# JAVA_LIB_DIR defaults to JAVA_APP_DIR
111-
export JAVA_LIB_DIR="${JAVA_LIB_DIR:-${JAVA_APP_DIR}}"
112-
if [ -z "${JAVA_MAIN_CLASS}" ] && [ -z "${JAVA_APP_JAR}" ]; then
113-
JAVA_APP_JAR="$(auto_detect_jar_file ${JAVA_APP_DIR})"
114-
check_error "${JAVA_APP_JAR}"
115-
fi
116-
117-
if [ "x${JAVA_APP_JAR}" != x ]; then
118-
local jar="$(get_jar_file ${JAVA_APP_JAR} ${JAVA_APP_DIR} ${JAVA_LIB_DIR})"
119-
check_error "${jar}"
120-
export JAVA_APP_JAR=${jar}
121-
else
122-
export JAVA_MAIN_CLASS
42+
# Load JAVA_APP_JAR and JAVA_LIB_DIR
43+
if [ -f "${JBOSS_CONTAINER_UTIL_PATHFINDER_MODULE}/pathfinder.sh" ]; then
44+
source "$JBOSS_CONTAINER_UTIL_PATHFINDER_MODULE/pathfinder.sh"
45+
setup_java_app_and_lib
12346
fi
12447
}
12548

modules/s2i/bash/artifacts/usr/local/s2i/assemble

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,24 @@ source "${JBOSS_CONTAINER_JAVA_S2I_MODULE}/maven-s2i-overrides"
1414
# invoke the build
1515
maven_s2i_build
1616

17+
# run the pathfinder scripts to define JAVA_APP_JAR and JAVA_LIB_DIR
18+
source "${JBOSS_CONTAINER_UTIL_PATHFINDER_MODULE}/pathfinder.sh"
19+
echo "Setting up java app and lib variables"
20+
setup_java_app_and_lib
21+
22+
# include our jlink scripts
23+
source "${JBOSS_CONTAINER_JAVA_JLINK_MODULE}/mkdeps.sh"
24+
echo "Invoking mkdeps"
25+
generate_deps
26+
27+
source "${JBOSS_CONTAINER_JAVA_JLINK_MODULE}/mkstrippeddeps.sh"
28+
echo "Stripping dependencies"
29+
mkstrippeddeps
30+
31+
source "${JBOSS_CONTAINER_JAVA_JLINK_MODULE}/generatejdkdeps.sh"
32+
echo "Generating JDK dependencies"
33+
generatejdkdeps
34+
35+
source "${JBOSS_CONTAINER_JAVA_JLINK_MODULE}/mkjreimage.sh"
36+
echo "Linking jre"
37+
generate_jre_image

0 commit comments

Comments
 (0)