10

Does anyone know how to compile *.proto files for grpc application in maven?

This is how I'm compiling protobuf in maven - (old way, using installed protoc compiler, excerpt from pom.xml):

  <build>
    <plugins>

      <!-- protocol buffers runner, requires protoc -->
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>generate-protobuf-sources</id>
            <phase>generate-sources</phase>
            <configuration>
              <tasks>
                <mkdir dir="target/generated-sources/java" />

                <exec executable="protoc">
                  <arg value="--java_out=target/generated-sources/java" />
                  <arg value="src/main/protobuf/hello.proto" />
                </exec>
              </tasks>
              <sourceRoot>target/generated-sources/java</sourceRoot>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

I wonder if something similar is possible for grpc. From what I understand I need to somehow connect protoc-gen-grpc-java plugin with protobuf, but I'm not sure how to do that.

UPDATE: For those who interested I created a fully working example of client-server app using maven on github.

Alex
  • 2,916
  • 3
  • 22
  • 27

2 Answers2

13

I'd highly recommend using protobuf-maven-plugin as described in the grpc-java README.

If you really want to do it manually, you can download protoc-gen-grpc-java from Maven Central and add another <arg> for the exec of protoc:
--plugin=protoc-gen-grpc-java=path/to/protoc-gen-grpc-java

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
Eric Anderson
  • 24,057
  • 5
  • 55
  • 76
  • I updated my question with a link to my github repo with a fully working example. – Alex Mar 17 '16 at 03:03
  • 1
    Step by step tutorial using grpc-java and maven at https://bertrandszoghy.wordpress.com/2017/06/01/protocol-buffers-grpc-client-and-server-using-secure-channels-with-java/ – Bertrand_Szoghy Jun 01 '17 at 18:53
1

protoc-jar-maven-plugin does not require installing the protoc compiler, unlike protobuf-maven-plugin.

Add this to your plugins:

<plugin>
    <groupId>com.github.os72</groupId>
    <artifactId>protoc-jar-maven-plugin</artifactId>
    <version>3.11.4</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

and it uses the cross platform protoc-jar to generate your protobuf.

joseph
  • 2,429
  • 1
  • 22
  • 43