Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.DependencyResolveDetails;
import org.gradle.api.artifacts.ModuleVersionSelector;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
Expand Down Expand Up @@ -62,11 +63,27 @@ public Class<? extends Plugin<? extends Project>> getPluginClass() {
public void execute(Project project) {
ProtobufExtension protobuf = project.getExtensions().getByType(ProtobufExtension.class);
protobuf.protoc(this::configureProtoc);
protobuf.plugins(this::configurePlugins);
protobuf.generateProtoTasks(this::configureGenerateProtoTasks);
project.getConfigurations()
.named(this::isProtobufToolsLocator)
.configureEach((configuration) -> configureProtobufToolsLocator(project, configuration));
// gRPC plugin configuration is deferred so we can check if gRPC is actually
// declared as a dependency before registering the protoc-gen-grpc-java artifact.
// Without this guard, Gradle would try to resolve io.grpc:protoc-gen-grpc-java:null
// for projects that use protobuf but not gRPC.
project.afterEvaluate((p) -> {
if (hasGrpcDependency(p)) {
protobuf.plugins(this::configurePlugins);
protobuf.generateProtoTasks(this::configureGenerateProtoTasks);
}
});
}

private boolean hasGrpcDependency(Project project) {
return project.getConfigurations()
.stream()
.flatMap((configuration) -> configuration.getDependencies().stream())
.map(Dependency::getGroup)
.anyMatch("io.grpc"::equals);
}

private void configureProtoc(ExecutableLocator protoc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ void usesVersionOfProtocDependencyWhenSpecified() {
void usesVersionOfGrpcPluginDependencyWhenSpecified() {
assertThat(this.gradleBuild.build("dependencies", "--configuration", "protobufToolsLocator_grpc").getOutput())
.contains("io.grpc:protoc-gen-grpc-java:1.78.0");
}

@TestTemplate
void doesNotConfigureGrpcWhenGrpcIsNotPresent() {
assertThat(this.gradleBuild.build("grpcNotConfigured").getOutput())
.contains("grpc tools locator present: false");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2012-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}

apply plugin: 'com.google.protobuf'

group = 'com.example'
version = '0.0.1'

repositories {
mavenCentral()
}

dependencies {
implementation("com.google.protobuf:protobuf-java:4.34.0")
}

tasks.register("grpcNotConfigured") {
doFirst {
def hasGrpcToolsLocator = project.configurations.names.any { it == "protobufToolsLocator_grpc" }
println "grpc tools locator present: $hasGrpcToolsLocator"
}
}
3 changes: 0 additions & 3 deletions module/spring-boot-test-classic-modules/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ dependencies {
api(project(":module:spring-boot-graphql-test")) {
transitive = false
}
api(project(":module:spring-boot-grpc-test")) {
transitive = false
}
api(project(":module:spring-boot-jdbc-test")) {
transitive = false
}
Expand Down