Iwavenice
Iwavenice

Reputation: 584

gRPC not generating interface for a service, only a service class

I am new to gRPC and have this problem: I created a .proto with rpc service definition. After compilation I get generated sources: all messages have a class that implements an interface. However a service itself doesn't implement any interface - it's simply not generated. And that's the interface I'm supposed to implement in my server. What am I doing wrong? I am pretty sure gRPC documentation says nothing about this problem.

My .proto service:

syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.blah.my.rpc.api";
option java_outer_classname = "MyServiceProto";
option objc_class_prefix = "Pb";

package com.blah.my.rpc.api;

service MyService
{
  rpc connect(PbEmptyMessage) returns (PbParameterGroup){}

  rpc getParams(PbGenList) returns (PbParameterGroup){}

}

message PbEmptyMessage
{
}

message PbGenId
{
      string paramName = 1;
      string systemName = 2;
      string sName = 3;
      string sId = 4;
}

message PbParameterGroup
{
       bytes sParameters = 2;
       fixed64 time  = 3;
}

My plugin definition in maven:

<extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.4.0.Final</version>
            </extension>
        </extensions>

        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.0</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-2:exe:${os.detected.classifier}
                    </protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.14.0:exe:${os.detected.classifier}</pluginArtifact>
                    <protoSourceRoot>${basedir}/src/main/resources</protoSourceRoot>
                    <outputDirectory>${basedir}/target/generated-sources</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Upvotes: 14

Views: 7377

Answers (2)

Iwavenice
Iwavenice

Reputation: 584

Got the answer from plugin developer.

1st thing: goals should be:

<goal>compile</goal>
<goal>compile-custom</goal>

2d and main thing: <outputDirectory> is reused between the two goals and thus its contents are rewritten. Removing this parameter solved the problem.

Upvotes: 11

Sam Barnum
Sam Barnum

Reputation: 10734

Had the same issue, and @Alexandra's answer got me headed in the right direction.

outputDirectory is the parameter that needs to be removed. The GRPC generated code goes in a different package, but if you specify an outputDirectory parameter things will get overwritten.

I also found that I needed to specify an explicit path to protoc in my maven file. That said, I'm no maven on that build system.

        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.5.1</version>
            <extensions>true</extensions>
            <configuration>
                <protocExecutable>/usr/local/bin/protoc</protocExecutable>
                <pluginId>grpc-java</pluginId>
            </configuration>

            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Using the above seems to have worked for me.

Upvotes: 1

Related Questions