alreadytaken
alreadytaken

Reputation: 2146

Generating protobuf file descriptors with protobuf-gradle-plugin

I am trying to generate file descriptors using protobuf-gradle-plugin:

protobuf {
    // Configure the protoc executable
    protoc {
        artifact = "com.google.protobuf:protoc:$dependencyVersions.protobuf"
        generatedFilesBaseDir = "$projectDir/src/generated-sources"
    }
    generateProtoTasks {
        all().each { task ->
            task.generateDescriptorSet = true
            task.descriptorSetOptions.path = "$projectDir/src/generated-sources/descriptors/{$task.sourceSet.name}.dsc"
        }
    }
}

sourceSets {
    main {
        proto {
            srcDir 'src/main/proto'
            exclude 'google/*'
        }
    }
}

But this only generates one descriptor file:{main}.desc. Am I only supposed to have one descriptor file for many proto files? If not, how would I go about generating a separate descriptor file for each proto file using the plugin?

Upvotes: 4

Views: 2731

Answers (1)

alreadytaken
alreadytaken

Reputation: 2146

I thought that there would be a separate file for each proto, but it appears we can only generate one large file descriptor for all protos: descriptors.dsc.

In hindsight, multiple descriptor files aren't really necessary because you can use this one file pretty easily in Java:

final FileInputStream fileInputStream = new FileInputStream("directory/descriptors.dsc");
final DescriptorProtos.FileDescriptorSet descriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(fileInputStream);

for (DescriptorProtos.FileDescriptorProto fileDescriptor : descriptorSet.getFileList()) {
     // Do as you wish with fileDescriptor
}

Upvotes: 3

Related Questions