我正在尝试使用ScalaPB和Maven创建一个简单的gRPC服务。以下是我的目录结构:
├── main
│ ├── proto
│ │ └── simple.proto
│ ├── resources
│ └── scala
│ └── me
│ ├── protogrpc
│ │ └── services
│ │ └── Main.scala
│
└── test
└── scala
和我的simlpe.proto看起来像这样:
syntax = "proto3";
package scalapb.protos;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
和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>
当我执行mvn compile
或mvn scala:compile
时,可以看到在目标目录中生成了protobuf类,除了与gRPC相关的任何类。
├── generated-sources
│ └── protobuf
│ └── java
│ └── scalapb
│ └── protos
│ ├── Simple.java
│ └── simple
│ ├── HelloReply.scala
│ ├── HelloRequest.scala
│ └── SimpleProto.scala
我错过了什么吗?
答案 0 :(得分:1)
我更新了ScalaPB maven示例以为grpc生成代码。默认情况下不执行此操作。
<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>
请参见https://github.com/thesamet/scalapb-maven-example/blob/master/pom.xml
答案 1 :(得分:0)
您还需要在io.grpc:protoc-gen-grpc-java
文件中配置pom.xml
来运行gRPC原型代码生成插件。 Example here