-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
426 lines (331 loc) · 12.6 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
import com.github.jk1.license.render.CsvReportRenderer
import com.github.jk1.license.render.InventoryHtmlReportRenderer
import com.github.jk1.license.render.InventoryMarkdownReportRenderer
import com.github.jk1.license.render.XmlReportRenderer
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.plugins.ide.idea.model.IdeaLanguageLevel
import org.owasp.dependencycheck.reporting.ReportGenerator.Format
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
plugins {
java
jacoco
id("com.github.jk1.dependency-license-report") version "2.0"
id("org.ajoberstar.grgit") version "4.1.1"
id("org.kordamp.gradle.source-xref") version "0.47.0"
id("org.sonarqube") version "3.3"
id("org.owasp.dependencycheck") version "6.5.1"
id("com.dorongold.task-tree") version "2.1.0"
`maven-publish`
idea
}
// project settings ------------------------------------------------------------
group = "org.jarhc"
version = "1.6-SNAPSHOT"
description = "JarHC - JAR Health Check"
// Java version check ----------------------------------------------------------
// NOTE: Gradle build must run on Java 11, but code must be compiled for Java 8
if (!JavaVersion.current().isJava11Compatible) {
val error = "Build requires Java 11 and does not run on Java ${JavaVersion.current().majorVersion}."
throw GradleException(error)
}
// configuration properties ----------------------------------------------------
// flag to skip unit and integration tests
// command line option: -Pskip.tests
val skipTests: Boolean = project.hasProperty("skip.tests")
// select JMH benchmarks
// to run a single benchmark: -Pbenchmarks=DefaultJavaRuntimeBenchmark
val benchmarks: String = (project.properties["benchmarks"] ?: ".*") as String
// constants -------------------------------------------------------------------
val mainClassName: String = "org.jarhc.Main"
val buildTimestamp: String = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssX").withZone(ZoneId.of("UTC")).format(Instant.now())
val licenseReportPath: String = "${buildDir}/reports/licenses"
val jacocoTestReportDir: String = "${buildDir}/reports/jacoco/test"
val jacocoTestReportXml: String = "${jacocoTestReportDir}/report.xml"
val jacocoIntegrationTestReportDir: String = "${buildDir}/reports/jacoco/integrationTest"
val jacocoIntegrationTestReportXml: String = "${jacocoIntegrationTestReportDir}/report.xml"
// dependencies ----------------------------------------------------------------
repositories {
mavenLocal()
maven {
url = uri("https://repo.maven.apache.org/maven2/")
}
}
dependencies {
// primary dependencies
implementation("org.ow2.asm:asm:9.2")
implementation("org.json:json:20211205")
implementation("org.eclipse.aether:aether-impl:1.1.0")
implementation("org.eclipse.aether:aether-connector-basic:1.1.0")
implementation("org.eclipse.aether:aether-transport-http:1.1.0")
implementation("org.apache.maven:maven-aether-provider:3.3.9")
implementation("org.slf4j:slf4j-api:1.7.32")
implementation("org.slf4j:slf4j-simple:1.7.32")
// fix vulnerabilities in transitive dependencies
// fix CVE-2018-10237 and CVE-2020-8908
implementation("com.google.guava:guava:31.0.1-jre")
// fix CVE-2015-5262 and CVE-2020-13956
implementation("org.apache.httpcomponents:httpclient:4.5.13")
// fix https://github.com/codehaus-plexus/plexus-utils/issues/3
implementation("org.codehaus.plexus:plexus-utils:3.0.24")
// test dependencies
testImplementation("org.junit.jupiter:junit-jupiter:5.8.2")
testImplementation("org.mockito:mockito-core:4.2.0")
testImplementation("org.openjdk.jmh:jmh-core:1.34")
testAnnotationProcessor("org.openjdk.jmh:jmh-generator-annprocess:1.34")
}
// plugin configurations -------------------------------------------------------
// special settings for IntelliJ IDEA
idea {
// NOTE: Gradle build must run on Java 11, but code must be compiled for Java 8
project {
jdkName = "11"
languageLevel = IdeaLanguageLevel(JavaVersion.VERSION_1_8)
}
module {
isDownloadJavadoc = true
isDownloadSources = true
}
}
licenseReport {
outputDir = licenseReportPath
renderers = arrayOf(
InventoryHtmlReportRenderer("licenses.html"),
XmlReportRenderer("licenses.xml"),
CsvReportRenderer("licenses.csv"),
InventoryMarkdownReportRenderer("licenses.md")
)
}
java {
// NOTE: Gradle build must run on Java 11, but code must be compiled for Java 8
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
// automatically package source code as artifact -sources.jar
withSourcesJar()
// automatically package Javadoc as artifact -javadoc.jar
withJavadocJar()
}
jacoco {
toolVersion = "0.8.7"
}
sonarqube {
// documentation: https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-gradle/
properties {
// connection to SonarCloud
property("sonar.host.url", "https://sonarcloud.io")
property("sonar.organization", "smarkwal")
property("sonar.projectKey", "smarkwal_jarhc")
// Git branch
property("sonar.branch.name", getGitBranchName())
// include test coverage results
property("sonar.java.coveragePlugin", "jacoco")
property("sonar.coverage.jacoco.xmlReportPaths", "${jacocoTestReportXml},${jacocoIntegrationTestReportXml}")
}
}
dependencyCheck {
// documentation: https://jeremylong.github.io/DependencyCheck/dependency-check-gradle/configuration.html
// settings
format = Format.ALL
skipTestGroups = false
outputDirectory = "${buildDir}/reports/dependency-check"
// path to database directory
data.directory = "${projectDir}/dependency-check"
// disable .NET Assembly Analyzer (fix for unexpected build exception)
analyzers.assemblyEnabled = false
}
config {
docs {
sourceXref {
// documentation: https://kordamp.org/kordamp-gradle-plugins/#_org_kordamp_gradle_sourcexref
windowTitle = "JarHC $version"
docTitle = "JarHC $version"
}
}
}
publishing {
publications.create<MavenPublication>("maven") {
from(components["java"])
}
}
// tasks -----------------------------------------------------------------------
tasks {
processResources {
// replace placeholders in resources
// (see src/main/resources/jarhc.properties)
expand(
"version" to project.version,
"timestamp" to buildTimestamp
)
}
jar {
// set Main-Class in MANIFEST.MF
manifest {
attributes["Main-Class"] = mainClassName
}
// add LICENSE to JAR file
from("LICENSE")
}
test {
// exclude integration tests
filter {
excludeTestsMatching("*IT")
}
}
check {
dependsOn(integrationTest)
}
jacocoTestReport {
// generate HTML, XML and CSV report
reports {
html.required.set(true)
html.outputLocation.set(file("${jacocoTestReportDir}/html"))
xml.required.set(true)
xml.outputLocation.set(file(jacocoTestReportXml))
csv.required.set(true)
csv.outputLocation.set(file("${jacocoTestReportDir}/report.csv"))
}
}
assemble {
dependsOn(jarWithDeps, libsZip, testJar, testLibsZip, "sourceXrefJar")
}
}
val testJar = task("testJar", type = Jar::class) {
group = "build"
description = "Assembles a jar archive containing the test classes."
// compile test classes first
dependsOn(tasks.testClasses)
// append classifier "-tests"
archiveClassifier.set("tests")
// include compiled test classes
from(sourceSets.test.get().output)
}
val libsZip = task("libsZip", type = Zip::class) {
group = "build"
description = "Assembles a zip archive containing the runtime dependencies."
// append classifier "-tests"
archiveClassifier.set("libs")
// create archive in "libs" folder (instead of "distributions")
destinationDirectory.set(file("${buildDir}/libs"))
// include all runtime dependencies
from(configurations.runtimeClasspath)
}
val testLibsZip = task("testLibsZip", type = Zip::class) {
group = "build"
description = "Assembles a zip archive containing the test dependencies."
// append classifier "-tests"
archiveClassifier.set("test-libs")
// create archive in "libs" folder (instead of "distributions")
destinationDirectory.set(file("${buildDir}/libs"))
// include all test dependencies
from(configurations.testRuntimeClasspath)
}
val integrationTest = task("integrationTest", type = Test::class) {
group = "verification"
description = "Runs the integration test suite."
// include only integration tests
filter {
includeTestsMatching("*IT")
}
// run integration tests after unit tests
mustRunAfter(tasks.test)
}
// common settings for all test tasks
tasks.withType<Test> {
// skip tests if property "skip.tests" is set
onlyIf { !skipTests }
// use JUnit 5
useJUnitPlatform()
// settings
maxHeapSize = "1G"
// test task output
testLogging {
events = mutableSetOf(
// TestLogEvent.STARTED,
// TestLogEvent.PASSED,
TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_OUT,
TestLogEvent.STANDARD_ERROR
)
showStandardStreams = true
exceptionFormat = TestExceptionFormat.SHORT
showExceptions = true
showCauses = true
showStackTraces = true
}
}
val jacocoIntegrationTestReport = task("jacocoIntegrationTestReport", type = JacocoReport::class) {
group = "verification"
description = "Generates code coverage report for the integration test task."
// run integration tests first
// note: this task is skipped if integration tests have not been executed
mustRunAfter(integrationTest)
// get JaCoCo data from integration tests
executionData.from("${buildDir}/jacoco/integrationTest.exec")
// set paths to source and class files
sourceDirectories.from(sourceSets.main.get().java) // src/main/java
classDirectories.from(sourceSets.main.get().output) // build/classes/java/main
// configure reports
reports {
html.required.set(true)
html.outputLocation.set(file("${jacocoIntegrationTestReportDir}/html"))
xml.required.set(true)
xml.outputLocation.set(file(jacocoIntegrationTestReportXml))
csv.required.set(true)
csv.outputLocation.set(file("${jacocoIntegrationTestReportDir}/report.csv"))
}
}
val runBenchmarks = task("runBenchmarks", type = JavaExec::class) {
group = "verification"
description = "Runs JMH benchmarks."
// settings
// documentation: https://github.com/guozheng/jmh-tutorial/blob/master/README.md
classpath = tasks.test.get().classpath
mainClass.set("org.openjdk.jmh.Main")
args = listOf(benchmarks, "-jvmArgs", "-Xms1G -Xmx2G")
}
val jarWithDeps = task("jar-with-deps", type = Jar::class) {
group = "build"
description = "Assembles a fat/uber jar archive with all runtime dependencies."
// make sure that license report has been generated
dependsOn(tasks.generateLicenseReport)
// append classifier "-with-deps"
archiveClassifier.set("with-deps")
// set Main-Class in MANIFEST.MF
manifest {
attributes["Main-Class"] = mainClassName
}
// include all files from all runtime dependencies
from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
// include license report
from(licenseReportPath) {
into("META-INF/licenses")
}
exclude(
// exclude module-info files
"module-info.class",
// exclude license files
"META-INF/LICENSE", "META-INF/LICENSE.txt",
"META-INF/NOTICE", "META-INF/NOTICE.txt",
"META-INF/DEPENDENCIES",
// exclude signature files
"META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA"
)
// exclude duplicates
duplicatesStrategy = DuplicatesStrategy.EXCLUDE // alternative: WARN
with(tasks.jar.get() as CopySpec)
}
tasks.withType<JavaCompile> {
options.encoding = "ASCII"
}
tasks.sonarqube {
// run all tests and generate JaCoCo XML reports
dependsOn(tasks.test, integrationTest, tasks.jacocoTestReport, jacocoIntegrationTestReport)
}
// helper functions ------------------------------------------------------------
fun getGitBranchName(): String {
return grgit.branch.current().name
}