forked from oss-review-toolkit/ort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
433 lines (348 loc) · 14.9 KB
/
build.gradle.kts
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
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
* Copyright (C) 2019 Bosch Software Innovations GmbH
*
* 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.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
import io.gitlab.arturbosch.detekt.Detekt
import java.net.URL
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.AbbreviatedObjectId
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.gradle.ext.Gradle
import org.jetbrains.gradle.ext.runConfigurations
import org.jetbrains.gradle.ext.settings
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val detektPluginVersion: String by project
val kotlinPluginVersion: String by project
val jacksonVersion: String by project
val kotestVersion: String by project
val log4jCoreVersion: String by project
val okhttpVersion: String by project
plugins {
kotlin("jvm")
id("com.github.ben-manes.versions")
id("com.github.gmazzo.buildconfig")
id("io.gitlab.arturbosch.detekt")
id("org.barfuin.gradle.taskinfo")
id("org.jetbrains.dokka")
id("org.jetbrains.gradle.plugin.idea-ext")
}
buildscript {
dependencies {
// For some reason "jgitVersion" needs to be declared here instead of globally.
val jgitVersion: String by project
classpath("org.eclipse.jgit:org.eclipse.jgit:$jgitVersion")
}
}
// Only override a default version (which usually is "unspecified"), but not a custom version.
if (version == Project.DEFAULT_VERSION) {
version = Git.open(rootDir).use { git ->
// Make the output exactly match "git describe --abbrev=10 --always --tags --dirty", which is what is used in
// "scripts/docker_build.sh", to make the hash match what JitPack uses.
val abbrevLength = 10
val description = git.describe().setAlways(true).setTags(true).call()
// Manually resolve an abbreviated hash to the custom length until
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=537883 is implemented.
val customDescription = runCatching {
val abbrevObject = AbbreviatedObjectId.fromString(description)
val objects = git.repository.objectDatabase.newReader().use { it.resolve(abbrevObject) }
objects.single().name.take(abbrevLength)
}.getOrDefault(description)
val isDirty = git.status().call().hasUncommittedChanges()
if (isDirty) "$customDescription-dirty" else customDescription
}
}
logger.quiet("Building ORT version $version.")
// Note that Gradle's Java toolchain mechanism cannot be used here as that only applies to the Java version used in
// compile tasks. But already ORT's build scripts, like the compilation of this file itself, depend on Java 11 due to
// the Java target used by some plugins, see e.g. https://github.com/martoe/gradle-svntools-plugin#version-compatibility.
val javaVersion = JavaVersion.current()
if (!javaVersion.isCompatibleWith(JavaVersion.VERSION_11)) {
throw GradleException("At least Java 11 is required, but Java $javaVersion is being used.")
}
idea {
project {
settings {
runConfigurations {
// Disable "condensed" multi-line diffs when running tests from the IDE via Gradle run configurations to
// more easily accept actual results as expected results.
defaults(Gradle::class.java) {
jvmArgs = "-Dkotest.assertions.multi-line-diff=simple"
}
}
}
}
}
extensions.findByName("buildScan")?.withGroovyBuilder {
setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
setProperty("termsOfServiceAgree", "yes")
}
tasks.named<DependencyUpdatesTask>("dependencyUpdates").configure {
val nonFinalQualifiers = listOf(
"alpha", "b", "beta", "cr", "dev", "ea", "eap", "m", "milestone", "pr", "preview", "rc", "\\d{14}"
).joinToString("|", "(", ")")
val nonFinalQualifiersRegex = Regex(".*[.-]$nonFinalQualifiers[.\\d-+]*", RegexOption.IGNORE_CASE)
gradleReleaseChannel = "current"
rejectVersionIf {
candidate.version.matches(nonFinalQualifiersRegex)
}
}
allprojects {
buildscript {
repositories {
mavenCentral()
}
}
repositories {
mavenCentral()
}
apply(plugin = "com.github.gmazzo.buildconfig")
apply(plugin = "io.gitlab.arturbosch.detekt")
// Note: Kotlin DSL cannot directly access configurations that are created by applying a plugin in the very same
// project, thus put configuration names in quotes to leverage lazy lookup.
dependencies {
"detektPlugins"(project(":detekt-rules"))
"detektPlugins"("io.gitlab.arturbosch.detekt:detekt-formatting:$detektPluginVersion")
}
detekt {
// Align the detekt core and plugin versions.
toolVersion = detektPluginVersion
// Only configure differences to the default.
buildUponDefaultConfig = true
config = files("$rootDir/.detekt.yml")
source.from(fileTree(".") { include("*.gradle.kts") }, "src/funTest/kotlin")
basePath = rootProject.projectDir.path
}
tasks.withType<Detekt> detekt@{
dependsOn(":detekt-rules:assemble")
reports {
html.required.set(false)
sarif.required.set(true)
txt.required.set(false)
xml.required.set(false)
}
}
}
subprojects {
version = rootProject.version
if (name == "reporter-web-app") return@subprojects
// Apply core plugins.
apply(plugin = "jacoco")
apply(plugin = "maven-publish")
// Apply third-party plugins.
apply(plugin = "org.jetbrains.kotlin.jvm")
apply(plugin = "org.jetbrains.dokka")
sourceSets.create("funTest") {
kotlin.sourceSets.getByName(name).kotlin.srcDirs("src/funTest/kotlin")
}
// Associate the "funTest" compilation with the "main" compilation to be able to access "internal" objects from
// functional tests.
kotlin.target.compilations.apply {
getByName("funTest").associateWith(getByName(KotlinCompilation.MAIN_COMPILATION_NAME))
}
plugins.withType<JavaLibraryPlugin> {
dependencies {
"testImplementation"(project(":utils:test-utils"))
"testImplementation"("io.kotest:kotest-runner-junit5:$kotestVersion")
"testImplementation"("io.kotest:kotest-assertions-core:$kotestVersion")
"funTestImplementation"(sourceSets["main"].output)
}
configurations["funTestImplementation"].extendsFrom(configurations["testImplementation"])
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinPluginVersion")
}
configurations.all {
// Do not tamper with configurations related to the detekt plugin, for some background information
// https://github.com/detekt/detekt/issues/2501.
if (!name.startsWith("detekt")) {
resolutionStrategy {
// Ensure all OkHttp versions match our version >= 4 to avoid Kotlin vs. Java issues with OkHttp 3.
force("com.squareup.okhttp3:okhttp:$okhttpVersion")
// Ensure all API library versions match our core library version.
force("org.apache.logging.log4j:log4j-api:$log4jCoreVersion")
// Ensure that all transitive versions of Kotlin libraries match our version of Kotlin.
force("org.jetbrains.kotlin:kotlin-reflect:$kotlinPluginVersion")
force("org.jetbrains.kotlin:kotlin-script-runtime:$kotlinPluginVersion")
}
}
}
tasks.withType<KotlinCompile>().configureEach {
val customCompilerArgs = listOf(
"-Xallow-result-return-type",
"-Xopt-in=kotlin.contracts.ExperimentalContracts",
"-Xopt-in=kotlin.io.path.ExperimentalPathApi",
"-Xopt-in=kotlin.time.ExperimentalTime"
)
kotlinOptions {
allWarningsAsErrors = true
jvmTarget = javaVersion.majorVersion
apiVersion = "1.6"
freeCompilerArgs = freeCompilerArgs + customCompilerArgs
}
}
tasks.dokkaHtml.configure {
dokkaSourceSets {
configureEach {
jdkVersion.set(11)
externalDocumentationLink {
val baseUrl = "https://codehaus-plexus.github.io/plexus-containers/plexus-container-default/apidocs"
url.set(URL(baseUrl))
packageListUrl.set(URL("$baseUrl/package-list"))
}
externalDocumentationLink {
val majorMinorVersion = jacksonVersion.split('.').let { "${it[0]}.${it[1]}" }
val baseUrl = "https://fasterxml.github.io/jackson-databind/javadoc/$majorMinorVersion"
url.set(URL(baseUrl))
packageListUrl.set(URL("$baseUrl/package-list"))
}
externalDocumentationLink {
val baseUrl = "https://jakewharton.github.io/DiskLruCache"
url.set(URL(baseUrl))
packageListUrl.set(URL("$baseUrl/package-list"))
}
externalDocumentationLink {
val majorVersion = log4jCoreVersion.substringBefore('.')
val baseUrl = "https://logging.apache.org/log4j/$majorVersion.x/log4j-api/apidocs"
url.set(URL(baseUrl))
packageListUrl.set(URL("$baseUrl/package-list"))
}
}
}
}
val funTest by tasks.registering(Test::class) {
description = "Runs the functional tests."
group = "Verification"
classpath = sourceSets["funTest"].runtimeClasspath
testClassesDirs = sourceSets["funTest"].output.classesDirs
}
tasks.withType<Test>().configureEach {
val testSystemProperties = mutableListOf("gradle.build.dir" to project.buildDir.path)
listOf(
"java.io.tmpdir",
"kotest.assertions.multi-line-diff",
"kotest.tags"
).mapNotNullTo(testSystemProperties) { key ->
System.getProperty(key)?.let { key to it }
}
systemProperties = testSystemProperties.toMap()
testLogging {
events = setOf(TestLogEvent.STARTED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
exceptionFormat = TestExceptionFormat.FULL
}
useJUnitPlatform()
}
// Enable JaCoCo only if a JacocoReport task is in the graph as JaCoCo
// is using "append = true" which disables Gradle's build cache.
gradle.taskGraph.whenReady {
val enabled = allTasks.any { it is JacocoReport }
tasks.withType<Test>().configureEach {
extensions.configure(JacocoTaskExtension::class) {
isEnabled = enabled
}
}
}
tasks.named<JacocoReport>("jacocoTestReport").configure {
reports {
// Enable XML in addition to HTML for CI integration.
xml.required.set(true)
}
}
tasks.register<JacocoReport>("jacocoFunTestReport").configure {
description = "Generates code coverage report for the funTest task."
group = "Reporting"
executionData(funTest.get())
sourceSets(sourceSets["main"])
reports {
// Enable XML in addition to HTML for CI integration.
xml.required.set(true)
}
}
tasks.register("jacocoReport").configure {
description = "Generates code coverage reports for all test tasks."
group = "Reporting"
dependsOn(tasks.withType<JacocoReport>())
}
tasks.named("check").configure {
dependsOn(funTest)
}
tasks.withType<Jar>().configureEach {
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
manifest {
attributes["Implementation-Version"] = project.version
}
}
tasks.register<Jar>("sourcesJar").configure {
archiveClassifier.set("sources")
from(sourceSets["main"].allSource)
}
tasks.register<Jar>("dokkaHtmlJar").configure {
dependsOn(tasks.dokkaHtml)
description = "Assembles a jar archive containing the minimalistic HTML documentation."
group = "Documentation"
archiveClassifier.set("dokka")
from(tasks.dokkaHtml)
}
tasks.register<Jar>("dokkaJavadocJar").configure {
dependsOn(tasks.dokkaJavadoc)
description = "Assembles a jar archive containing the Javadoc documentation."
group = "Documentation"
archiveClassifier.set("javadoc")
from(tasks.dokkaJavadoc)
}
configure<PublishingExtension> {
publications {
create<MavenPublication>(name) {
groupId = "org.ossreviewtoolkit"
from(components["java"])
artifact(tasks["sourcesJar"])
artifact(tasks["dokkaJavadocJar"])
pom {
licenses {
license {
name.set("Apache-2.0")
url.set("https://www.apache.org/licenses/LICENSE-2.0")
}
}
scm {
connection.set("scm:git:https://github.com/oss-review-toolkit/ort.git")
developerConnection.set("scm:git:[email protected]:oss-review-toolkit/ort.git")
tag.set(version.toString())
url.set("https://github.com/oss-review-toolkit/ort")
}
}
}
}
}
}
// Gradle's "dependencies" task selector only executes on a single / the current project [1]. However, sometimes viewing
// all dependencies at once is beneficial, e.g. for debugging version conflict resolution.
// [1]: https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html#sec:listing_dependencies
tasks.register("allDependencies").configure {
val dependenciesTasks = allprojects.map { it.tasks.named("dependencies") }
dependsOn(dependenciesTasks)
// Ensure deterministic output by requiring to run tasks after each other in always the same order.
dependenciesTasks.zipWithNext().forEach { (a, b) ->
b.configure {
mustRunAfter(a)
}
}
}