Merge pull request #50405 from DragonFSKY

Closes gh-50405

* pr/50405:
  Align protoc version with protobuf-java
  Simplify ProtobufPluginAction `versionFromRuntimeClasspath`
This commit is contained in:
Phillip Webb
2026-05-12 09:09:57 -07:00
3 changed files with 23 additions and 6 deletions
@@ -33,7 +33,6 @@ import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencyResolveDetails;
import org.gradle.api.artifacts.ModuleVersionSelector;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.artifacts.result.DependencyResult;
import org.gradle.api.artifacts.result.ResolvedComponentResult;
import org.jspecify.annotations.Nullable;
@@ -42,6 +41,7 @@ import org.jspecify.annotations.Nullable;
* applied.
*
* @author Andy Wilkinson
* @author Dongliang Xie
*/
final class ProtobufPluginAction implements PluginApplicationAction {
@@ -50,7 +50,7 @@ final class ProtobufPluginAction implements PluginApplicationAction {
private static final Dependency grpcDependency = new Dependency("io.grpc", "protoc-gen-grpc-java");
private static final List<VersionAlignment> versionAlignment = List.of(
protocDependency.alignVersionWith("com.google.protobuf", "protobuf-java-util"),
protocDependency.alignVersionWith("com.google.protobuf", "protobuf-java"),
grpcDependency.alignVersionWith("io.grpc", "grpc-util"));
@Override
@@ -141,9 +141,8 @@ final class ProtobufPluginAction implements PluginApplicationAction {
.getByName("runtimeClasspath")
.getIncoming()
.getResolutionResult()
.getAllDependencies()
.getAllComponents()
.stream()
.map(DependencyResult::getFrom)
.map(ResolvedComponentResult::getId)
.filter(ModuleComponentIdentifier.class::isInstance)
.map(ModuleComponentIdentifier.class::cast)
@@ -16,6 +16,10 @@
package org.springframework.boot.gradle.plugin;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.junit.jupiter.api.TestTemplate;
import org.springframework.boot.gradle.junit.GradleCompatibility;
@@ -27,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Integration tests for {@link ProtobufPluginAction}.
*
* @author Andy Wilkinson
* @author Dongliang Xie
*/
@GradleCompatibility
class ProtobufPluginActionIntegrationTests {
@@ -59,6 +64,20 @@ class ProtobufPluginActionIntegrationTests {
.contains("com.google.protobuf:protoc:null -> 4.34.0");
}
@TestTemplate
void generatesProtoSourcesWhenProtobufJavaProvidesProtocVersion() throws IOException {
File proto = new File(this.gradleBuild.getProjectDir(), "src/main/proto/example.proto");
proto.getParentFile().mkdirs();
Files.writeString(proto.toPath(), """
syntax = "proto3";
option java_package = "com.example";
message Example {
string name = 1;
}
""");
this.gradleBuild.build("generateProto");
}
@TestTemplate
void alignsVersionOfGrpcDependency() {
assertThat(this.gradleBuild.build("dependencies", "--configuration", "protobufToolsLocator_grpc").getOutput())
@@ -29,7 +29,7 @@ repositories {
}
dependencies {
implementation("com.google.protobuf:protobuf-java-util:4.34.0")
implementation("com.google.protobuf:protobuf-java:4.34.0")
implementation("io.grpc:grpc-util:1.79.0")
}
@@ -60,4 +60,3 @@ tasks.register("generateProtoTasksGrpcPluginOptions") {
tasks.withType(com.google.protobuf.gradle.GenerateProtoTask).each { println "${it.name}: ${it.pluginsForCaching.collect { it.options }}" }
}
}