本来自己在网上找如何使protocol buffer在IDE(我用的是IDEA)上使用的,结果搜索出来的都不尽人意,因为都太粗略了,没有重点的去阐述,所以最后还是决定自己搜索相关的Maven插件,再慢慢地摸索,费了我好多的时间啊(本人小白),现在把过程写出来好给自己和有需要的人看吧。
参考:Maven Protocol Buffers Plugin
下面进入正题!
方法1:
<build>
...
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<extensions>true</extensions>
<configuration>
<!--默认值-->
<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
<!--默认值-->
<!--<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>-->
<outputDirectory>${project.build.sourceDirectory}</outputDirectory>
<!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件-->
<clearOutputDirectory>false</clearOutputDirectory>
<!--默认值-->
<temporaryProtoFileDirectory>${project.build.directory}/protoc-dependencies</temporaryProtoFileDirectory>
<!--更多配置信息可以查看https://www.xolstice.org/protobuf-maven-plugin/compile-mojo.html-->
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
<!--也可以设置成局部变量,执行compile或test-compile时才执行-->
<!--<configuration>-->
<!--<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>-->
<!--<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>-->
<!--<temporaryProtoFileDirectory>${project.build.directory}/protoc-dependencies</temporaryProtoFileDirectory>-->
<!--</configuration>-->
</execution>
</executions>
</plugin>
</plugins>
...
</build>
方法2:使用自己下载安装了的protocol buffer(关于安装protocol buffer的教程可以参考http://blog.csdn.net/qq_27273089/article/details/70231513),与maven结合,只要在方法1里的configuration里添加如下代码:
<!--这里填自己下载的protocol buffer的路径-->
<protocExecutable>F:/protocol buffer/protoc-3.1.0-win32/bin/protoc.exe</protocExecutable>
方法3:使用Maven的Toolchains,首先定义一个toolchains.xml文件,注意要放在 ${user.home}/.m2/文件夹下,其实就是maven默认的储存jar包的路径的上一级路径。代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
<toolchain>
<type>protobuf</type>
<provides>
<version>3.1.0</version>
</provides>
<configuration>
<protocExecutable>F:/protocol buffer/protoc-3.1.0-win32/bin/protoc.exe</protocExecutable>
</configuration>
</toolchain>
</toolchains>
然后在方法1里添加一个toolchains插件,代码如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<protobuf>
<version>3.1</version>
</protobuf>
</toolchains>
</configuration>
</plugin>
大功告成!接下来就可以使用通过protocol buffer生成的类文件来进行序列化啦。
转载请附上地址:http://blog.csdn.net/qq_27273089/article/details/70230897