Protobuf插件使用
在Netty开发时需protoc转java类,一般的做法是执行protoc命令,依次将.proto文件转成Java类
protoc.exe -I=d:/tmp --java_out=d:/tmp d:/tmp/monitor_data.proto
今天介绍一个快速的将protobuf转java文件正确姿势,只需要在我们的pom.xml中添加如下配置,然后再maven中执行compile命令则会生成响应的文件
注意
protoSourceRoot 读取resources目录中的所有proto (按自己喜好修改)
outputDirectory 生成输出的java文件也是存放在resources目录中(按自己喜好修改)
其它更多参数参考官方文档
https://www.xolstice.org/protobuf-maven-plugin/compile-mojo.html
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>protoc-java</pluginId>
<!--读取proto文件路径-->
<protoSourceRoot>${project.basedir}/src/main/resources</protoSourceRoot>
<!--生产的java文件路径-->
<outputDirectory>${project.basedir}/src/main/resources/protobuf/entity</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行效果