做网站百度还是阿里巴巴好,电脑网站推荐,聊城做网站建设,招商加盟网站模板html本次是通过Maven工具构建Java测试工程#xff0c;需要将原本通过gradle构建的项目需要通过maven构建加入公司代码库#xff0c;通过Maven构建涉及到接下来要介绍的插件#xff0c;总是发现pom.xml编译不通过#xff0c;看到网上都是千篇一律的插件配置#xff0c;自己就是…本次是通过Maven工具构建Java测试工程需要将原本通过gradle构建的项目需要通过maven构建加入公司代码库通过Maven构建涉及到接下来要介绍的插件总是发现pom.xml编译不通过看到网上都是千篇一律的插件配置自己就是编译不通过不知道大家有木有遇到下面我将调试过程以及解决方案记录如下。
1.定义proto文件
因为是测试和官网一样定义一个简单的hello.proto文件 2.maven插件配置
1添加依赖
propertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodinggrpc.version1.59.1/grpc.version!-- CURRENT_GRPC_VERSION --protobuf.version3.24.0/protobuf.versionprotoc.version3.24.0/protoc.version!-- required for JDK 8 --maven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/propertiesdependencyManagementdependenciesdependencygroupIdio.grpc/groupIdartifactIdgrpc-bom/artifactIdversion${grpc.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementdependenciesdependencygroupIdio.grpc/groupIdartifactIdgrpc-netty-shaded/artifactId!--version1.59.1/version--scoperuntime/scope/dependencydependencygroupIdio.grpc/groupIdartifactIdgrpc-protobuf/artifactId!--version1.59.1/version--/dependencydependencygroupIdio.grpc/groupIdartifactIdgrpc-services/artifactId/dependencydependencygroupIdio.grpc/groupIdartifactIdgrpc-stub/artifactId!--version1.59.1/version--/dependencydependency !-- necessary for Java 9 --groupIdorg.apache.tomcat/groupIdartifactIdannotations-api/artifactIdversion6.0.53/versionscopeprovided/scope/dependencydependencygroupIdcom.google.protobuf/groupIdartifactIdprotobuf-java/artifactIdversion${protobuf.version}/version/dependencydependencygroupIdcom.google.protobuf/groupIdartifactIdprotobuf-java-util/artifactIdversion${protobuf.version}/version/dependency/dependencies
2添加插件
buildextensionsextensiongroupIdkr.motd.maven/groupIdartifactIdos-maven-plugin/artifactIdversion1.7.1/version/extension/extensionspluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdconfigurationsource1.8/sourcetarget1.8/target/configuration/pluginplugingroupIdorg.xolstice.maven.plugins/groupIdartifactIdprotobuf-maven-plugin/artifactIdversion0.6.1/versionconfigurationprotocArtifactcom.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}/protocArtifactpluginIdgrpc-java/pluginIdpluginArtifactio.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}/pluginArtifactprotoSourceRootsrc/main/proto/protoSourceRoot/configurationexecutionsexecutiongoalsgoalcompile/goalgoalcompile-custom/goal/goals/execution/executions/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-enforcer-plugin/artifactIdversion1.4.1/versionexecutionsexecutionidenforce/idgoalsgoalenforce/goal/goalsconfigurationrulesrequireUpperBoundDeps//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
查阅资料将插件配置修改为 configurationadditionalProtoPathElementsadditionalProtoPathElement${project.basedir}/src/main/resources/additionalProtoPathElement/additionalProtoPathElementsprotocArtifactcom.google.protobuf:protoc:3.7.0:exe:${os.detected.classifier}/protocArtifact/configuration
再次执行编译还是不通过继续search。。。
找到io.github.ascopes下的protobuf-maven-plugin plugingroupIdio.github.ascopes/groupIdartifactIdprotobuf-maven-plugin/artifactId
lt;!ndash; version${protobuf-maven-plugin.version}/versionndash;gt;configurationprotocVersion${protobuf.version}/protocVersion/configurationexecutionsexecutiongoalsgoalgenerate/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 plugingroupIdorg.codehaus.mojo/groupIdartifactIdbuild-helper-maven-plugin/artifactIdversion3.3.0/versionexecutionsexecutionidtest/idphasegenerate-sources/phasegoalsgoaladd-source/goal/goalsconfigurationsourcessource${project.basedir}/target/generated-sources/source/sources/configuration/execution/executions/pluginplugingroupIdcom.github.os72/groupIdartifactIdprotoc-jar-maven-plugin/artifactIdversion3.11.4/versionexecutionsexecutionphasegenerate-sources/phasegoalsgoalrun/goal/goalsconfigurationoptimizeCodegenfalse/optimizeCodegenprotocVersion${protobuf.version}/protocVersionincludeStdTypestrue/includeStdTypes/configuration/execution/executions/plugin
他强调需要依赖 dependencygroupIdcom.google.protobuf/groupIdartifactIdprotobuf-java/artifactIdversion${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 profilebuildpluginsplugingroupIdcom.github.os72/groupIdartifactIdprotoc-jar-maven-plugin/artifactIdexecutions executionconfigurationprotocCommandprotoc/protocCommand !-- brew install protobuf --/configuration/execution /executions/plugin/plugins/buildactivationosnamemac os x/namearchaarch64/archfamilymac/family/os/activation/profile
/profiles
5配置插件生成java-grpc调用接口定义
plugingroupIdcom.github.os72/groupIdartifactIdprotoc-jar-maven-plugin/artifactIdversion3.11.4/versionexecutionsexecutionphasegenerate-sources/phasegoalsgoalrun/goal/goalsconfigurationoptimizeCodegentrue/optimizeCodegenprotocVersion${protobuf.version}/protocVersionincludeStdTypestrue/includeStdTypes!--includeDirectoriesdirectorysrc/main/proto/directory/includeDirectories--!--指定proto文件 --inputDirectoriesdirectorysrc/main/protobuf/directory/inputDirectoriesoutputTargetsoutputTargettypejava/type/outputTargetoutputTargettypegrpc-java/typepluginArtifactio.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