|
| 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.ListProperty |
| 8 | +import org.gradle.api.provider.MapProperty |
| 9 | +import org.gradle.api.provider.SetProperty |
| 10 | +import org.gradle.api.tasks.Input |
| 11 | +import org.gradle.api.tasks.OutputFile |
| 12 | +import org.gradle.api.tasks.TaskAction |
| 13 | + |
| 14 | +/** Check that all versions declared in a java-platform build.gradle.kts file are actually used. */ |
| 15 | +abstract class JavaVersionConsistencyCheck : DefaultTask() { |
| 16 | + |
| 17 | + /** The versions declared in the build.gradle.kts file. */ |
| 18 | + @get:Input abstract val definedVersions: MapProperty<String, String> |
| 19 | + |
| 20 | + /** The aggregated classpath of all modules using the versions to resolve their dependencies. */ |
| 21 | + @get:Input abstract val aggregatedClasspath: SetProperty<ResolvedComponentResult> |
| 22 | + |
| 23 | + /** |
| 24 | + * List of versions to ignore. This may be needed if versions for components that are not part of the runtime module |
| 25 | + * path of the applications are managed. |
| 26 | + */ |
| 27 | + @get:Input abstract val excludes: ListProperty<String> |
| 28 | + |
| 29 | + /** The report TXT file that will contain the issues found. */ |
| 30 | + @get:OutputFile abstract val reportFile: RegularFileProperty |
| 31 | + |
| 32 | + @TaskAction |
| 33 | + fun compare() { |
| 34 | + var issues = "" |
| 35 | + definedVersions.get().forEach { (id, version) -> |
| 36 | + val resolved = |
| 37 | + aggregatedClasspath.get().find { |
| 38 | + val resolvedId = it.id |
| 39 | + resolvedId is ModuleComponentIdentifier && resolvedId.moduleIdentifier.toString() == id |
| 40 | + } |
| 41 | + if (resolved == null) { |
| 42 | + if (!excludes.get().contains(id)) { |
| 43 | + issues += "Not used: $id:$version\n" |
| 44 | + } |
| 45 | + } else { |
| 46 | + val resolvedVersion = resolved.moduleVersion?.version |
| 47 | + if (resolvedVersion != version) { |
| 48 | + issues += "Wrong version: $id (declared=$version; used=$resolvedVersion)\n" |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + reportFile.get().asFile.writeText(issues) |
| 54 | + |
| 55 | + if (!issues.isEmpty()) { |
| 56 | + throw RuntimeException(issues) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments