Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobuf plugin for gradle does not generate service class

I try to build a gRPC and protobuf application in java with Gradle

I followed the instruction from: https://github.com/grpc/grpc-java/blob/master/README.md

The issue is that one file is not generated: *ServiceGrpc.java But the corresponding *ServiceGrpc.class file is in the build directory generated by the gradle build.

I tried with running the compiler manually with the command protoc but I have the exact same issue (I'm on Ubuntu 18.04)

Here is my proto file

syntax = "proto3";

option java_multiple_files=true; 
option java_generic_services= true;
//...//
message Track { 
    int64 id = 1; //... }
service TrackService { 
    rpc Create(Track) returns (Response); }
//...

The file Track.java, TrackOrBuilder.java, TrackOuterClass.java are all there. As well as their corresponding .class files in the build directory.

With the flag "option java_generic_services= true", TrackService.java is generated, and again .class file.

But not matter what, the file TrackServiceGrpc.java is not created, contrary its correspond .class file, which is quite confusing.

here is my build.gradle :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
    }
}
plugins {
    id 'java'
}
repositories {
    mavenCentral()
}
apply plugin: 'com.google.protobuf'

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.9.0"
        generateProtoTasks.generatedFilesBaseDir = 'src'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.23.0'
        }
    }

    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    //https://github.com/grpc/grpc-java/blob/master/README.md
    implementation 'io.grpc:grpc-netty-shaded:1.23.0'
    implementation 'io.grpc:grpc-protobuf:1.23.0'
    implementation 'io.grpc:grpc-stub:1.23.0'
    compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
}

What am I doing wrong ?

like image 975
moguil Avatar asked Nov 06 '22 14:11

moguil


1 Answers

If the .class file for the gRPC service exists, the corresponding .java file must have been at some place. By default, it should appear in $generatedFilesBaseDir/{main, test}/grpc. By default, $generatedFilesBaseDir is $buildDir/generated/source/proto. But seems you have changed (or intended to change) generatedFilesBaseDir, that configuration should be done inside the protobuf closure instead of the protoc closure.

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.9.0"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.23.0'
        }
    }
    generatedFilesBaseDir = 'src'
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

Also, need to mention that configuring generatedFileBaseDir is discouraged as it might have potential problems. See discussion at https://github.com/google/protobuf-gradle-plugin/issues/332

like image 185
voidzcy Avatar answered Nov 12 '22 16:11

voidzcy