xolstice / protobuf-maven-plugin

Maven Plugin that executes the Protocol Buffers (protoc) compiler
https://www.xolstice.org/protobuf-maven-plugin/
Other
232 stars 76 forks source link

Is it possible to generate CPP GRPC Stub Code using plugin? #25

Closed amarcionek closed 7 years ago

amarcionek commented 7 years ago

The instructions for grpc-java mention using this plugin to generate the grpc client and/or server stub code for Java. The Java stub seems to be generated compile-custom goal and put in target/generated-sources/protobuf/grpc-java/com/seven10/gateway/io directory by default. From what I can tell from the cpptutorial, the c++ stubs are generated using the following command: $ protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc="which grpc_cpp_plugin" ../../protos/route_guide.proto

Is it currently possible to do that with this plugin?

sergei-ivanov commented 7 years ago

Hello Adam,

First of all, apologies for a slow response, I have been snowed under with work in the last few weeks.

To your question. I think what you are trying to achieve is quite feasible (even if Maven is not the best tool for dealing with non-JVM languages). Unlike protoc-gen-grpc-java that Google distributes through Maven Central, grpc_cpp_plugin needs to be built/downloaded separately and made available in the $PATH (or an absolute path needs to be specified in the POM).

Assuming you still need to generate and compile Java sources as well, you will need to introduce separate plugin executions as follows:

<build>
  <extensions>
    <extension>
      <groupId>kr.motd.maven</groupId>
      <artifactId>os-maven-plugin</artifactId>
      <version>1.4.1.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.2.0:exe:${os.detected.classifier}</protocArtifact>
      </configuration>
      <executions>
        <execution>
          <id>protoc-java</id>
          <goals>
            <goal>compile</goal>
          </goals>
        </execution>
        <execution>
          <id>protoc-cpp</id>
          <goals>
            <goal>compile-cpp</goal>
          </goals>
        </execution>
        <execution>
          <id>protoc-grpc-java</id>
          <goals>
            <goal>compile-custom</goal>
          </goals>
          <configuration>
            <pluginId>grpc-java</pluginId>
            <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.3.0:exe:${os.detected.classifier}</pluginArtifact>
          </configuration>
        </execution>
        <execution>
          <id>protoc-grpc-cpp</id>
          <goals>
            <goal>compile-custom</goal>
          </goals>
          <configuration>
            <pluginId>grpc-cpp</pluginId>
            <!-- You may need to run `which grpc_cpp_plugin` beforehand to determine the actual path-->
            <pluginExecutable>grpc_cpp_plugin</pluginExecutable>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Please note that the above assumes conventional defaults for all executions, and you may want to customise source or target directories, or any other relevant parameters.

Please give it a go and let me know if it works for you.

Kind regards, Sergei

amarcionek commented 7 years ago

Hi Sergei,

I can confirm these instructions work! Hopefully this will help anyone in the future who has a maven build system. THANK YOU SO MUCH!

Adam

Gopinathpalanisami commented 4 years ago

Hi Sergei/adam,

This help me now :) I was trying to use plugin and build it but am not able to compile it , Kindly help me on this :)

Please find my pom.xml and PATH /usr/local/bin/grpc_cpp_plugin,

sles15:~/communication/gRPC-communication # echo $PATH /sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/opt/apache-maven-3.3.9/bin:/usr/local/bin/grpc_cpp_plugin:/usr/local/bin/grpc_python_plugin

Still my mvn compile fails with below error .

Kindly help me in this :)

      <execution>
        <id>protoc-grpc-cpp</id>
        <!--id>protoc-grpc-python</id-->
        <goals>
              <goal>compile-custom</goal>
            </goals>
        <configuration>
          <pluginId>grpc-cpp</pluginId>
          <!--pluginId>grpc-python</pluginId-->
                  <!-- You may need to run `which grpc_cpp_plugin` and `which grpc_python_plugin` beforehand to determine the actual path-->
          <pluginExecutable>grpc_cpp_plugin</pluginExecutable>
          <!--pluginExecutable>grpc_python_plugin</pluginExecutable-->
            </configuration>           
              </execution>

When I compile am getting below error,

[INFO] Compiling 1 proto file(s) to /root/communication/gRPC-communication/target/generated-sources/protobuf/grpc-cpp [ERROR] PROTOC FAILED: grpc_cpp_plugin: program not found or is not executable Please specify a program using absolute path or make sure the program is available in your PATH system variable --grpc-cpp_out: protoc-gen-grpc-cpp: Plugin failed with status code 1.

[ERROR] /root/communication/gRPC-communication/src/main/proto/user.proto [0:0]: grpc_cpp_plugin: program not found or is not executable Please specify a program using absolute path or make sure the program is available in your PATH system variable --grpc-cpp_out: protoc-gen-grpc-cpp: Plugin failed with status code 1.

sergei-ivanov commented 4 years ago

Is /usr/local/bin/grpc_cpp_plugin in your $PATH an executable or a directory? Can you run grpc_cpp_plugin from the same terminal session as you run Maven? If you run which grpc_cpp_plugin, what does it return? As the error says, Maven cannot locate your grpc_cpp_plugin executable in the $PATH. Are you running on Unix/Linux, MacOS or some flavour of Cygwin (e.g. MSysGit)?

Gopinathpalanisami commented 4 years ago

Thanks a lot @sergei-ivanov, After giving absolute path in pom.xml for grpc_cpp_plugin and we are able generate language specific stubs using maven.