-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathort-application-conventions.gradle.kts
More file actions
233 lines (194 loc) · 7.99 KB
/
ort-application-conventions.gradle.kts
File metadata and controls
233 lines (194 loc) · 7.99 KB
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
/*
* Copyright (C) 2023 The ORT Project Copyright Holders <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.KotlinJvm
import java.nio.charset.Charset
import java.nio.file.Files
import org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask
import org.jetbrains.kotlin.buildtools.api.ExperimentalBuildToolsApi
val javaLanguageVersion: String by project
val libsCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
plugins {
// Apply core plugins.
application
signing
// Apply precompiled plugins.
id("ort-kotlin-conventions")
id("ort-publication-conventions")
// Apply third-party plugins.
id("org.graalvm.buildtools.native")
}
application {
// As the Java version used at runtime is not generally known, only hard-code some uncritical options.
applicationDefaultJvmArgs = listOf(
"--add-opens", "java.base/java.io=ALL-UNNAMED",
"--add-opens", "java.base/sun.nio.ch=ALL-UNNAMED"
)
}
mavenPublishing {
// Note that "dokkaGenerateHtml" is simply an alias for the below task name.
configure(KotlinJvm(JavadocJar.Dokka("dokkaGeneratePublicationHtml")))
}
graalvmNative {
toolchainDetection = System.getenv("GRAALVM_HOME") == null
// For options see https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html.
binaries {
named("main") {
imageName = provider { application.applicationName }
val initializeAtBuildTime = listOf(
"ch.qos.logback",
"ch.qos.logback.classic.Logger",
"org.apache.sshd.common.file.root.RootedFileSystemProvider",
"org.xml.sax.helpers"
).joinToString(separator = ",", prefix = "--initialize-at-build-time=")
buildArgs.addAll(
initializeAtBuildTime,
"--parallelism=8",
"-J-Xmx16g",
"-Os"
)
excludeConfig.putAll(
mapOf(
// JLine is shaded into kotlin-compiler-embeddable, rendering the configuration invalid. See
// https://youtrack.jetbrains.com/issue/KT-68829.
@OptIn(ExperimentalBuildToolsApi::class)
"org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlin.compilerVersion.get()}" to
listOf("^/META-INF/native-image/org.jline/.*"),
// The contained "reflect-config.json" does not match the code of the AWS flavor of the Apache HTTP
// client.
"software.amazon.awssdk:apache-client:2.46.3" to
listOf("^/META-INF/native-image/software.amazon.awssdk/apache-client/.*")
)
)
}
}
metadataRepository {
enabled = true
}
}
dependencies {
implementation(enforcedPlatform(libsCatalog.findLibrary("kotlin-bom").get()))
implementation(libsCatalog.findLibrary("logbackClassic").get())
runtimeOnly(libsCatalog.findLibrary("log4j-api-slf4j").get())
}
tasks.withType<GenerateModuleMetadata>().configureEach {
// Suppress the error about publishing dependencies to enforced platforms, which Gradle overzealously considers to
// be a mistake, although it is completely fine to publish JARs for CLIs to Maven Central as a convenient
// distribution channel for programmatic use.
suppressedValidationErrors.add("enforced-platform")
}
tasks.named<BuildNativeImageTask>("nativeCompile") {
doFirst {
// Gradle's "Copy" task cannot handle symbolic links, see https://github.com/gradle/gradle/issues/3982. That is
// why links contained in the GraalVM distribution archive get broken during provisioning and are replaced by
// empty files. Address this by recreating the links in the toolchain directory.
val graalvmHomeDir = System.getenv("GRAALVM_HOME")?.let { File(it) }
val toolchainDir = graalvmHomeDir ?: run {
val nativeImageLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(javaLanguageVersion)
nativeImageCapable = true
}
options.get().javaLauncher = nativeImageLauncher
nativeImageLauncher.get().executablePath.asFile.parentFile.run {
if (name == "bin") parentFile else this
}
}
val toolchainFiles = toolchainDir.walkTopDown().filter { it.isFile }
val emptyFiles = toolchainFiles.filter { it.length() == 0L }
// Find empty toolchain files that are named like other toolchain files and assume these should have been links.
val links = toolchainFiles.mapNotNull { file ->
emptyFiles.singleOrNull { it != file && it.name == file.name }?.let {
file to it
}
}
// Fix up symbolic links.
links.forEach { (target, link) ->
logger.quiet("Fixing up '$link' to link to '$target'.")
if (link.delete()) {
Files.createSymbolicLink(link.toPath(), target.toPath())
} else {
logger.warn("Unable to delete '$link'.")
}
}
}
}
val appJar = tasks.named<Jar>("jar") {
manifest {
// Work around https://github.com/gradle/gradle/issues/32263.
attributes["Main-Class"] = application.mainClass
}
}
val pathingJar by tasks.registering(Jar::class) {
archiveClassifier = "pathing"
manifest {
// Work around the command line length limit on Windows when passing the classpath to Java, see
// https://github.com/gradle/gradle/issues/1989.
attributes["Class-Path"] = configurations.runtimeClasspath.get().sorted().joinToString(" ") { it.name }
}
}
tasks.named<CreateStartScripts>("startScripts") {
classpath = appJar.get().outputs.files + pathingJar.get().outputs.files
doLast {
// Append the plugin directory to the Windows classpath.
val windowsScriptText = windowsScript.readText(Charset.defaultCharset())
windowsScript.writeText(
windowsScriptText.replace(
Regex("(set CLASSPATH=%APP_HOME%\\\\lib\\\\.*)"), "$1;%APP_HOME%\\\\plugin\\\\*"
)
)
// Append the plugin directory to the Unix classpath.
val unixScriptText = unixScript.readText(Charset.defaultCharset())
unixScript.writeText(
unixScriptText.replace(
Regex("(CLASSPATH=\\\$APP_HOME/lib/.*)"), "$1:\\\$APP_HOME/plugin/*"
)
)
}
}
distributions {
main {
contents {
from(pathingJar) {
into("lib")
}
}
}
}
val distTar = tasks.named<Tar>("distTar") {
compression = Compression.GZIP
}
val distZip = tasks.named<Zip>("distZip")
signing {
val signingInMemoryKey: String? by project
val signingInMemoryKeyPassword: String? by project
if (signingInMemoryKey != null && signingInMemoryKeyPassword != null) {
useInMemoryPgpKeys(signingInMemoryKey, signingInMemoryKeyPassword)
sign(distTar.get())
sign(distZip.get())
}
}
tasks.named<JavaExec>("run") {
System.getenv("TERM")?.also {
val mode = it.substringAfter('-', "16color")
environment("FORCE_COLOR" to mode)
}
System.getenv("COLORTERM")?.also {
environment("FORCE_COLOR" to it)
}
}