/
java-library-conventions.gradle.kts
322 lines (289 loc) · 9.12 KB
/
java-library-conventions.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
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.tasks.PathSensitivity.RELATIVE
plugins {
`java-library`
eclipse
idea
checkstyle
id("java-toolchain-conventions")
}
val mavenizedProjects: List<Project> by rootProject.extra
val modularProjects: List<Project> by rootProject.extra
val buildDate: String by rootProject.extra
val buildTime: String by rootProject.extra
val buildRevision: Any by rootProject.extra
val builtByValue: String by rootProject.extra
val extension = extensions.create<JavaLibraryExtension>("javaLibrary")
val moduleSourceDir = file("src/module/$javaModuleName")
val moduleOutputDir = file("$buildDir/classes/java/module")
val javaVersion = JavaVersion.current()
eclipse {
jdt {
file {
// Set properties for org.eclipse.jdt.core.prefs
withProperties {
// Configure Eclipse projects with -parameters compiler flag.
setProperty("org.eclipse.jdt.core.compiler.codegen.methodParameters", "generate")
}
}
}
}
java {
modularity.inferModulePath.set(false)
}
if (project in mavenizedProjects) {
apply(plugin = "publishing-conventions")
apply(plugin = "osgi-conventions")
java {
withJavadocJar()
withSourcesJar()
}
tasks.javadoc {
options {
memberLevel = JavadocMemberLevel.PROTECTED
header = project.name
encoding = "UTF-8"
locale = "en"
(this as StandardJavadocDocletOptions).apply {
addBooleanOption("Xdoclint:html,syntax", true)
addBooleanOption("html5", true)
// Javadoc 13 removed support for `--no-module-directories`
// https://bugs.openjdk.java.net/browse/JDK-8215580
if (javaVersion.isJava12 && executable == null) {
addBooleanOption("-no-module-directories", true)
}
addMultilineStringsOption("tag").value = listOf(
"apiNote:a:API Note:",
"implNote:a:Implementation Note:"
)
use(true)
noTimestamp(true)
}
}
}
tasks.named<Jar>("javadocJar").configure {
from(tasks.javadoc.map { File(it.destinationDir, "element-list") }) {
// For compatibility with older tools, e.g. NetBeans 11
rename { "package-list" }
}
}
tasks.named<Jar>("sourcesJar").configure {
from(moduleSourceDir) {
include("module-info.java")
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
pluginManager.withPlugin("java-test-fixtures") {
val javaComponent = components["java"] as AdhocComponentWithVariants
javaComponent.withVariantsFromConfiguration(configurations["testFixturesApiElements"]) { skip() }
javaComponent.withVariantsFromConfiguration(configurations["testFixturesRuntimeElements"]) { skip() }
}
configure<PublishingExtension> {
publications {
named<MavenPublication>("maven") {
from(components["java"])
versionMapping {
allVariants {
fromResolutionResult()
}
}
pom {
description.set(provider { "Module \"${project.name}\" of JUnit 5." })
}
}
}
}
} else {
tasks {
jar {
enabled = false
}
javadoc {
enabled = false
}
}
}
normalization {
runtimeClasspath {
metaInf {
// Ignore inconsequential JAR manifest attributes such as timestamps and the commit checksum.
// This is used when checking whether runtime classpaths, e.g. of test tasks, have changed and
// improves cacheability of such tasks.
ignoreAttribute("Built-By")
ignoreAttribute("Build-Date")
ignoreAttribute("Build-Time")
ignoreAttribute("Build-Revision")
ignoreAttribute("Created-By")
}
}
}
val allMainClasses by tasks.registering {
dependsOn(tasks.classes)
}
val compileModule by tasks.registering(JavaCompile::class) {
dependsOn(allMainClasses)
source = fileTree(moduleSourceDir)
destinationDirectory.set(moduleOutputDir)
sourceCompatibility = "9"
targetCompatibility = "9"
classpath = files()
options.release.set(9)
options.compilerArgs.addAll(listOf(
// Suppress warnings for automatic modules: org.apiguardian.api, org.opentest4j
"-Xlint:all,-requires-automatic,-requires-transitive-automatic",
"-Werror", // Terminates compilation when warnings occur.
"--module-version", "${project.version}",
"--module-source-path", files(modularProjects.map { "${it.projectDir}/src/module" }).asPath
))
options.compilerArgumentProviders.add(ModulePathArgumentProvider())
options.compilerArgumentProviders.addAll(modularProjects.map { PatchModuleArgumentProvider(it) })
modularity.inferModulePath.set(false)
}
tasks.withType<Jar>().configureEach {
from(rootDir) {
include("LICENSE.md", "LICENSE-notice.md")
into("META-INF")
}
val suffix = archiveClassifier.getOrElse("")
if (suffix.isBlank() || this is ShadowJar) {
dependsOn(allMainClasses, compileModule)
from("$moduleOutputDir/$javaModuleName") {
include("module-info.class")
}
}
}
tasks.jar {
manifest {
attributes(
"Created-By" to "${System.getProperty("java.version")} (${System.getProperty("java.vendor")} ${System.getProperty("java.vm.version")})",
"Built-By" to builtByValue,
"Build-Date" to buildDate,
"Build-Time" to buildTime,
"Build-Revision" to buildRevision,
"Specification-Title" to project.name,
"Specification-Version" to (project.version as String).substringBefore('-'),
"Specification-Vendor" to "junit.org",
"Implementation-Title" to project.name,
"Implementation-Version" to project.version,
"Implementation-Vendor" to "junit.org"
)
}
}
tasks.withType<JavaCompile>().configureEach {
options.encoding = "UTF-8"
}
tasks.compileJava {
// See: https://docs.oracle.com/en/java/javase/12/tools/javac.html
options.compilerArgs.addAll(listOf(
"-Xlint:all", // Enables all recommended warnings.
"-Werror" // Terminates compilation when warnings occur.
))
}
tasks.compileTestJava {
// See: https://docs.oracle.com/en/java/javase/12/tools/javac.html
options.compilerArgs.addAll(listOf(
"-Xlint", // Enables all recommended warnings.
"-Xlint:-overrides", // Disables "method overrides" warnings.
"-Werror", // Terminates compilation when warnings occur.
"-parameters" // Generates metadata for reflection on method parameters.
))
}
inner class ModulePathArgumentProvider : CommandLineArgumentProvider, Named {
@get:CompileClasspath
val modulePath: Provider<Configuration> = configurations.compileClasspath
override fun asArguments() = listOf("--module-path", modulePath.get().asPath)
@Internal
override fun getName() = "module-path"
}
inner class PatchModuleArgumentProvider(it: Project) : CommandLineArgumentProvider, Named {
@get:Input
val module: String = it.javaModuleName
@get:InputFiles
@get:PathSensitive(RELATIVE)
val patch: Provider<FileCollection> = provider {
if (it == project)
files(sourceSets.matching { it.name.startsWith("main") }.map { it.output })
else
files(it.sourceSets["main"].java.srcDirs)
}
override fun asArguments(): List<String> {
val path = patch.get().filter { it.exists() }.asPath
if (path.isEmpty()) {
return emptyList()
}
return listOf("--patch-module", "$module=$path")
}
@Internal
override fun getName() = "patch-module($module)"
}
afterEvaluate {
configurations {
apiElements {
attributes {
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, extension.mainJavaVersion.majorVersion.toInt())
}
}
runtimeElements {
attributes {
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, extension.mainJavaVersion.majorVersion.toInt())
}
}
}
tasks {
compileJava {
if (extension.configureRelease) {
options.release.set(extension.mainJavaVersion.majorVersion.toInt())
} else {
sourceCompatibility = extension.mainJavaVersion.majorVersion
targetCompatibility = extension.mainJavaVersion.majorVersion
}
}
compileTestJava {
if (extension.configureRelease) {
options.release.set(extension.testJavaVersion.majorVersion.toInt())
} else {
sourceCompatibility = extension.testJavaVersion.majorVersion
targetCompatibility = extension.testJavaVersion.majorVersion
}
}
}
pluginManager.withPlugin("groovy") {
tasks.named<GroovyCompile>("compileGroovy").configure {
if (extension.configureRelease) {
options.release.set(extension.mainJavaVersion.majorVersion.toInt())
} else {
sourceCompatibility = extension.mainJavaVersion.majorVersion
targetCompatibility = extension.mainJavaVersion.majorVersion
}
}
tasks.named<GroovyCompile>("compileTestGroovy").configure {
if (extension.configureRelease) {
options.release.set(extension.testJavaVersion.majorVersion.toInt())
} else {
sourceCompatibility = extension.testJavaVersion.majorVersion
targetCompatibility = extension.testJavaVersion.majorVersion
}
}
}
}
checkstyle {
val libs = project.extensions["libs"] as VersionCatalog
toolVersion = libs.findVersion("checkstyle").get().requiredVersion
configDirectory.set(rootProject.file("src/checkstyle"))
}
tasks {
checkstyleMain {
configFile = rootProject.file("src/checkstyle/checkstyleMain.xml")
}
checkstyleTest {
configFile = rootProject.file("src/checkstyle/checkstyleTest.xml")
}
}
pluginManager.withPlugin("java-test-fixtures") {
tasks.named<Checkstyle>("checkstyleTestFixtures") {
configFile = rootProject.file("src/checkstyle/checkstyleTest.xml")
}
tasks.named<JavaCompile>("compileTestFixturesJava") {
options.release.set(extension.testJavaVersion.majorVersion.toInt())
}
}