google protobuf的介绍和使用官方地址为:https://developers.google.com/protocol-buffers/
先总结一下如何在eclipse中轻松的使用protobuf.
首先需要安装一个插件叫protobuf-dt,介绍及安装说明:https://code.google.com/p/protobuf-dt/
These instructions assume that you have already installed some flavor of Eclipse 3.7 or 3.8. If you have not, Eclipse can be downloaded fromhttp://download.eclipse.org/eclipse/downloads/
Once you have Eclipse up and running, do the following
- install Xtext 2.3.0 from the update site http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/
- install protobuf-dt from the update site http://protobuf-dt.googlecode.com/git/update-site
我用的是eclipse juno,安装过程中遇到了一些问题,在https://code.google.com/p/protobuf-dt/wiki/Installing下面的留言中找到了解决方案。
总结一下就是:
1 安装顺序不能乱,先安装xtext,再安装protobuf-dt,
2 xtext插件只安装xtext ui组件,其他的不要安装,不然后面安装protobuf-dt的时候会有依赖冲突。
3 protobuf-dt和xtext插件的版本有关系,protobuf-dt的最新版本依赖的是xtext2.4.2版本,因此安装时记得选对版本,官方安装说明里那个已经过时了是很早以前的安装说明。
安装好后,就可以在eclipse里非常方便的编写自己的.proto文件了。file-new中也会有新建.proto文件。
新建一个maven项目,添加依赖
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>