| 1 |
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar |
| 2 |
import junitbuild.java.ModuleCompileOptions |
| 3 |
import junitbuild.java.ModulePathArgumentProvider |
| 4 |
import junitbuild.java.PatchModuleArgumentProvider |
| 5 |
|
| 6 |
plugins { |
| 7 |
`java-library` |
| 8 |
eclipse |
| 9 |
idea |
| 10 |
checkstyle |
| 11 |
id("junitbuild.base-conventions") |
| 12 |
id("junitbuild.build-parameters") |
| 13 |
id("junitbuild.jacoco-java-conventions") |
| 14 |
} |
| 15 |
|
| 16 |
val mavenizedProjects: List<Project> by rootProject.extra |
| 17 |
val modularProjects: List<Project> by rootProject.extra |
| 18 |
val buildDate: String by rootProject.extra |
| 19 |
val buildTime: String by rootProject.extra |
| 20 |
val buildRevision: Any by rootProject.extra |
| 21 |
|
| 22 |
val extension = extensions.create<JavaLibraryExtension>("javaLibrary") |
| 23 |
|
| 24 |
val moduleSourceDir = layout.projectDirectory.dir("src/module/$javaModuleName") |
| 25 |
val combinedModuleSourceDir = layout.buildDirectory.dir("module") |
| 26 |
val moduleOutputDir = layout.buildDirectory.dir("classes/java/module") |
| 27 |
val javaVersion = JavaVersion.current() |
| 28 |
|
| 29 |
eclipse { |
| 30 |
jdt { |
| 31 |
file { |
| 32 |
// Set properties for org.eclipse.jdt.core.prefs |
| 33 |
withProperties { |
| 34 |
// Configure Eclipse projects with -parameters compiler flag. |
| 35 |
setProperty("org.eclipse.jdt.core.compiler.codegen.methodParameters", "generate") |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
java { |
| 42 |
modularity.inferModulePath = false |
| 43 |
} |
| 44 |
|
| 45 |
if (project in mavenizedProjects) { |
| 46 |
|
| 47 |
apply(plugin = "junitbuild.publishing-conventions") |
| 48 |
apply(plugin = "junitbuild.osgi-conventions") |
| 49 |
|
| 50 |
java { |
| 51 |
withJavadocJar() |
| 52 |
withSourcesJar() |
| 53 |
} |
| 54 |
|
| 55 |
tasks.javadoc { |
| 56 |
options { |
| 57 |
memberLevel = JavadocMemberLevel.PROTECTED |
| 58 |
header = project.name |
| 59 |
encoding = "UTF-8" |
| 60 |
locale = "en" |
| 61 |
(this as StandardJavadocDocletOptions).apply { |
| 62 |
addBooleanOption("Xdoclint:all,-missing,-reference", true) |
| 63 |
addBooleanOption("XD-Xlint:none", true) |
| 64 |
addBooleanOption("html5", true) |
| 65 |
addMultilineStringsOption("tag").value = listOf( |
| 66 |
"apiNote:a:API Note:", |
| 67 |
"implNote:a:Implementation Note:" |
| 68 |
) |
| 69 |
use(true) |
| 70 |
noTimestamp(true) |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
tasks.named<Jar>("javadocJar").configure { |
| 76 |
from(tasks.javadoc.map { File(it.destinationDir, "element-list") }) { |
| 77 |
// For compatibility with older tools, e.g. NetBeans 11 |
| 78 |
rename { "package-list" } |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
tasks.named<Jar>("sourcesJar").configure { |
| 83 |
from(moduleSourceDir) { |
| 84 |
include("module-info.java") |
| 85 |
} |
| 86 |
duplicatesStrategy = DuplicatesStrategy.EXCLUDE |
| 87 |
} |
| 88 |
|
| 89 |
pluginManager.withPlugin("java-test-fixtures") { |
| 90 |
val javaComponent = components["java"] as AdhocComponentWithVariants |
| 91 |
javaComponent.withVariantsFromConfiguration(configurations["testFixturesApiElements"]) { skip() } |
| 92 |
javaComponent.withVariantsFromConfiguration(configurations["testFixturesRuntimeElements"]) { skip() } |
| 93 |
} |
| 94 |
|
| 95 |
configure<PublishingExtension> { |
| 96 |
publications { |
| 97 |
named<MavenPublication>("maven") { |
| 98 |
from(components["java"]) |
| 99 |
versionMapping { |
| 100 |
allVariants { |
| 101 |
fromResolutionResult() |
| 102 |
} |
| 103 |
} |
| 104 |
pom { |
| 105 |
description = provider { "Module \"${project.name}\" of JUnit 5." } |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
} else { |
| 112 |
tasks { |
| 113 |
jar { |
| 114 |
enabled = false |
| 115 |
} |
| 116 |
javadoc { |
| 117 |
enabled = false |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
tasks.withType<AbstractArchiveTask>().configureEach { |
| 123 |
isPreserveFileTimestamps = false |
| 124 |
isReproducibleFileOrder = true |
| 125 |
dirMode = Integer.parseInt("0755", 8) |
| 126 |
fileMode = Integer.parseInt("0644", 8) |
| 127 |
} |
| 128 |
|
| 129 |
normalization { |
| 130 |
runtimeClasspath { |
| 131 |
metaInf { |
| 132 |
// Ignore inconsequential JAR manifest attributes such as timestamps and the commit checksum. |
| 133 |
// This is used when checking whether runtime classpaths, e.g. of test tasks, have changed and |
| 134 |
// improves cacheability of such tasks. |
| 135 |
ignoreAttribute("Built-By") |
| 136 |
ignoreAttribute("Build-Date") |
| 137 |
ignoreAttribute("Build-Time") |
| 138 |
ignoreAttribute("Build-Revision") |
| 139 |
ignoreAttribute("Created-By") |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
val allMainClasses by tasks.registering { |
| 145 |
dependsOn(tasks.classes) |
| 146 |
} |
| 147 |
|
| 148 |
val prepareModuleSourceDir by tasks.registering(Sync::class) { |
| 149 |
from(moduleSourceDir) |
| 150 |
from(sourceSets.matching { it.name.startsWith("main") }.map { it.allJava }) |
| 151 |
into(combinedModuleSourceDir.map { it.dir(javaModuleName) }) |
| 152 |
duplicatesStrategy = DuplicatesStrategy.EXCLUDE |
| 153 |
} |
| 154 |
|
| 155 |
val compileModule by tasks.registering(JavaCompile::class) { |
| 156 |
dependsOn(allMainClasses) |
| 157 |
enabled = project in modularProjects |
| 158 |
source = fileTree(combinedModuleSourceDir).builtBy(prepareModuleSourceDir) |
| 159 |
destinationDirectory = moduleOutputDir |
| 160 |
sourceCompatibility = "9" |
| 161 |
targetCompatibility = "9" |
| 162 |
classpath = files() |
| 163 |
options.release = 9 |
| 164 |
options.compilerArgs.addAll(listOf( |
| 165 |
// Suppress warnings for automatic modules: org.apiguardian.api, org.opentest4j |
| 166 |
"-Xlint:all,-requires-automatic,-requires-transitive-automatic", |
| 167 |
"-Werror", // Terminates compilation when warnings occur. |
| 168 |
"--module-version", "${project.version}", |
| 169 |
)) |
| 170 |
|
| 171 |
val moduleOptions = objects.newInstance(ModuleCompileOptions::class) |
| 172 |
extensions.add("moduleOptions", moduleOptions) |
| 173 |
moduleOptions.modulePath.from(configurations.compileClasspath) |
| 174 |
|
| 175 |
options.compilerArgumentProviders.add(objects.newInstance(ModulePathArgumentProvider::class, project, combinedModuleSourceDir, modularProjects).apply { |
| 176 |
modulePath.from(moduleOptions.modulePath) |
| 177 |
}) |
| 178 |
options.compilerArgumentProviders.addAll(modularProjects.map { objects.newInstance(PatchModuleArgumentProvider::class, project, it) }) |
| 179 |
|
| 180 |
modularity.inferModulePath = false |
| 181 |
|
| 182 |
doFirst { |
| 183 |
options.allCompilerArgs.forEach { |
| 184 |
logger.info(it) |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
tasks.withType<Jar>().configureEach { |
| 190 |
from(rootDir) { |
| 191 |
include("LICENSE.md", "LICENSE-notice.md") |
| 192 |
into("META-INF") |
| 193 |
} |
| 194 |
val suffix = archiveClassifier.getOrElse("") |
| 195 |
if (suffix.isBlank() || this is ShadowJar) { |
| 196 |
dependsOn(allMainClasses, compileModule) |
| 197 |
from(moduleOutputDir.map { it.dir(javaModuleName) }) { |
| 198 |
include("module-info.class") |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
tasks.jar { |
| 204 |
manifest { |
| 205 |
attributes( |
| 206 |
"Created-By" to (buildParameters.manifest.createdBy.orNull |
| 207 |
?: "${System.getProperty("java.version")} (${System.getProperty("java.vendor")} ${System.getProperty("java.vm.version")})"), |
| 208 |
"Built-By" to buildParameters.manifest.builtBy.orElse("JUnit Team"), |
| 209 |
"Build-Date" to buildDate, |
| 210 |
"Build-Time" to buildTime, |
| 211 |
"Build-Revision" to buildRevision, |
| 212 |
"Specification-Title" to project.name, |
| 213 |
"Specification-Version" to (project.version as String).substringBefore('-'), |
| 214 |
"Specification-Vendor" to "junit.org", |
| 215 |
"Implementation-Title" to project.name, |
| 216 |
"Implementation-Version" to project.version, |
| 217 |
"Implementation-Vendor" to "junit.org" |
| 218 |
) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
tasks.withType<ShadowJar>().configureEach { |
| 223 |
outputs.doNotCacheIf("Shadow jar contains a Manifest with Build-Time") { true } |
| 224 |
} |
| 225 |
|
| 226 |
tasks.withType<JavaCompile>().configureEach { |
| 227 |
options.encoding = "UTF-8" |
| 228 |
} |
| 229 |
|
| 230 |
tasks.compileJava { |
| 231 |
// See: https://docs.oracle.com/en/java/javase/12/tools/javac.html |
| 232 |
options.compilerArgs.addAll(listOf( |
| 233 |
"-Xlint:all", // Enables all recommended warnings. |
| 234 |
"-Werror" // Terminates compilation when warnings occur. |
| 235 |
)) |
| 236 |
} |
| 237 |
|
| 238 |
tasks.compileTestJava { |
| 239 |
// See: https://docs.oracle.com/en/java/javase/12/tools/javac.html |
| 240 |
options.compilerArgs.addAll(listOf( |
| 241 |
"-Xlint", // Enables all recommended warnings. |
| 242 |
"-Xlint:-overrides", // Disables "method overrides" warnings. |
| 243 |
"-Werror", // Terminates compilation when warnings occur. |
| 244 |
"-parameters" // Generates metadata for reflection on method parameters. |
| 245 |
)) |
| 246 |
} |
| 247 |
|
| 248 |
afterEvaluate { |
| 249 |
configurations { |
| 250 |
apiElements { |
| 251 |
attributes { |
| 252 |
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, extension.mainJavaVersion.majorVersion.toInt()) |
| 253 |
} |
| 254 |
} |
| 255 |
runtimeElements { |
| 256 |
attributes { |
| 257 |
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, extension.mainJavaVersion.majorVersion.toInt()) |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
tasks { |
| 262 |
compileJava { |
| 263 |
if (extension.configureRelease) { |
| 264 |
options.release = extension.mainJavaVersion.majorVersion.toInt() |
| 265 |
} else { |
| 266 |
sourceCompatibility = extension.mainJavaVersion.majorVersion |
| 267 |
targetCompatibility = extension.mainJavaVersion.majorVersion |
| 268 |
} |
| 269 |
} |
| 270 |
compileTestJava { |
| 271 |
if (extension.configureRelease) { |
| 272 |
options.release = extension.testJavaVersion.majorVersion.toInt() |
| 273 |
} else { |
| 274 |
sourceCompatibility = extension.testJavaVersion.majorVersion |
| 275 |
targetCompatibility = extension.testJavaVersion.majorVersion |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
pluginManager.withPlugin("groovy") { |
| 280 |
tasks.named<GroovyCompile>("compileGroovy").configure { |
| 281 |
// Groovy compiler does not support the --release flag. |
| 282 |
sourceCompatibility = extension.mainJavaVersion.majorVersion |
| 283 |
targetCompatibility = extension.mainJavaVersion.majorVersion |
| 284 |
} |
| 285 |
tasks.named<GroovyCompile>("compileTestGroovy").configure { |
| 286 |
// Groovy compiler does not support the --release flag. |
| 287 |
sourceCompatibility = extension.testJavaVersion.majorVersion |
| 288 |
targetCompatibility = extension.testJavaVersion.majorVersion |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
checkstyle { |
| 294 |
toolVersion = requiredVersionFromLibs("checkstyle") |
| 295 |
configDirectory = rootProject.layout.projectDirectory.dir("gradle/config/checkstyle") |
| 296 |
} |
| 297 |
|
| 298 |
tasks { |
| 299 |
checkstyleMain { |
| 300 |
config = resources.text.fromFile(checkstyle.configDirectory.file("checkstyleMain.xml")) |
| 301 |
} |
| 302 |
checkstyleTest { |
| 303 |
config = resources.text.fromFile(checkstyle.configDirectory.file("checkstyleTest.xml")) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
pluginManager.withPlugin("java-test-fixtures") { |
| 308 |
tasks.named<Checkstyle>("checkstyleTestFixtures") { |
| 309 |
config = resources.text.fromFile(checkstyle.configDirectory.file("checkstyleTest.xml")) |
| 310 |
} |
| 311 |
tasks.named<JavaCompile>("compileTestFixturesJava") { |
| 312 |
options.release = extension.testJavaVersion.majorVersion.toInt() |
| 313 |
} |
| 314 |
} |