使用maven管理项目时,确实包的管理方便了很多,使用protobuf时,在pom文件中需要增加以下编译插件。
具体的配置参数可以到https://www.xolstice.org/protobuf-maven-plugin/index.html 进行查阅
依赖的包:
<dependency> <groupId>io.grpc</groupId> <artifactId>grpc-all</artifactId> <version>${grpc.version}</version> </dependency>
自动编译的配置:
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${plugin.compiler.version}</version> <configuration> <!--JDK版本--> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!--参数信息:https://www.xolstice.org/protobuf-maven-plugin/index.html--> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.5.1</version> <configuration> <protoSourceRoot>${basedir}/src/main/proto</protoSourceRoot><!--默认的proto文件路径--> <!--Protobuf compiler artifact specification, in groupId:artifactId:version[:type[:classifier]] format. When this parameter is set, the plugin attempts to resolve the specified artifact as protoc executable.--> <!--os.detected.classifier 变量来源:https://github.com/trustin/os-maven-plugin--> <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier} </protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} </pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> <!--按照指定的插件进行编译,即按照GRPC协议编译protob文件--> </goals> </execution> </executions> </plugin> </plugins>