Maven可以使用protobuf-maven-plugin插件来自动生成Java文件。在项目的pom.xml文件中,添加以下插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.5.0</version>
            <configuration>
                <protocExecutable>protoc</protocExecutable>
                <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
                <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

其中,protocExecutable表示使用的protoc编译器路径,protoSourceRoot表示.proto文件的根目录,outputDirectory表示生成的Java文件存放的目录。

执行mvn compile命令即可自动在outputDirectory目录下生成对应的Java文件。

maven自动proto生成java文件

原文地址: https://www.cveoy.top/t/topic/bEOQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录