|
| 1 | +package org.example.gradle.tasks |
| 2 | + |
| 3 | +import org.gradle.api.DefaultTask |
| 4 | +import org.gradle.api.artifacts.component.ModuleComponentIdentifier |
| 5 | +import org.gradle.api.artifacts.result.ResolvedComponentResult |
| 6 | +import org.gradle.api.file.RegularFileProperty |
| 7 | +import org.gradle.api.provider.MapProperty |
| 8 | +import org.gradle.api.provider.SetProperty |
| 9 | +import org.gradle.api.tasks.Input |
| 10 | +import org.gradle.api.tasks.OutputFile |
| 11 | +import org.gradle.api.tasks.TaskAction |
| 12 | + |
| 13 | +abstract class JavaVersionConsistencyCheck : DefaultTask() { |
| 14 | + |
| 15 | + @get:Input abstract val definedVersions: MapProperty<String, String> |
| 16 | + |
| 17 | + @get:Input abstract val aggregatedClasspath: SetProperty<ResolvedComponentResult> |
| 18 | + |
| 19 | + @get:OutputFile abstract val reportFile: RegularFileProperty |
| 20 | + |
| 21 | + @TaskAction |
| 22 | + fun compare() { |
| 23 | + var potentiallyUnusedVersion = "" |
| 24 | + var wrongVersions = "" |
| 25 | + definedVersions.get().forEach { (id, version) -> |
| 26 | + val resolved = |
| 27 | + aggregatedClasspath.get().find { |
| 28 | + val resolvedId = it.id |
| 29 | + resolvedId is ModuleComponentIdentifier && resolvedId.moduleIdentifier.toString() == id |
| 30 | + } |
| 31 | + if (resolved == null) { |
| 32 | + potentiallyUnusedVersion += "Not used in production code: $id:$version\n" |
| 33 | + } else { |
| 34 | + val resolvedVersion = resolved.moduleVersion?.version |
| 35 | + if (resolvedVersion != version) { |
| 36 | + wrongVersions += "Wrong version: $id (declared=$version; used=$resolvedVersion)\n" |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + reportFile.get().asFile.writeText(potentiallyUnusedVersion + wrongVersions) |
| 42 | + |
| 43 | + if (!potentiallyUnusedVersion.isEmpty()) { |
| 44 | + println(potentiallyUnusedVersion) |
| 45 | + } |
| 46 | + if (!wrongVersions.isEmpty()) { |
| 47 | + throw RuntimeException(wrongVersions) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments