公司的所有新项目全部转型Java,无奈只能搁置伴随自已8年之久的.NET,开始逐渐了解Java的开发,以方便自己后续的工作(主要是不被兄弟们忽悠)。在同事的强烈推荐中,选择了IntelliJ IDEA作为自己的Java IDE。
最近在了解ProtoBuf的编码技术,遂开始在IntelliJ IDEA下配置ProtoBuf的开发环境。在结合Baidu和Google,都只是零碎的介绍或者Ctrl+C,Ctrl+V的文章,对于新手来说不够系统和完成。经过一天的系统整理及实验,终于在上午完成了全部配置且达到了无鏠的集成。特分享给大家,供路人甲参考。(考虑到篇幅问题,下面过程只介绍步骤及过程,不再详述具体原理。若有需要,可以私下交流。)
环境介绍
操作系统:OS X EI Capitan
IDE: IntellJ IDEA 2017
JDK: Java 9
安装ProtoBuf Compiler
1, 到Google的Protobuf站点下载最新Release版本的Compiler(https://developers.google.com/protocol-buffers/).
2, 解压下载到的文件夹,名如:protobuf-3.5.1
3,使用Terminal进入protobuf-3.5.1目录。
4,依次执行下述命令,可以完成protobuf的安装及验证。
./configure
mark
mark check
sodu mark install
which protoc
protoc --version
执行完最后一个命令后,若能看到“libprotoc 3.5.1”,即说明已完成ProtoBuf Compiler的安装。
安装Protobuf Support插件
- 打开 IntelliJ IDEA 的 Preferences,在左侧找到 Plugins,进入如下图。
- 在上图中,点击 Browser Repositories,在其中搜索 protobuf,在搜索结果列表中会出现『Protobuf Support』,安装之。
- 由于国内网络的原因,部分可能无法通过上述步骤完成安装,可以到此链接(https://github.com/protostuff/protobuf-jetbrains-plugin/releases)下载文件后,使用『Install Plugin from Disk』功能,安装本地文件。
- 重启 IDE.
基于 Maven 创建项目
- 创建基于 Maven 的空项目(过程略)。
- 随意写一段可以执行的 Hello world 程序。
- 设置Project SDK 为Java 9.
- 在项目的 pom.xml 文件中加入 Protobuf plugin 配置。
<build> <pluginManagement> <plugins> <plugin> <!--protobuf 插件默认的 Phase 为 GenerateCode--> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.5.1</version> <executions> <execution> <!--把 Compile mojo和 test compile mojo 绑定到 GenerateCode 阶段。 这样,在 GenerateCode 阶段,会执行此插件的两个 mojo。否则,在Maven 默认的 Compile 或 Test 阶段 ,不会执行编译动作。--> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> </plugin> </plugins> </build>
- 引入 com.google.protobuf 依赖。
<dependencies> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.5.1</version> </dependency> </dependencies>
- 在 Project 的 src/main 下创建 proto目录。
- 右击 proto 目录,在『Mark Directory as...』下选择『Sources Root』。
- 按照 ProtoBuf 官方指南中的事例,在 proto 目录下创建『AddressBook.proto』,内容如下。
syntax = "proto2"; package tutorial; option java_package = "com.simonwang.java.learn.protobufentities"; option java_outer_classname = "AddressBookProtos"; message Person{ required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType{ MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber{ required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phones = 4; } message AddressBook{ repeated Person people = 1; }
- 在 Maven Project 列表中选择 Compile。在 output 框中提示『Compilationfailure:[ERROR] Source option 1.5 is no longer supported. Use 1.6 orlater.[ERROR] Target option 1.5 is no longer supported. Use 1.6 orlater.[ERROR]。
- 此错误是由于Maven 默认版本不支持 Java 9的原因。按如下在 pom.xml 中增加配置。
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
至此,就可以通过 Maven Project 中的 Compile 命令完成整个项目的编译。
Q&A:
- Q: ProtoBuf-plugin 默认的.proto文件的路径为${basedir}/src/main/proto,生成的 .java 文件为${project.build.directory}/generated-sources/protobuf/java,如何修改?
- A: 可以通过 Maven Plugin 的 configuration 结点修改两个文件夹的默认路径,如下配置:
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<outputDirectory>${basedir}</outputDirectory>
</configuration>
</plugin>
Protobuf-maven-plugin 的详细配置,可以访问我的另外一篇专门介绍的博文『
Maven Protobuf 详细配置』。