diff --git a/build.gradle b/build.gradle index ccb39c888fc..c63e12570d5 100644 --- a/build.gradle +++ b/build.gradle @@ -489,3 +489,7 @@ allprojects { } } } + +configure(projectsWithFlags('java', 'publish')) { + failOnVersionConflict(libs.protobuf.java) +} diff --git a/gradle/scripts/lib/common-dependencies.gradle b/gradle/scripts/lib/common-dependencies.gradle index 19eaf73c5b2..df9e8bb5999 100644 --- a/gradle/scripts/lib/common-dependencies.gradle +++ b/gradle/scripts/lib/common-dependencies.gradle @@ -21,6 +21,7 @@ allprojects { p -> managedVersions = getManagedVersions(p.rootProject) findLibrary = this.&findLibrary.curry(p.rootProject) findPlugin = this.&findPlugin.curry(p.rootProject) + failOnVersionConflict = this.&failOnVersionConflict.curry(p) } } @@ -331,3 +332,31 @@ final class GentlePlainTextReporter implements Reporter { return delegate.getFileExtension() } } + +/** + * A custom version of failOnVersionConflict which can limit which dependencies should be checked for conflict. + * Heavily inspired https://github.com/gradle/gradle/issues/8813. + */ +static def failOnVersionConflict(Project project, ProviderConvertible providerConvertible) { + return failOnVersionConflict(project, providerConvertible.asProvider()) +} + +static def failOnVersionConflict(Project project, Provider dependencyProvider) { + if (!dependencyProvider.isPresent()) { + return + } + def targetDependency = dependencyProvider.get() + project.configurations.configureEach { config -> + incoming.afterResolve { + resolutionResult.allComponents {ResolvedComponentResult result -> + if (selectionReason.conflictResolution && moduleVersion != null) { + // we don't care if the selected version is the one specified in dependencies.toml + if (targetDependency.module == moduleVersion.module && targetDependency.version != moduleVersion.version) { + throw new IllegalStateException("Project '${project.name}:${config.name}' resolution failed " + + "for '${targetDependency.module}' with '${getSelectionReason()}") + } + } + } + } + } +} diff --git a/grpc/build.gradle b/grpc/build.gradle index b898b81065e..93c750457d9 100644 --- a/grpc/build.gradle +++ b/grpc/build.gradle @@ -15,7 +15,9 @@ dependencies { optionalImplementation libs.grpc.kotlin optionalImplementation libs.kotlin.coroutines.core - api libs.protobuf.jackson + api(libs.protobuf.jackson) { + exclude group: 'com.google.protobuf', module: 'protobuf-java' + } testImplementation(libs.gax.grpc) { exclude group: 'com.google.protobuf', module: 'protobuf-java'