Skip to content

Commit a0a15d5

Browse files
committed
Add a consistency check task for defined versions
1 parent 02cf019 commit a0a15d5

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import org.example.gradle.tasks.JavaVersionConsistencyCheck
2+
3+
plugins { id("java-platform") }
4+
5+
tasks.register<JavaVersionConsistencyCheck>("checkVersionConsistency") {
6+
group = JavaBasePlugin.VERIFICATION_GROUP
7+
definedVersions = provider {
8+
configurations["api"].dependencyConstraints.associate { "${it.group}:${it.name}" to it.version!! }
9+
}
10+
aggregatedClasspath = provider { configurations["mainRuntimeClasspath"].incoming.resolutionResult.allComponents }
11+
reportFile = layout.buildDirectory.file("reports/version-consistency.txt")
12+
}
13+
14+
tasks.named("qualityCheck") { dependsOn(tasks.named("checkVersionConsistency")) }
15+
16+
tasks.named("qualityGate") { dependsOn(tasks.named("checkVersionConsistency")) }

gradle/plugins/src/main/kotlin/org.example.gradle.feature.use-all-catalog-versions.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
plugins { id("org.gradle.java-platform") }
1+
plugins {
2+
id("org.gradle.java-platform")
3+
id("org.example.gradle.base.lifecycle")
4+
}
25

36
// Allow upgrading (transitive) versions via catalog by adding constraints
47
dependencies.constraints {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

gradle/versions/build.gradle.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
plugins {
22
id("org.example.gradle.base.dependency-rules")
3-
id("org.example.gradle.base.lifecycle")
43
id("org.example.gradle.feature.use-all-catalog-versions")
4+
id("org.example.gradle.check.dependency-versions")
55
id("org.example.gradle.check.format-gradle")
66
}
77

@@ -16,3 +16,9 @@ dependencies.constraints {
1616
api(libs.resteasy.core) { version { reject("[5.0.0.Final,)") } }
1717
api(libs.solr.solrj) { version { reject("[8.0.0,)") } } // API changes in 8 require production code changes
1818
}
19+
20+
tasks.checkVersionConsistency {
21+
excludes.add("org.junit.jupiter:junit-jupiter-api") // testing only
22+
excludes.add("org.assertj:assertj-core") // testing only
23+
excludes.add("com.google.code.findbugs:jsr305") // test fixtures only
24+
}

0 commit comments

Comments
 (0)