|
1 |
| -import org.gradle.crypto.checksum.Checksum |
2 |
| - |
3 | 1 | plugins {
|
4 | 2 | id("ktlint-publication-library")
|
5 | 3 | alias(libs.plugins.shadow)
|
6 |
| - alias(libs.plugins.checksum) |
7 | 4 | signing
|
8 | 5 | }
|
9 | 6 |
|
@@ -43,116 +40,37 @@ dependencies {
|
43 | 40 | testRuntimeOnly(libs.junit5.platform.launcher)
|
44 | 41 | }
|
45 | 42 |
|
46 |
| -// Implements https://github.com/brianm/really-executable-jars-maven-plugin maven plugin behaviour. |
47 |
| -// To check details how it works, see https://skife.org/java/unix/2011/06/20/really_executable_jars.html. |
48 |
| -val shadowJarExecutable by tasks.registering(DefaultTask::class) { |
49 |
| - description = "Creates self-executable file, that runs generated shadow jar" |
50 |
| - group = "Distribution" |
| 43 | +// Output is the self-executable file, the signature file and a batch file for Windows OS. |
| 44 | +// Folder content is published as GitHub release artifacts |
| 45 | +val ktlintOutputRoot = layout.buildDirectory.dir("run") |
51 | 46 |
|
| 47 | +val shadowJarExecutable by tasks.registering(ShadowJarExecutableTask::class) { |
52 | 48 | dependsOn(tasks.shadowJar)
|
53 | 49 |
|
54 | 50 | // Find the "ktlint-cli-<version>-all.jar" file
|
55 | 51 | val ktlintCliAllJarFile =
|
56 | 52 | tasks.shadowJar
|
57 | 53 | .get()
|
58 |
| - .outputs.files.singleFile |
59 |
| - logger.lifecycle("ktlint-cli: Base jar to build self-executable file: ${ktlintCliAllJarFile.absolutePath}") |
60 |
| - inputs.files(ktlintCliAllJarFile) |
61 |
| - |
62 |
| - val windowsBatchFileInputPath = layout.projectDirectory.file("src/main/scripts/ktlint.bat").asFile |
63 |
| - inputs.files(windowsBatchFileInputPath) |
64 |
| - // Output is the self-executable file, the signature file and a batch file for Windows OS |
65 |
| - val outputDirectoryPath = |
66 |
| - layout.buildDirectory |
67 |
| - .dir("run") |
| 54 | + .archiveFile |
68 | 55 | .get()
|
69 |
| - .asFile |
70 |
| - val selfExecutableKtlintOutputPath = outputDirectoryPath.resolve("ktlint") |
71 |
| - val selfExecutableKtlintSignatureOutputPath = outputDirectoryPath.resolve("ktlint.asc") |
72 |
| - val windowsBatchFileOutputPath = outputDirectoryPath.resolve("ktlint.bat") |
73 |
| - outputs.files(selfExecutableKtlintOutputPath) |
74 |
| - outputs.files(selfExecutableKtlintSignatureOutputPath) |
75 |
| - outputs.files(windowsBatchFileOutputPath) |
76 |
| - |
77 |
| - doLast { |
78 |
| - selfExecutableKtlintOutputPath.apply { |
79 |
| - logger.lifecycle("Creating the self-executable file: $selfExecutableKtlintOutputPath") |
80 |
| - |
81 |
| - // writeText effective replaces the entire content if the file already exists. If appendText is used, the file keeps on growing |
82 |
| - // with each build if the clean target is not used. |
83 |
| - writeText( |
84 |
| - """ |
85 |
| - #!/bin/sh |
86 |
| -
|
87 |
| - # From this SO answer: https://stackoverflow.com/a/56243046 |
88 |
| -
|
89 |
| - # First we get the major Java version as an integer, e.g. 8, 11, 16. It has special handling for the leading 1 of older java |
90 |
| - # versions, e.g. 1.8 = Java 8 |
91 |
| - JV=$(java -version 2>&1 | sed -E -n 's/.* version "([^.-]*).*".*/\1/p') |
92 |
| -
|
93 |
| - # Suppress warning "sun.misc.Unsafe::objectFieldOffset" on Java24+ (https://github.com/pinterest/ktlint/issues/2973) |
94 |
| - X=$( [ "${"$"}JV" -ge "24" ] && echo "${"$"}X --sun-misc-unsafe-memory-access=allow" || echo "") |
| 56 | + allJarFile.set(ktlintCliAllJarFile) |
| 57 | + windowsBatchScriptSource.set(layout.projectDirectory.file("src/main/scripts/ktlint.bat")) |
| 58 | + outputDirectory.set(ktlintOutputRoot) |
95 | 59 |
|
96 |
| - # Suppress warning "A restricted method in java.lang.System has been called" on Java 24 |
97 |
| - # Error is only printed when running command "ktlint --help" |
98 |
| - X=$( [ "${"$"}JV" -ge "24" ] && echo "${"$"}X --enable-native-access=ALL-UNNAMED" || echo "") |
99 |
| -
|
100 |
| - exec java ${"$"}X -Xmx512m -jar "$0" "$@" |
101 |
| -
|
102 |
| - """.trimIndent(), |
103 |
| - ) |
104 |
| - // Add the jar |
105 |
| - appendBytes(ktlintCliAllJarFile.readBytes()) |
106 |
| - |
107 |
| - setExecutable(true, false) |
108 |
| - |
109 |
| - if (signing.isRequired) { |
110 |
| - logger.lifecycle("Creating the signature file: $selfExecutableKtlintSignatureOutputPath") |
111 |
| - signing.sign(this) |
112 |
| - } |
113 |
| - } |
114 |
| - logger.lifecycle("Creating the batch file for Windows OS: $windowsBatchFileOutputPath") |
115 |
| - windowsBatchFileOutputPath.writeText(windowsBatchFileInputPath.readText()) |
116 |
| - logger.lifecycle("Finished creating output files ktlint-cli") |
117 |
| - } |
| 60 | + finalizedBy("signShadowJarExecutable") |
118 | 61 | }
|
119 | 62 |
|
120 |
| -tasks.register<Checksum>("shadowJarExecutableChecksum") { |
121 |
| - description = "Generates MD5 checksum for ktlint executable" |
122 |
| - group = "Distribution" |
123 |
| - |
| 63 | +val signShadowJarExecutable by tasks.registering(Sign::class) { |
124 | 64 | dependsOn(shadowJarExecutable)
|
125 | 65 |
|
126 |
| - inputFiles.setFrom(shadowJarExecutable.map { it.outputs.files }) |
127 |
| - // put the checksums in the same folder with the executable itself |
128 |
| - outputDirectory.fileProvider( |
129 |
| - shadowJarExecutable |
130 |
| - .map { |
131 |
| - it |
132 |
| - .outputs |
133 |
| - .files |
134 |
| - .files |
135 |
| - .first() |
136 |
| - .parentFile |
137 |
| - }, |
138 |
| - ) |
139 |
| - checksumAlgorithm = Checksum.Algorithm.MD5 |
| 66 | + sign(shadowJarExecutable.flatMap { it.selfExecutable }.get()) |
140 | 67 | }
|
141 | 68 |
|
142 | 69 | tasks.withType<Test>().configureEach {
|
143 | 70 | dependsOn(shadowJarExecutable)
|
144 | 71 |
|
145 | 72 | // TODO: Use providers directly after https://github.com/gradle/gradle/issues/12247 is fixed.
|
146 |
| - val executableFilePath = |
147 |
| - providers |
148 |
| - .provider { |
149 |
| - shadowJarExecutable |
150 |
| - .get() |
151 |
| - .outputs |
152 |
| - .files |
153 |
| - .first { it.name == "ktlint" } |
154 |
| - .absolutePath |
155 |
| - }.get() |
| 73 | + val executableFilePath = shadowJarExecutable.flatMap { it.selfExecutable }.map { it.absolutePath }.get() |
156 | 74 | val ktlintVersion = providers.provider { version }.get()
|
157 | 75 | doFirst {
|
158 | 76 | systemProperty(
|
|
0 commit comments