-
通过maven将proto文件编译成Java源代码,需要添加protobuf-java
依赖
<properties>
<protobuf.version>3.19.4</protobuf.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
</dependencies>
-
进行builder插件设置,<protoSourceRoot>、<outputDirectory>、<clearOutputDirectory>,这三个参数非常有用使用时可以灵活设置
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.1</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protoSourceRoot>${basedir}/src/main/proto</protoSourceRoot>
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
-
是否成功配置maven的protobuf编译功能,点击左上角的Maven
,查看Plugins中是否存在protobuf插件
-
protobuf-maven-plugin还有很多其他的参数,具体可以参考文档:Maven Protocol Buffers Plugin