-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.gradle
executable file
·600 lines (519 loc) · 23 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import de.undercouch.gradle.tasks.download.Download
plugins {
id 'java'
id 'application'
// pulls the correct platform dependent native libraries onto the classpath
id 'org.openjfx.javafxplugin' version '0.1.0'
id 'com.github.johnrengelman.shadow' version '8.1.1'
id 'org.beryx.jlink' version '3.0.1'
id "de.undercouch.download" version "4.1.1"
}
group = 'io.github.vincemann'
version = '2.0.0'
application {
mainModule = 'io.github.vincemann.subtitlebuddy'
mainClass = 'io.github.vincemann.subtitlebuddy.Main'
}
ext {
// Use the provided targetPlatform property if available, otherwise use the detected OS
// Options are [linux | win | mac | mac-aarch64 ]
targetPlatform = project.hasProperty('targetPlatform') ? project.targetPlatform : detectOS()
println("target platform detected: " + targetPlatform)
releaseDir = "${projectDir}/build/releases"
}
static def detectOS() {
def os = org.gradle.internal.os.OperatingSystem.current()
println("Operating system detected: " + os)
if (os.isWindows()) {
println("Detected Windows OS")
return "win"
} else if (os.isLinux()) {
println("Detected Linux OS")
return "linux"
} else if (os.isMacOsX()) {
def arch = System.getProperty("os.arch")
println("Detected macOS with architecture: " + arch)
if (arch.contains("aarch64") || arch.contains("arm64")) {
println("Detected ARM architecture")
return "mac-aarch64"
} else {
return "mac"
}
}
throw new GradleException("Cannot evaluate current OS")
}
javafx {
version = "17"
modules = ['javafx.controls', 'javafx.fxml']
platform = targetPlatform
}
test {
// Set this to true to capture printed output during tests
testLogging.showStandardStreams = true
// Configure which events get logged
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
// To show detailed exception messages and stack traces in the console
exceptionFormat "full"
}
doFirst {
// Initialize the list with common JVM arguments
List<String> args = [
'--add-modules', javafx.modules.join(","),
// need to add this here to make mockito work
"-javaagent:${projectDir}/lib/byte-buddy-agent-1.14.15.jar",
'--add-reads', 'org.apache.commons.configuration2=ALL-UNNAMED',
'--add-exports', 'javafx.graphics/com.sun.javafx.application=org.testfx',
'--add-opens', 'javafx.graphics/com.sun.javafx.application=org.testfx'
]
// win always fails to pick stuff up from within fat jar or cp/modulepath
if (project.targetPlatform == 'win') {
println("adding win javafx modules to module path")
args += ['--module-path', "${projectDir}/lib/javafx/win/jmods"]
}
else if (project.targetPlatform.startsWith('mac')) {
args += ['--add-exports', 'java.desktop/com.apple.eawt=io.github.vincemann.subtitlebuddy']
// doesnt seem to be needed - native libs are picked up from classpath/modulepath
// println("adding osx javafx modules to module path")
// args += ['--module-path', "${projectDir}/lib/javafx/osx/jmods"]
}
// Assign the constructed argument list to jvmArgs
jvmArgs = args
}
}
// copies native jnativehook libraries into the image, which can be picked up by JLinkJNativeLibraryLocator
tasks.register("copyNativeLibs", Copy) {
mustRunAfter "jlink"
doFirst {
// Clean the destination directory before copying new files
delete "${projectDir}/build/image/appLauncher-${targetPlatform}/lib/jnativehook"
}
from "${projectDir}/lib/jnativehook"
into "${projectDir}/build/image/appLauncher-${targetPlatform}/lib/jnativehook"
doLast {
println "Native libraries copied to ${projectDir}/build/image/appLauncher-${targetPlatform}/lib/jnativehook"
}
}
// creates ./build/image-platform.zip
tasks.named('jlinkZip').configure {
dependsOn 'jlink'
dependsOn 'copyWinStartScript'
dependsOn 'copyNativeLibs'
}
// copy start script generated by jlink to correct location to be picked up by inno, when createWindowsInstaller called
// only relevant when building win jlink image and then createWindowsInstaller on linux host
tasks.register("copyWinStartScript", Copy) {
onlyIf { project.targetPlatform == 'win' }
doFirst {
if (project.targetPlatform != 'win') {
logger.lifecycle("Skipping copyWinStartScript as the target platform is not Windows.")
} else {
delete "${projectDir}/releases/windows/x64/inno/start.bat"
}
}
mustRunAfter "jlink"
from("${projectDir}/build/image/appLauncher-win/bin/appLauncher.bat") {
rename { 'start.bat' }
}
into "${projectDir}/releases/windows/x64/inno"
doLast {
println "Win start script copied to ${projectDir}/releases/windows/x64/inno"
}
}
tasks.named('jlink').configure {
finalizedBy 'copyWinStartScript', 'copyNativeLibs'
}
tasks.register('repackageMacApp', Exec) {
onlyIf { project.targetPlatform.startsWith('mac') }
mustRunAfter 'jpackage'
def originalAppPath = "${jpackage.getJpackageData().installerOutputDir}/subtitle-buddy.app"
println("Original app path: ${originalAppPath}")
doFirst {
if (!project.targetPlatform.startsWith('mac')) {
logger.lifecycle("Skipping repackageMacApp as the target platform is not macOS.")
return
}
// Define the binary and symlink target paths
def binaryPath = new File(originalAppPath, "Contents/MacOS/subtitle-buddy")
def targetPath = "../runtime/Contents/Home/bin/appLauncher" // Relative path from the binary location
// Remove the original binary if it exists
if (binaryPath.exists()) {
binaryPath.delete() // Delete the original binary
println "Original binary deleted."
}
// Create the symlink command
commandLine 'ln', '-s', targetPath, binaryPath.name
workingDir = binaryPath.parentFile
println "Symlink command set up to replace the original binary."
}
doLast {
println "Repackaging of macOS app completed."
}
}
// builds platform specific jlink images in build/images
// NOTE: for mac this only works, when building on mac for whatever reason - otherwise it will segfault
jlink {
options = [
'--strip-debug',
'--compress', '2',
'--no-header-files',
'--no-man-pages'
]
switch (targetPlatform) {
case 'linux':
targetPlatform("linux") {
jdkHome = jdkDownload('https://github.com/AdoptOpenJDK/openjdk17-binaries/releases/download/jdk-2021-05-07-13-31/OpenJDK-jdk_x64_linux_hotspot_2021-05-06-23-30.tar.gz')
}
// use jpackage to build deb installer
if (detectOS() == 'linux') {
jpackage {
targetPlatformName = "linux"
icon = "${projectDir}/icons/icon.png"
imageName = "subtitle-buddy"
installerName = "subtitle-buddy"
vendor = "vincemann"
installerOptions = [
'--linux-deb-maintainer', '[email protected]',
'--linux-menu-group', 'Applications',
]
// add post installation script for creating a symlink after installation
resourceDir = file("${projectDir}/releases/linux/deb")
println("resource dir: ${resourceDir.getPath()}")
installerType = 'deb'
}
}
break
case 'win':
targetPlatform("win") {
jdkHome = jdkDownload('https://github.com/AdoptOpenJDK/openjdk17-binaries/releases/download/jdk-2021-05-07-13-31/OpenJDK-jdk_x64_windows_hotspot_2021-05-06-23-30.zip')
addExtraModulePath("${projectDir}/lib/javafx/win/jmods")
}
if (detectOS() == 'win') {
jpackage {
targetPlatformName = "win"
icon = "${projectDir}/icons/icon.ico"
imageName = "subtitle-buddy"
installerName = "subtitle-buddy"
vendor = "vincemann"
installerOptions = ['--win-menu', '--win-shortcut']
installerType = 'msi'
}
}
break
case 'mac':
targetPlatform("mac") {
jdkHome = jdkDownload('https://github.com/AdoptOpenJDK/openjdk17-binaries/releases/download/jdk-2021-05-07-13-31/OpenJDK-jdk_x64_mac_hotspot_2021-05-06-23-30.tar.gz')
addExtraModulePath("${projectDir}/lib/javafx/osx/x86/jmods")
}
// use jpackage to build dmg and app files, only works when also building on mac
if (detectOS().startsWith('mac')) {
jpackage {
targetPlatformName = "mac"
imageName = "subtitle-buddy"
installerName = "subtitle-buddy"
icon = "${projectDir}/icons/icon.icns"
vendor = "vincemann"
}
}
break
case 'mac-aarch64':
targetPlatform("mac") {
jdkHome = jdkDownload('https://github.com/AdoptOpenJDK/openjdk17-binaries/releases/download/jdk-2021-05-07-13-31/OpenJDK-jdk_arm64_mac_hotspot_2021-05-06-23-30.tar.gz')
addExtraModulePath("${projectDir}/lib/javafx/osx/arm/jmods")
}
// use jpackage to build dmg and app files, only works when also building on mac
if (detectOS().startsWith('mac')) {
jpackage {
targetPlatformName = "mac"
imageName = "subtitle-buddy"
installerName = "subtitle-buddy"
icon = "${projectDir}/icons/icon.icns"
vendor = "vincemann"
}
}
break
default:
throw new GradleException("need to specify target platform")
}
launcher {
name = 'appLauncher'
mainClass = application.mainClass.get()
forceMerge('log4j-api')
// fixes : package org.apache.commons.logging is declared in module org.apache.commons.logging, but module io.github.vincemann.merged.module does not read it
forceMerge('commons-logging')
// make sure symlinks work when installed via homebrew and more
unixScriptTemplate = new File("${projectDir}/releases/linux/appLauncher")
windowsScriptTemplate = new File("${projectDir}/releases/windows/start.bat")
jvmArgs = [
// tells application to use the jlink lib locator for jnativehook
'-Djlink=true',
'--add-reads', 'org.apache.commons.configuration2=ALL-UNNAMED',
'--add-reads', 'io.github.vincemann.merged.module=org.apache.commons.logging',
'--add-reads', 'org.apache.commons.configuration2=io.github.vincemann.merged.module',
'--add-exports', 'java.desktop/com.apple.eawt=io.github.vincemann.subtitlebuddy'
// "--add-exports", "org.apache.commons.logging/org.apache.commons.logging=org.apache.commons.configuration2"
]
}
}
// somehow jpackage does not include my postinst script, so I need to repackage the deb package myself
// also I need to replace the startup binary with a symlink, bc otherwise my liblocator for jnativehook breaks
tasks.register('repackageDeb', Exec) {
onlyIf { project.targetPlatform == 'linux' }
mustRunAfter 'jpackage'
def originalDeb = "${jpackage.getJpackageData().installerOutputDir}/subtitle-buddy_${project.version}-1_amd64.deb" // there is no var to retrieve the installer name from
println("original deb: ${originalDeb}")
def tempDir = "${projectDir}/build/deb-temp"
// put postinst data into deb-temp
doFirst {
if (project.targetPlatform != 'linux') {
logger.lifecycle("Skipping repackageDeb as the target platform is not Linux.")
return
}
// Ensure the temp directory is clean
delete tempDir
// Unpack the original DEB
commandLine 'dpkg-deb', '-R', originalDeb, tempDir
}
doLast {
// I dont want a binary created in bin folder, bc that breaks my JLinkLibLocator,
// instead replace with symlink to runtime start script
def binDir = new File(tempDir, '/opt/subtitle-buddy/bin')
def originalBinary = new File(binDir, 'subtitle-buddy')
if (originalBinary.exists()) {
originalBinary.delete() // Delete the original binary
}
// Create a symlink
exec {
workingDir = binDir
commandLine 'ln', '-s', '../lib/runtime/bin/appLauncher', 'subtitle-buddy'
}
// Ensure the DEBIAN directory exists
def debianDir = new File(tempDir, 'DEBIAN')
def postinstScript = "$projectDir/releases/linux/deb/DEBIAN/postinst"
// Check if postinst already exists and delete if necessary
def postinstFile = new File(debianDir, 'postinst')
if (postinstFile.exists()) {
println "postinst script already exists, overwriting..."
postinstFile.delete()
}
// Copy the postinst script
copy {
from postinstScript
into debianDir
fileMode 0755
}
// Define the modified package path
def modifiedDeb = "${projectDir}/build/jpackage/subtitle-buddy-modified.deb"
// delete the old deb package
delete originalDeb
// Repackage the DEB
exec {
commandLine 'dpkg-deb', '-b', tempDir, modifiedDeb
}
// Log the completion
println "Modified DEB package created at: $modifiedDeb"
}
}
tasks.named('jpackageImage').configure {
dependsOn 'copyWinStartScript', 'copyNativeLibs'
finalizedBy 'repackageMacApp'
}
tasks.named('jpackage').configure {
finalizedBy 'repackageDeb'
}
def appImageToolUrl = 'https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-x86_64.AppImage'
def appImageToolPath = "${projectDir}/releases/linux/appimagetool.AppImage"
tasks.register('downloadAppImageTool', Download) {
src appImageToolUrl
dest appImageToolPath
onlyIfNewer true
doLast {
file(appImageToolPath).setExecutable(true)
}
}
// uses app image tool generating binary, which must be placed into: releases/linux/appimagetool.AppImage
// execute with -PtargetPlatform=linux
tasks.register('buildLinuxAppImage') {
dependsOn('jlink', 'downloadAppImageTool')
doFirst {
mkdir releaseDir
}
doLast {
if (targetPlatform != "linux") {
throw new GradleException("Need to specify linux target platform or run on linux")
}
// Define version and paths
def baseDir = "${projectDir}/releases/linux/x64"
def imageName = "subtitle-buddy-${version}-linuxx64.AppImage"
def linuxImagePath = "${releaseDir}/${imageName}"
def appImagePath = "${baseDir}/SubtitleBuddy.AppDir/usr/image"
def jlinkImagePath = "${projectDir}/build/image/appLauncher-linux" // Path to the jlink runtime image
// Prepare AppDir for AppImage
println "Preparing AppDir with jlink image..."
delete appImagePath // Clear the previous contents
mkdir appImagePath
copy {
from "$jlinkImagePath" // Copy runtime image created by jlink
into appImagePath
}
println "jlink runtime image copied"
// Build AppImage
println "Building AppImage..."
exec {
environment "ARCH", "x86_64"
commandLine 'bash', '-c', "cd ${baseDir} && ../appimagetool.AppImage SubtitleBuddy.AppDir $linuxImagePath"
}
println "AppImage created at $linuxImagePath"
println "Done with linux x64 AppImage build"
}
}
// used for creating installer for windows running on linux host system
// when building installer from win for win, better run 'gradlew.bat clean jpackage'
// uses a docker container with image name 'amake/innosetup' that uses wine for building windows installer from linux host machines
// execute with -PtargetPlatform=win
tasks.register('buildWindowsInstaller') {
dependsOn('jlink')
if (targetPlatform != "win") {
throw new GradleException("Need to specify win target platform")
}
def os = detectOS()
if (os == "win") {
throw new GradleException("This task is meant for building windows installer on a linux host machine. " +
"For building on windows use 'gradlew.bat clean jpackage'")
}
doFirst {
mkdir releaseDir
}
doLast {
def baseDir = "${projectDir}/releases/windows/x64"
def installerBaseDir = "${baseDir}/inno"
def outputExeName = "subtitle-buddy-installer.exe"
def installerName = "subtitle-buddy-${version}-windowsx64-installer.exe"
def outputExePath = "${installerBaseDir}/output/${outputExeName}"
def appImagePath = "${installerBaseDir}/image"
def jlinkImagePath = "${projectDir}/build/image/appLauncher-win" // Path to the jlink runtime image
// Ensure target directories exist
file(installerBaseDir).mkdirs()
// Deploy the jlink image to the installer directory
delete "$appImagePath"
mkdir "$appImagePath"
copy {
from "$jlinkImagePath" // Copy runtime image created by jlink
into "$appImagePath"
}
println "Deployed jlink runtime image to installer directory"
// Building the installer using Inno Setup within a Docker container
println "Building installer"
exec {
commandLine 'bash', '-c', "cd ${installerBaseDir} && docker run --rm -v \"\$PWD:/work\" amake/innosetup subtitle-buddy.iss"
}
def installerPath = "${releaseDir}/${installerName}"
delete installerPath
copy {
from "${installerBaseDir}/output/${outputExeName}"
into "${releaseDir}"
rename { String fileName ->
installerName
}
}
delete outputExePath
println "Installer created at ${installerPath}"
}
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
modularity.inferModulePath.set(true)
}
// builds fat jar containing javafx moules and all other deps
// in windows and macos jmods of javafx need to be shipped and declared in module path anyways
// after building, jar can be launched like this (if jdk17 is installed):
// linux: java --add-modules javafx.controls,javafx.fxml -jar /path/to/myjar.jar
// osx: java --module-path lib/javafx/osx/x86/sdk/lib --add-modules javafx.controls,javafx.fxml -jar /path/to/myjar.jar
// win: java --module-path lib/javafx/win/jmods --add-modules javafx.controls,javafx.fxml -jar /path/to/myjar.jar
// ( note that osx seems to need sdk jars and not jmods )
// Download: https://gluonhq.com/products/javafx/
shadowJar {
archiveClassifier.set('')
archiveFileName = "subtitle-buddy-${version}-${targetPlatform}.jar"
manifest {
attributes 'Main-Class': application.getMainClass().get()
}
mergeServiceFiles {}
// this removes needed classes for slf4j, making dynamic class loading of slf4j impl fail
// also the default jlink native lib loader probably wont work as well
// todo configure minimize with exceptions
// minimize() // Optional: minimize the jar to remove unused classes
}
repositories {
mavenCentral()
}
run {
doFirst {
// Initialize the list with common JVM arguments
List<String> args = [
// modules are not part of sdk anymore - need to add them manually to module graph
"--add-modules", javafx.modules.join(","),
'--add-reads', 'org.apache.commons.configuration2=ALL-UNNAMED'
// "--add-exports", "org.apache.commons.logging/org.apache.commons.logging.impl=org.apache.commons.configuration2"
]
// Conditionally add module-path based on the target platform
if (project.targetPlatform == 'win') {
println("adding win javafx modules to module path")
args += ['--module-path', "${projectDir}/lib/javafx/win/jmods"]
}
else if (project.targetPlatform == 'mac') {
println("adding osx javafx modules to module path")
args += ['--add-exports', 'java.desktop/com.apple.eawt=io.github.vincemann.subtitlebuddy']
args += ['--module-path', "${projectDir}/lib/javafx/osx/x86/jmods"]
}
else if (project.targetPlatform == 'mac-aarch64') {
println("adding osx javafx modules to module path")
args += ['--add-exports', 'java.desktop/com.apple.eawt=io.github.vincemann.subtitlebuddy']
args += ['--module-path', "${projectDir}/lib/javafx/osx/arm/jmods"]
}
jvmArgs = args
}
}
dependencies {
// capture mouse and keyboard events
implementation 'com.github.kwhat:jnativehook:2.2.2'
// di
implementation('com.google.inject:guice:7.0.0')
implementation('com.google.inject.extensions:guice-assistedinject:7.0.0')
implementation('com.google.inject.extensions:guice-multibindings:4.2.3')
implementation('commons-io:commons-io:2.16.1')
// logging
// using a version here that works with jlink plugin
implementation 'org.apache.logging.log4j:log4j-api:2.11.1'
implementation 'org.apache.logging.log4j:log4j-core:2.11.1'
// route apache commons logging to log4j2 (commons configuration depends on using commons logging)
runtimeOnly 'org.apache.logging.log4j:log4j-jcl:2.14.0'
// event handling
// using non modularized and same version that guice is using for max compatibility
// when using latest jpms version here, some classes needed by guice are missing
implementation('com.google.guava:guava:31.0.1-jre')
// utils
compileOnly 'org.projectlombok:lombok:1.18.28'
annotationProcessor 'org.projectlombok:lombok:1.18.28'
implementation('org.apache.commons:commons-configuration2:2.10.1')
implementation('org.apache.commons:commons-lang3:3.12.0')
// commons configuration 2 depends on this, but does not provide it
implementation('commons-beanutils:commons-beanutils:1.9.4') {
exclude group: 'commons-logging', module: 'commons-logging'
}
// detect encoding
implementation 'com.ibm.icu:icu4j:75.1'
// testing
testImplementation('org.testfx:testfx-core:4.0.18') {
exclude module: 'hamcrest-all'
}
testImplementation('org.testfx:testfx-junit:4.0.18') {
exclude module: 'hamcrest-core'
}
testImplementation('org.loadui:testFx:3.1.2')
testImplementation('org.hamcrest:hamcrest-core:1.3')
testImplementation 'org.mockito:mockito-core:5.12.0'
testImplementation 'junit:junit:4.13.2'
}