Search code examples
scalamavenprotocol-buffersgrpcscalapb

scalapb with maven not generating any grpc service classes


I am trying to create a simple gRPC service with ScalaPB and Maven. Following is my directory structure :

├── main
│   ├── proto
│   │   └── simple.proto
│   ├── resources
│   └── scala
│       └── me
│           ├── protogrpc
│           │   └── services
│           │       └── Main.scala
│           
└── test
    └── scala

and my simlpe.proto looks like this :

syntax = "proto3";

package scalapb.protos;

service Greeter {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
    string name = 1;
}

message HelloReply {
    string message = 1;
}

and pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>simple-grpc</artifactId>
    <groupId>me.protogrpc</groupId>
    <version>0.1-SNAPSHOT</version>

    <properties>
        <generated.sources.location>${project.build.directory}/generated-sources</generated.sources.location>
        <generated.sources.protobuf.location>${generated.sources.location}/protobuf</generated.sources.protobuf.location>
        <proto.files.location>${project.basedir}/proto</proto.files.location>
        <scalapb.version>0.9.6</scalapb.version>
        <scala.compat.version>2.12</scala.compat.version>
        <spec2.version>4.8.1</spec2.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>com.thesamet.scalapb</groupId>
            <artifactId>scalapb-runtime_${scala.compat.version}</artifactId>
            <version>${scalapb.version}</version>
        </dependency>
        <dependency>
            <groupId>com.thesamet.scalapb</groupId>
            <artifactId>scalapb-runtime-grpc_${scala.compat.version}</artifactId>
            <version>${scalapb.version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <version>1.25.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>1.25.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>1.25.0</version>
        </dependency>
    </dependencies>

    <build>
           <sourceDirectory>src/main/scala</sourceDirectory>
           <!--<testSourceDirectory>src/test/scala</testSourceDirectory> -->
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.0</version>
            </extension>
        </extensions>

        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>4.2.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <protocArtifact>com.google.protobuf:protoc:3.8.0:exe:${os.detected.classifier}</protocArtifact>
                            <protocPlugins>
                                <protocPlugin>
                                    <id>scalapb</id>
                                    <groupId>com.thesamet.scalapb</groupId>
                                    <artifactId>scalapbc_${scala.compat.version}</artifactId>
                                    <version>${scalapb.version}</version>
                                    <mainClass>scalapb.scripts.ProtocGenScala</mainClass>
                                </protocPlugin>
                            </protocPlugins>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

when I do mvn compile or mvn scala:compile I can see the protobuf classes are generated in the target directory except for any classes related to gRPC.

├── generated-sources
│   └── protobuf
│       └── java
│           └── scalapb
│               └── protos
│                   ├── Simple.java
│                   └── simple
│                       ├── HelloReply.scala
│                       ├── HelloRequest.scala
│                       └── SimpleProto.scala

Am I missing anything?


Solution

  • I updated the ScalaPB maven example to generate code for grpc. It does not do it by default.

          <plugin>
              <groupId>com.github.os72</groupId>
              <artifactId>protoc-jar-maven-plugin</artifactId>
              <version>3.11.1</version>
              <executions>
                  <execution>
                      <phase>generate-sources</phase>
                      <goals>
                          <goal>run</goal>
                      </goals>
                  </execution>
              </executions>
              <configuration>
                <includeMavenTypes>transitive</includeMavenTypes>
                <outputTargets>
                    <outputTarget>
                        <type>scalapb</type>
                        <outputOptions>grpc</outputOptions> <!-- more scalapb options can be added here -->
                        <pluginArtifact>com.thesamet.scalapb:protoc-gen-scala:0.9.6:sh:unix</pluginArtifact>
                    </outputTarget>
                </outputTargets>
            </configuration>
          </plugin>
    

    See https://github.com/thesamet/scalapb-maven-example/blob/master/pom.xml