@@ -3,19 +3,15 @@ package bisq.gradle.packaging
33import org.gradle.api.DefaultTask
44import org.gradle.api.file.DirectoryProperty
55import org.gradle.api.file.RegularFileProperty
6- import org.gradle.api.tasks.*
7- import java.io.BufferedReader
8- import java.io.InputStreamReader
9-
10-
11- // import java.util.regex.Pattern
6+ import org.gradle.api.tasks.InputDirectory
7+ import org.gradle.api.tasks.InputFile
8+ import org.gradle.api.tasks.Optional
9+ import org.gradle.api.tasks.OutputDirectory
10+ import org.gradle.api.tasks.TaskAction
11+ import java.util.concurrent.TimeUnit
1212
1313abstract class JLinkTask : DefaultTask () {
1414
15- // companion object {
16- // val pattern: Pattern = Pattern.compile(".*\\b(java\\..*|javax\\..*)\\b.*\$")
17- // }
18-
1915 @get:InputDirectory
2016 abstract val jdkDirectory: DirectoryProperty
2117
@@ -31,105 +27,46 @@ abstract class JLinkTask : DefaultTask() {
3127
3228 @TaskAction
3329 fun run () {
34- // Ensure the output directory is clean
30+ // jlink expects non-existent output directory
3531 val outputDirectoryFile = outputDirectory.asFile.get()
36- if (outputDirectoryFile.exists() && outputDirectoryFile.listFiles()?.isNotEmpty() == true ) {
37- // In case you want to run it manually, this will stop the plugin from running the task
38- logger.lifecycle(" custom jre already created, skipping - see: ${jdkDirectory.get()} " )
39- } else {
40- if (! outputDirectoryFile.canWrite()) {
41- throw IllegalStateException (" Output directory is not writable: ${outputDirectoryFile.absolutePath} " )
42- }
43- outputDirectoryFile.deleteRecursively()
44-
45- logger.lifecycle(" JDK path: ${jdkDirectory.get()} " )
46-
47- // Construct the jlink command
48- val jLinkPath = jdkDirectory.asFile.get().toPath().resolve(" bin" ).resolve(" jlink" )
49- val processBuilder = ProcessBuilder (
32+ outputDirectoryFile.deleteRecursively()
33+
34+ val jLinkPath = jdkDirectory.asFile.get().toPath().resolve(" bin" ).resolve(" jlink" )
35+ val processBuilder = ProcessBuilder (
5036 jLinkPath.toAbsolutePath().toString(),
51- " --verbose " ,
37+
5238 " --add-modules" , parseUsedJavaModulesFromJDepsOutput(),
53- " --include-locales=en,cs,de,es,it,pcm,pt-BR " ,
39+
5440 " --strip-native-commands" ,
5541 " --no-header-files" ,
5642 " --no-man-pages" ,
57- " --compress=2" ,
58- // "--strip-debug", // makes jlink fail in some platforms like Ubuntu linux
59- // "--add-reads", "java.base=ALL-UNNAMED",
43+ " --strip-debug" ,
44+
6045 " --output" , outputDirectoryFile.absolutePath
61- )
62- logger.lifecycle(" Executing jlink command: ${processBuilder.command().joinToString(" " )} " )
63-
64- // Add module path if specified
65- if (javaModulesDirectory.isPresent) {
66- val commands = processBuilder.command()
67- commands.add(" --module-path" )
68- commands.add(javaModulesDirectory.asFile.get().absolutePath)
69- logger.lifecycle(" Executing jlink command: ${commands.joinToString(" " )} " )
70- }
71-
72- logger.lifecycle(" Preparing process builder.." )
73- processBuilder.inheritIO()
74-
75- logger.lifecycle(" Starting process builder.." )
76- val process = processBuilder.start()
77- if (System .getProperty(" os.name" ).lowercase().contains(" windows" )) {
78- val reader =
79- BufferedReader (InputStreamReader (process.inputStream))
80- while ((reader.readLine()) != null ) {
81- // WORKAROUND: Jlink hangs in windows (11pro) when running from this custom plugin
82- // This buffer reader is needed jus to get it going and won't affect the rest of the platforms
83- }
84- }
85- logger.lifecycle(" Reading errors.." )
86- val errorOutput = process.errorStream.bufferedReader().readText()
87- val exitCode = process.waitFor()
88-
89- if (exitCode != 0 ) {
90- logger.error(" jlink error output:\n $errorOutput " )
91- throw IllegalStateException (" jlink couldn't create custom runtime. Exit code: $exitCode " )
92- }
46+ )
47+
48+ if (javaModulesDirectory.isPresent) {
49+ val commands = processBuilder.command()
50+ commands.add(" --module-path" )
51+ commands.add(javaModulesDirectory.asFile.get().absolutePath)
52+ }
53+
54+ processBuilder.inheritIO()
55+
56+ val process = processBuilder.start()
57+ process.waitFor(2 , TimeUnit .MINUTES )
58+
59+ val isSuccess = process.exitValue() == 0
60+ if (! isSuccess) {
61+ throw IllegalStateException (" jlink couldn't create custom runtime." )
9362 }
9463 }
9564
9665 private fun parseUsedJavaModulesFromJDepsOutput (): String {
97- // TODO instead of hardcoding he modules process the jdeps report and generate the below so we don't need
98- // to touch this code again in case of needed new jmods in the future
99- // readLines.filter { pattern.matcher(it).matches() }
100- // .flatMap { line -> line.split("->").map { it.trim() } }
101- // .filter { it.startsWith("java.") || it == "bisq.desktop_app_launcher" }
102- // .flatMap { line -> line.split(",").map { it.trim() } }
103- // .distinct()
104- // .joinToString(",")
105- // val readLines = jDepsOutputFile.asFile.get().readLines()
106- val modules = listOf (
107- // base
108- " java.base" ,
109- " java.datatransfer" ,
110- " java.desktop" ,
111- " java.logging" ,
112- " java.xml" ,
113- " java.naming" ,
114- " java.net.http" ,
115- // security
116- " java.se" ,
117- " java.security.jgss" ,
118- // javafx
119- " javafx.base" ,
120- " javafx.controls" ,
121- " javafx.fxml" ,
122- " javafx.graphics" ,
123- " javafx.media" ,
124- " javafx.swing" ,
125- // "javafx.web",
126- // jdk
127- " jdk.unsupported" ,
128- " jdk.localedata" ,
129- " jdk.crypto.cryptoki" ,
130- " jdk.crypto.ec"
131- ).joinToString(" ," )
132- logger.lifecycle(" Modules to be included: $modules " )
133- return modules
66+ var readLines = jDepsOutputFile.asFile.get().readLines()
67+ if (! javaModulesDirectory.isPresent) {
68+ readLines = readLines.filter { it.startsWith(" java." ) }
69+ }
70+ return readLines.joinToString(" ," )
13471 }
135- }
72+ }
0 commit comments