我试图用Java、Maven和Intellij为gRPC应用程序生成代码。
我遵循grpc https://github.com/grpc/grpc-java/blob/master/README.md的文档,但是当我构建这个项目时,没有生成任何代码。
这是我的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>
<groupId>org.nolapps</groupId>
<artifactId>grpc-simpleapp</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.35.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.35.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.35.0</version>
</dependency>
<dependency> <!-- necessary for Java 9+ -->
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>6.0.53</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.12.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.35.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>我的proto文件位于src/main/proto (messages.proto)中。
syntax = "proto3";
option java_package="grpc.messages";
message Employee {
int32 id = 1;
int32 badgeNumber = 2;
string firstName = 3;
string lastName = 4;
float vacationAccrualRate = 5;
float vacationAccrued = 6;
}
message GetAllRequest {}
message GetByBadgeNumberRequest {
int32 badgeNumber = 1;
}
message EmployeeRequest {
Employee employee = 1;
}
message EmployeeResponse {
Employee employee = 1;
}
message AddPhotoRequest {
bytes data = 1;
}
message AddPhotoResponse {
bool isOk = 1;
}
service EmployeeService {
rpc GetByBadgeNumber (GetByBadgeNumberRequest) returns (EmployeeResponse);
rpc GetAll (GetAllRequest) returns (stream EmployeeResponse);
rpc Save (EmployeeRequest) returns (EmployeeResponse);
rpc SaveAll (EmployeeRequest) returns (stream EmployeeResponse);
rpc AddPhoto (AddPhotoRequest) returns (AddPhotoResponse);
}要构建该项目(来自Intelij),我转到: build -> Build project。但什么都没发生。
我的问题:知道为什么没有生成我的代码吗?
发布于 2022-11-22 22:03:45
通常Maven将生成代码并将其置于target/generated-source/protubf下
为了构建代码,需要发出以下命令:
$ mvn clean install在IntelliJ中,您需要编辑run的配置( RUN -> Edit Configuration )
还可以通过向插件中添加以下配置来定义maven中的protobuf插件的输出目录
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.51.0:exe:${os.detected.classifier}</pluginArtifact>
<outputBaseDirectory>src/main/java/</outputBaseDirectory>
<outputDirectory>src/main/java/</outputDirectory>
<clearOutputDirectory>false</clearOutputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>这将输出java目录下生成的文件,并创建proto文件中定义的包(示例中为grpc.messages )。
https://stackoverflow.com/questions/66221093
复制相似问题