Skip to content

Commit 9f4c2f2

Browse files
Create custom JRE manually
1 parent 5ff4fdb commit 9f4c2f2

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ RUN curl -L --fail --retry 3 --retry-delay 5 "$LIBWEBP_URL" -O && \
1515
rm "$LIBWEBP_FILE"
1616

1717
ENV GRADLE_OPTS="-Dorg.gradle.daemon=false"
18-
COPY settings.gradle build.gradle gradlew ./
19-
COPY gradle ./gradle
20-
RUN --mount=type=cache,target=/home/gradle/.gradle/caches ./gradlew dependencies
18+
COPY *.gradle gradle.* gradlew ./
19+
COPY gradle gradle
20+
RUN --mount=type=cache,target=/root/.gradle ./gradlew dependencies
2121
COPY . .
22-
RUN ./gradlew runtime
22+
RUN --mount=type=cache,target=/root/.gradle ./gradlew jre shadowJar
2323

2424
FROM alpine AS bot
2525

build.gradle

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
1+
import org.apache.tools.ant.taskdefs.condition.Os
22

33
plugins {
4-
alias(libs.plugins.runtime)
54
alias(libs.plugins.shadow)
5+
id 'application'
66
id 'java'
77
id 'jacoco'
88
}
@@ -53,12 +53,48 @@ tasks.withType(JavaCompile).configureEach {
5353
options.encoding = 'UTF-8'
5454
}
5555

56+
def jreOutputDir = layout.buildDirectory.dir('jre')
57+
tasks.register("jre") {
58+
outputs.dir(jreOutputDir)
59+
60+
def toolchain = javaToolchains.launcherFor(java.toolchain)
61+
62+
doLast {
63+
def installationPath = toolchain.get().metadata.installationPath
64+
def jlink = installationPath.file('bin/jlink')
65+
def jmods = installationPath.dir('jmods')
66+
67+
delete(jreOutputDir)
68+
69+
def output = providers.exec {
70+
ignoreExitValue = true
71+
commandLine = [
72+
jlink.toString(), '-v',
73+
'--strip-debug', '--no-header-files', '--no-man-pages',
74+
'--module-path', jmods.toString(),
75+
'--add-modules', ['java.desktop', 'java.instrument', 'java.naming', 'java.sql', 'jdk.crypto.ec', 'jdk.unsupported'].join(','),
76+
'--output', jreOutputDir.get().toString()
77+
]
78+
}
79+
80+
def result = output.result.get()
81+
if (result.exitValue != 0) {
82+
logger.log(LogLevel.ERROR, output.standardError.asText.get())
83+
} else {
84+
logger.info(output.standardOutput.asText.get())
85+
}
86+
87+
result.assertNormalExitValue()
88+
result.rethrowFailure()
89+
}
90+
}
91+
5692
test {
57-
dependsOn jre
58-
inputs.dir tasks.jre.jreDir
93+
dependsOn tasks.named('jre')
94+
inputs.dir(jreOutputDir)
5995

60-
def file = DefaultNativePlatform.currentOperatingSystem.isWindows() ? 'java.exe' : 'java'
61-
executable = tasks.jre.jreDir.file('bin/' + file)
96+
def exe = Os.isFamily(Os.FAMILY_WINDOWS) ? 'bin\\java.exe' : 'bin/java'
97+
executable = jreOutputDir.get().file(exe)
6298

6399
useJUnitPlatform()
64100
finalizedBy jacocoTestReport
@@ -93,10 +129,3 @@ shadowJar {
93129
exclude('dist_webp_binaries/')
94130
}
95131
}
96-
97-
runtime {
98-
options = ['--strip-debug', '--no-header-files', '--no-man-pages'] // don't compress jre because final docker image will be larger
99-
modules = ['java.desktop', 'java.instrument', 'java.naming', 'java.sql', 'jdk.crypto.ec', 'jdk.unsupported']
100-
}
101-
102-
suggestModules.dependsOn shadowJar

gradle/libs.versions.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ telegram-bot-api = "com.github.pengrad:java-telegram-bot-api:8.3.0"
2424
tika = "org.apache.tika:tika-core:3.2.1"
2525

2626
[plugins]
27-
runtime = "com.dua3.gradle.runtime:1.13.1-patch-1"
2827
shadow = "com.gradleup.shadow:8.3.8"

0 commit comments

Comments
 (0)