Skip to content

Consider using Gradle configurations instead of protobuf extension for easier end-user API #628

Open

Description

Current protobuf-gradle-plugin API requires 25 lines in gradle.build.kts (16 without comments) for the most common trivial case:

protobuf {
protoc {
// The artifact spec for the Protobuf Compiler
artifact = "com.google.protobuf:protoc:3.6.1"
}
plugins {
// Optional: an artifact spec for a protoc plugin, with "grpc" as
// the identifier, which can be referred to in the "plugins"
// container of the "generateProtoTasks" closure.
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:1.15.1"
}
}
generateProtoTasks {
ofSourceSet("main").forEach {
it.plugins {
// Apply the "grpc" plugin whose spec is defined above, without
// options. Note the braces cannot be omitted, otherwise the
// plugin will not be added. This is because of the implicit way
// NamedDomainObjectContainer binds the methods.
id("grpc") { }
}
}
}
}

It looks like an unnecessary complication, so I wonder what would you think of providing the following API instead:

dependencies {
    // A configuration where end-users could declare the relevant protobuf dependency
    protobufCompiler("com.google.protobuf:protoc:3.21.7")
    // Plain files are supported by Gradle: https://docs.gradle.org/current/userguide/declaring_dependencies.html#sub:file_dependencies
    // protobufCompiler(files("/path/to/protobuf/compiler"))

    // I'm not sure regarding the name, however, the idea is that each sourceSet
    // could create a configuration for attaching "protoc plugins" just like "implementation, api, compileOnly, etc" configurations
    // are created for each source set
    // Let us imagine that protobufPlugin("...") declares dependencies for "protoc tasks for the main source set"
    protobufPlugin("io.grpc:protoc-gen-grpc-java:1.50.0")

    // testProtobufPlugin declares dependencies for "protoc tasks for the test source set" (~ like testImplementation)
    testProtobufPlugin("io.grpc:protoc-gen-grpc-java:1.50.0")
}

That completely removes the need for protobuf { plugins {...}, generateProtoTasks { of...}, and I believe, it makes the common case much easier to understand and manage.

In other words, "adding protobuf-gradle-plugin" would look as follows:

 plugins {
     id("java-library")
+     id("com.google.protobuf") version "0.8.19"
 }

 dependencies {
     testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
+    protobufCompiler("com.google.protobuf:protoc:3.21.7")
+    protobufPlugin("io.grpc:protoc-gen-grpc-java:1.50.0")
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions