Skip to content

Commit 57f35d8

Browse files
Create custom JRE manually and upgrade to Gradle 9 (#382)
1 parent 5ff4fdb commit 57f35d8

File tree

6 files changed

+52
-19
lines changed

6 files changed

+52
-19
lines changed

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ RUN curl -L --fail --retry 3 --retry-delay 5 "$LIBWEBP_URL" -O && \
1616

1717
ENV GRADLE_OPTS="-Dorg.gradle.daemon=false"
1818
COPY settings.gradle build.gradle gradlew ./
19-
COPY gradle ./gradle
20-
RUN --mount=type=cache,target=/home/gradle/.gradle/caches ./gradlew dependencies
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: 47 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,53 @@ tasks.withType(JavaCompile).configureEach {
5353
options.encoding = 'UTF-8'
5454
}
5555

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

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

63104
useJUnitPlatform()
64105
finalizedBy jacocoTestReport
@@ -93,10 +134,3 @@ shadowJar {
93134
exclude('dist_webp_binaries/')
94135
}
95136
}
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"

gradle/wrapper/gradle-wrapper.jar

1.65 KB
Binary file not shown.

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.0.0-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

gradlew

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)