本次是通过Maven工具构建Java测试工程,需要将原本通过gradle构建的项目需要通过maven构建加入公司代码库,通过Maven构建涉及到接下来要介绍的插件,总是发现pom.xml编译不通过,看到网上都是千篇一律的插件配置,自己就是编译不通过,不知道大家有木有遇到,下面我将调试过程以及解决方案记录如下。
1.定义proto文件
因为是测试和官网一样,定义一个简单的hello.proto文件
2.maven插件配置
<1>添加依赖
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><grpc.version>1.59.1</grpc.version><!-- CURRENT_GRPC_VERSION --><protobuf.version>3.24.0</protobuf.version><protoc.version>3.24.0</protoc.version><!-- required for JDK 8 --><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencyManagement><dependencies><dependency><groupId>io.grpc</groupId><artifactId>grpc-bom</artifactId><version>${grpc.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>io.grpc</groupId><artifactId>grpc-netty-shaded</artifactId><!--<version>1.59.1</version>--><scope>runtime</scope></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><!--<version>1.59.1</version>--></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-services</artifactId></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-stub</artifactId><!--<version>1.59.1</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><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>${protobuf.version}</version></dependency><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java-util</artifactId><version>${protobuf.version}</version></dependency></dependencies>
<2>添加插件
<build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.7.1</version></extension></extensions><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.6.1</version><configuration><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><protoSourceRoot>src/main/proto</protoSourceRoot></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-enforcer-plugin</artifactId><version>1.4.1</version><executions><execution><id>enforce</id><goals><goal>enforce</goal></goals><configuration><rules><requireUpperBoundDeps/></rules></configuration></execution></executions></plugin></plugins></build>
这个插件在我的本地无法编译通过,大家都知道maven编译不通过是没法执行接下来的命令,执行编译是为了将proto文件生成对应的java代码。
<3>执行命令
mvn clean compile
结果是报错了,具体错误没有记录。
<4>修改插件配置
参考:java - How to use google protobuf compiler with maven-compiler-plugin - Stack Overflow
查阅资料将插件配置修改为
<configuration><additionalProtoPathElements><additionalProtoPathElement>${project.basedir}/src/main/resources</additionalProtoPathElement></additionalProtoPathElements><protocArtifact>com.google.protobuf:protoc:3.7.0:exe:${os.detected.classifier}</protocArtifact></configuration>
再次执行编译还是不通过,继续search。。。
找到io.github.ascopes下的protobuf-maven-plugin
<plugin><groupId>io.github.ascopes</groupId><artifactId>protobuf-maven-plugin</artifactId>
<!– <version>${protobuf-maven-plugin.version}</version>–><configuration><protocVersion>${protobuf.version}</protocVersion></configuration><executions><execution><goals><goal>generate</goal></goals></execution></executions></plugin>
经过测试没有下载到io.github.ascopes#protobuf-maven-plugin插件,这个插件也许是可以用的,我没有切换maven仓库源,只是在maven 仓库官网没找到而已,果断放弃了。如果要使用这个插件需要注意一个默认约束,你定义的proto文件位置:src/main/protobuf
插件默认会从这个位置读取proto文件,除非你修改插件配置。
继续寻找其他插件,继续search。。。
功夫不负有心人,果然找到了一款插件配置如下
先贴一下地址:
How to Work With Protobuf-Maven Projects in IntelliJ IDEA
How to Work With Protobuf-Maven Projects in IntelliJ IDEA | The IntelliJ IDEA Blog
源码地址:GitHub - elenakozlova/maven-protobuf: sample maven-protobuf project for the blog post
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>build-helper-maven-plugin</artifactId><version>3.3.0</version><executions><execution><id>test</id><phase>generate-sources</phase><goals><goal>add-source</goal></goals><configuration><sources><source>${project.basedir}/target/generated-sources</source></sources></configuration></execution></executions></plugin><plugin><groupId>com.github.os72</groupId><artifactId>protoc-jar-maven-plugin</artifactId><version>3.11.4</version><executions><execution><phase>generate-sources</phase><goals><goal>run</goal></goals><configuration><optimizeCodegen>false</optimizeCodegen><protocVersion>${protobuf.version}</protocVersion><includeStdTypes>true</includeStdTypes></configuration></execution></executions></plugin>
他强调需要依赖
<dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>${project.basedir}</version></dependency>
配置后插件后,pom.xml编译通过 ,心里爽快多了。
继续执行命令编译
mvn clean compile
不幸的是又出现错误
Unsupported platform: protoc-3.24.0-osx-x86_64.exe
这个错误一看就是和protoc安装版本有关系,我的是arm操作系统,显示x86肯定不对。
我猜测自己的mac os安装的protobuf未生效造成的,执行命令将protoc命令加入环境变量
$ source ~/.bash_profile
执行完后重新构建,果真通过
在插件${project.basedir}/target/generated-sources指定的target下生成了我们需要的Java源码文件
⚠️注意:插件要求你的proto文件路径(默认):src/main/protobuf
关于Unsupported platform: protoc-3.24.0-osx-x86_64.exe错误应该还有以下解决办法,issues链接如下(针对mac os M1/M2):
Support for M1 Macs (osx-aarch_64) · Issue #93 · os72/protoc-jar · GitHub
<profile><build><plugins><plugin><groupId>com.github.os72</groupId><artifactId>protoc-jar-maven-plugin</artifactId><executions> <execution><configuration><protocCommand>protoc</protocCommand> <!-- brew install protobuf --></configuration></execution> </executions></plugin></plugins></build><activation><os><name>mac os x</name><arch>aarch64</arch><family>mac</family></os></activation></profile>
</profiles>
<5>配置插件生成java-grpc调用接口定义
<plugin><groupId>com.github.os72</groupId><artifactId>protoc-jar-maven-plugin</artifactId><version>3.11.4</version><executions><execution><phase>generate-sources</phase><goals><goal>run</goal></goals><configuration><optimizeCodegen>true</optimizeCodegen><protocVersion>${protobuf.version}</protocVersion><includeStdTypes>true</includeStdTypes><!--<includeDirectories><directory>src/main/proto</directory></includeDirectories>--><!--指定proto文件 --><inputDirectories><directory>src/main/protobuf</directory></inputDirectories><outputTargets><outputTarget><type>java</type></outputTarget><outputTarget><type>grpc-java</type><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.1</pluginArtifact></outputTarget></outputTargets></configuration></execution></executions></plugin>
执行重新构建
$ mvn clean compile
控制台打印如下:
生成程序如下:
以上针对Java版本的gRPC代码生成和官网不一样,很容易理解,希望给刷到的同学带来些许帮助。
插件:protoc-jar-maven-plugin官方使用详细说明:
protoc-jar-maven-plugin - Introduction