|
| 1 | +import me.champeau.gradle.japicmp.JapicmpTask |
| 2 | +import me.champeau.gradle.japicmp.JApiCmpWorkerAction |
| 3 | +import org.gradle.api.tasks.bundling.Jar |
| 4 | +import org.gradle.kotlin.dsl.named |
| 5 | +import org.gradle.kotlin.dsl.register |
| 6 | +import java.io.FileNotFoundException |
| 7 | +import java.net.URI |
| 8 | + |
| 9 | +plugins { |
| 10 | + java |
| 11 | +} |
| 12 | + |
| 13 | +val groupPath = project.group.toString().replace('.', '/') |
| 14 | + |
| 15 | +val baselineVersion = project.findProperty("japicmpBaseline")?.toString() ?: run { |
| 16 | + val currentVersion = project.version.toString() |
| 17 | + val currentMajor = currentVersion.substringBefore('.') |
| 18 | + val metadataUrl = "https://repo1.maven.org/maven2/$groupPath/${project.name}/maven-metadata.xml" |
| 19 | + val metadata = try { |
| 20 | + URI.create(metadataUrl).toURL().openStream().bufferedReader().use { it.readText() } |
| 21 | + } catch (_: FileNotFoundException) { |
| 22 | + return@run "" |
| 23 | + } |
| 24 | + val allVersions = """<version>([^<]+)</version>""".toRegex().findAll(metadata) |
| 25 | + .map { it.groupValues[1] } |
| 26 | + .filter { it.startsWith("$currentMajor.") } |
| 27 | + .toList() |
| 28 | + |
| 29 | + allVersions.maxWithOrNull { first, second -> |
| 30 | + val firstComponents = first.drop(currentMajor.length + 1).split('.') |
| 31 | + val secondComponents = second.drop(currentMajor.length + 1).split('.') |
| 32 | + for (i in 0 until minOf(firstComponents.size, secondComponents.size)) { |
| 33 | + val firstVersion = firstComponents[i].toInt() |
| 34 | + val secondVersion = secondComponents[i].toInt() |
| 35 | + firstVersion.compareTo(secondVersion).let { |
| 36 | + if (it != 0) { |
| 37 | + return@maxWithOrNull it |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + firstComponents.size.compareTo(secondComponents.size) |
| 42 | + } ?: "" |
| 43 | +} |
| 44 | + |
| 45 | +val baselineJar = layout.buildDirectory.file(project.provider { |
| 46 | + "japicmp/${project.name}-$baselineVersion.jar" |
| 47 | +}) |
| 48 | + |
| 49 | +val downloadJapicmpBaseline = tasks.register("downloadJapicmpBaseline") { |
| 50 | + group = "verification" |
| 51 | + description = "Download baseline JAR from Maven Central for japicmp comparison." |
| 52 | + onlyIf { baselineVersion.isNotEmpty() } |
| 53 | + outputs.file(baselineJar) |
| 54 | + notCompatibleWithConfigurationCache("downloads from Maven Central") |
| 55 | + |
| 56 | + doLast { |
| 57 | + val downloadUrl = "https://repo1.maven.org/maven2/$groupPath/${project.name}/$baselineVersion/${project.name}-$baselineVersion.jar" |
| 58 | + val dest = baselineJar.get().asFile |
| 59 | + if (!dest.exists()) { |
| 60 | + dest.parentFile.mkdirs() |
| 61 | + runCatching { |
| 62 | + logger.info("Downloading baseline JAR from {}", downloadUrl) |
| 63 | + URI.create(downloadUrl).toURL().openStream().use { |
| 64 | + dest.outputStream().use(it::copyTo) |
| 65 | + } |
| 66 | + logger.info("Baseline JAR downloaded: {}", dest.absolutePath) |
| 67 | + }.onFailure { |
| 68 | + logger.info("Failed to download baseline JAR ({}), skipping check", it.message) |
| 69 | + dest.writeText("") |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +val jarTask = tasks.named<Jar>("jar") |
| 76 | + |
| 77 | +val checkSemver = tasks.register<JapicmpTask>("checkSemver") { |
| 78 | + dependsOn(downloadJapicmpBaseline, jarTask) |
| 79 | + group = "verification" |
| 80 | + description = "Fails on binary-incompatible changes vs $baselineVersion." |
| 81 | + onlyIf { baselineJar.get().asFile.exists() && baselineJar.get().asFile.length() > 0 } |
| 82 | + |
| 83 | + oldArchiveList.add(project.provider { |
| 84 | + JApiCmpWorkerAction.Archive(baselineJar.get().asFile, baselineVersion) |
| 85 | + }) |
| 86 | + newArchiveList.add(project.provider { |
| 87 | + JApiCmpWorkerAction.Archive(jarTask.get().archiveFile.get().asFile, project.version.toString()) |
| 88 | + }) |
| 89 | + |
| 90 | + oldClasspath.from(configurations.runtimeClasspath) |
| 91 | + newClasspath.from(configurations.runtimeClasspath) |
| 92 | + |
| 93 | + onlyBinaryIncompatibleModified = true |
| 94 | + failOnModification = true |
| 95 | + ignoreMissingClasses = true |
| 96 | + |
| 97 | + // Filter Kotlin bytecode idioms that are JVM-public but not part of the API. |
| 98 | + methodExcludes = listOf("*#*\$*(*)") |
| 99 | + fieldExcludes = listOf("*#*\$*") |
| 100 | + classExcludes = listOf("*\$WhenMappings") |
| 101 | + |
| 102 | + txtOutputFile = layout.buildDirectory.file("reports/japicmp/${project.name}.txt") |
| 103 | + htmlOutputFile = layout.buildDirectory.file("reports/japicmp/${project.name}.html") |
| 104 | +} |
| 105 | + |
| 106 | +rootProject.tasks.named("checkSemver") { dependsOn(checkSemver) } |
0 commit comments