-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
352 lines (290 loc) · 12 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
buildscript { repositories { mavenCentral() } }
ext {
// skipping cnf since it is a not a java project
javaProjects = subprojects.findAll { it.name != "cnf" && it.plugins.hasPlugin('biz.aQute.bnd') }
// used to aggregate test coverage reports
testableProjects = javaProjects.findAll{ it.hasProperty('test') }
// directory to gather artifacts
distributionDir = rootProject.file("dist")
// some SpotBugs finding can be ignored / excluded
spotbugsExcludeFilterFile = rootProject.file('gradle/spotbugs/excludeFilter.xml')
// will hold configurations files for invidual formatters
spotlessDir = "$rootDir/gradle/spotless/"
// points to our maven dependencies (stored as GAV)
mavenGAV = "$rootDir/cnf/ext/maven/central.mvn"
}
allprojects {
repositories { mavenCentral() }
apply plugin: com.diffplug.gradle.spotless.SpotlessPlugin
spotless {
format 'bnd', {
target '**/*.bnd', '**/*.bndrun'
indentWithSpaces(4)
endWithNewline()
}
format 'markdown', {
target '**/*.md'
indentWithSpaces(2)
endWithNewline()
}
groovyGradle {
target '*.gradle'
greclipse().configFile(rootProject.file("gradle/spotless/org.kromo.gradleformat.properties"))
indentWithSpaces(4)
endWithNewline()
// helps debugging formatting issues
paddedCell()
}
}
}
// required as jacocoRootReport belongs to root project
apply plugin: 'jacoco'
jacoco { toolVersion = jacoco_version }
apply plugin: "com.dorongold.task-tree"
configure(javaProjects) { project ->
// using bnd as source of truth regarding version (actually defined in "cnf/build.bnd")
version = project.bnd.get("base.version")
// using bnd to get project description from bundle name
description = project.bnd.get("Bundle-Name")
// bnd release also includes Javadoc which has to be built beforehand
release { dependsOn javadoc }
task releaseClean(type: Delete) {
doLast {
// failOnError needed if 'distributionDir' directory does not exist
ant.delete(includeEmptyDirs: 'true', failOnError: 'false') {
// delete only files of _this_ project from distributionDir
fileset(dir: distributionDir, includes: "**/${project.name}/**")
}
}
}
clean { dependsOn releaseClean }
apply plugin: 'jacoco'
jacoco { toolVersion = jacoco_version }
// configure tests to use JUnit5
tasks.withType(Test) {
// use specific version
dependencies { testImplementation('org.junit.jupiter:junit-jupiter:' + junit_version) }
// required to run JUnit5 tests
useJUnitPlatform()
// log only this specific events for tests
testLogging {
events "skipped", "failed"
// enable stacktraces for tests
showExceptions true
showStackTraces true
showCauses true
showStandardStreams true
exceptionFormat org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
stackTraceFilters org.gradle.api.tasks.testing.logging.TestStackTraceFilter.ENTRY_POINT
}
jacocoTestReport {
// requires performed tests if called directly
dependsOn test
additionalSourceDirs.from = files(sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(sourceSets.main.allSource.srcDirs)
classDirectories.from = files(sourceSets.main.output)
reports {
html.enabled = true
xml.enabled = true
csv.enabled = false
}
}
// report is always generated after tests ran
finalizedBy jacocoTestReport
}
apply plugin: 'com.github.spotbugs'
spotbugs {
ignoreFailures = true
effort = 'max'
reportLevel = 'low'
excludeFilter = spotbugsExcludeFilterFile
}
spotbugsMain {
// activate SpotBugs only if there are sources
onlyIf { isNotEmpty(sourceSets.main.java) }
reports {
xml.enabled = false
html.enabled = true
}
}
spotbugsTest {
// activate SpotBugs only if there are sources
onlyIf { isNotEmpty(sourceSets.test.java) }
reports {
xml.enabled = false
html.enabled = true
}
}
apply plugin: 'net.ltgt.errorprone'
dependencies { errorproneJavac("com.google.errorprone:error_prone_core:${errorprone_version}") }
spotless {
java {
trimTrailingWhitespace()
removeUnusedImports()
eclipse("${eclipse_version}").configFile rootProject.file(spotlessDir + "org.kromo.eclipseformat.xml")
importOrderFile rootProject.file(spotlessDir + "org.kromo.importorder")
licenseHeaderFile rootProject.file(spotlessDir + "license.txt")
// helps debugging formatting issues
paddedCell()
}
}
javadoc {
title = "${project.description} - v${version}"
source = sourceSets.main.allJava
classpath = configurations.compileClasspath
options { setMemberLevel JavadocMemberLevel.PROTECTED }
}
task javadocCheck(type: Javadoc) {
source = sourceSets.main.allJava
source += sourceSets.test.allJava
classpath = sourceSets.main.compileClasspath
classpath += sourceSets.test.compileClasspath
// use a non-default destinationDir to avoid interfering with regular javadoc
destinationDir = file("${buildDir}/tmp/checks/javadoc")
// copy most options from regular javadoc task
options { setMemberLevel JavadocMemberLevel.PRIVATE }
}
// the Javadoc check should be included when doing final checks
check { dependsOn javadocCheck }
// before creating Javadoc we want to check all of it
// the dependency must be declared after the declaration of "javadocCheck", so that the task is already "known"
javadoc { dependsOn javadocCheck }
// common configuration for all Javadoc tasks
tasks.withType(Javadoc) {
options {
author = true
charSet = 'UTF-8'
encoding = 'UTF-8'
docEncoding = 'UTF-8'
use = true
splitIndex = true
noIndex = false
noNavBar = false
noTree = false
links "https://docs.oracle.com/javase/8/docs/api/"
}
}
}
def isNotEmpty(SourceDirectorySet sourceDirectorySet) {
return null != sourceDirectorySet && (0 < sourceDirectorySet.size())
}
task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
description = 'Generates an aggregate report from all (testable) subprojects'
dependsOn testableProjects.test
additionalSourceDirs.from = files(testableProjects.sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(testableProjects.sourceSets.main.allSource.srcDirs)
classDirectories.from = files(testableProjects.sourceSets.main.output)
// ensure we care only about projects which have execution data
executionData.from = files(testableProjects.jacocoTestReport.executionData).filter { f -> f.exists() }
reports {
html.enabled = true
xml.enabled = false
csv.enabled = false
}
}
task buildPom {
description 'Compiles GAV depndecies into a root level pom.xml which can be parsed by bnd-agnostic tools'
dependsOn javaProjects.classes
File gavFile = new File(mavenGAV)
inputs.file gavFile
def pomFile = rootProject.file("pom.xml")
outputs.file pomFile
StringBuilder sb = new StringBuilder()
sb.append("<?xml version='1.0' encoding='UTF-8'?>")
sb.append("\n<project")
sb.append("\n xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'")
sb.append("\n xmlns='http://maven.apache.org/POM/4.0.0'")
sb.append("\n xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd'>")
sb.append("\n <modelVersion>4.0.0</modelVersion>")
sb.append("\n")
sb.append("\n <groupId>${rootProject.name}</groupId>")
sb.append("\n <artifactId>dependecies</artifactId>")
sb.append("\n <version>1.0.0</version>")
sb.append("\n <name>${rootProject.name} depedencies</name>")
sb.append("\n")
sb.append("\n <dependencies>")
addGAVFileAsMavenDependencies(sb, gavFile, "compile")
sb.append("\n </dependencies>")
sb.append("\n")
sb.append("\n <build>")
sb.append("\n <plugins>")
sb.append("\n <plugin>")
sb.append("\n <groupId>org.codehaus.mojo</groupId>")
sb.append("\n <artifactId>versions-maven-plugin</artifactId>")
sb.append("\n <version>${versions_maven_plugin_version}</version>")
sb.append("\n <configuration>")
sb.append("\n <!-- Using rules to exclude alpha/beta/milestone/RC versions -->")
// using single quotes since the variable is to be expanded by Maven and NOT Gradle
sb.append('\n <rulesUri>file:///${project.basedir}/version-rules.xml</rulesUri>')
sb.append("\n </configuration>")
sb.append("\n </plugin>")
sb.append("\n </plugins>")
sb.append("\n")
sb.append("\n <defaultGoal>versions:display-dependency-updates</defaultGoal>")
sb.append("\n </build>")
sb.append("\n")
sb.append("\n</project>\n")
pomFile.write sb.toString()
}
// execute task after we were able to compile projects
task classes {
dependsOn javaProjects.classes
finalizedBy buildPom
}
def addGAVFileAsMavenDependencies(StringBuilder sb, File gavFile, String scope) {
String[] gavFileLines = gavFile
int line = 0
StringBuilder errors = new StringBuilder()
for(String gavFileLine in gavFileLines) {
line++
String gavLine = gavFileLine.split("#", 2)[0].trim()
if (!gavLine.isEmpty()) {
if (gavLine.indexOf(":") == -1) {
def msg = "Invalid GAV '${gavLine}', line: ${line}. No colon found! File: ${gavFile}"
errors.append("\n ").append(msg)
continue
}
final String[] gav = gavLine.split(":", -1)
if (gav.length != 3) {
def msg = "Invalid GAV '${gavLine}', line: ${line}. Needs 3 parts, found: " + gav.length + " File: ${gavFile}"
errors.append("\n ").append(msg)
continue
}
String groupId = gav[0].trim()
String artifactId = gav[1].trim()
String version = gav[2].trim()
sb.append("\n <dependency>")
sb.append("\n <groupId>${groupId}</groupId>")
sb.append("\n <artifactId>${artifactId}</artifactId>")
sb.append("\n <version>${version}</version>")
sb.append("\n <scope>${scope}</scope>")
sb.append("\n </dependency>")
}
}
if (0 < errors.length()) {
errors.insert(0, "Failed to parse Maven GAV coordinates, file: ${fileName}")
throw new IllegalArgumentException(errors.toString())
}
}
tasks.named('build') { dependsOn javaProjects.build, jacocoRootReport }
task release {
dependsOn javaProjects.release, jacocoRootReport
doLast {
copy {
from buildDir
include "reports/jacoco/**"
exclude "**/*.xml"
into distributionDir
}
}
}
task releaseClean(type: Delete) {
dependsOn javaProjects.releaseClean
doLast {
// failOnError needed if 'distributionDir' directory does not exist
ant.delete(includeEmptyDirs: 'true', failOnError: 'false') {
fileset(dir: distributionDir, includes: "**/**")
}
}
}
tasks.named('clean') { dependsOn releaseClean }