将协议缓冲区整合到Maven2构build中

我在一个现有的,相当香草的Maven 2项目中使用Protocol Buffers进行实验。 目前,我每次需要更新生成的源代码时,都会调用一个shell脚本。 这显然是一个麻烦,因为我希望在每次构build之前自动生成源代码。 希望没有诉诸可耻的hackery。

所以,我的问题是双重的:

  1. 远景:有没有一个“协议缓冲区插件”为Maven 2可以实现上述以automagic方式? 谷歌代码上有一个分支,他的作者似乎在实现这样一个插件方面做了一些尝试。 不幸的是,它没有通过代码审查或合并到protobuf主干。 该插件的状态是未知的。

  2. 可能更现实的是:缺less一个实际的插件,我怎么可以从我的Maven 2构build调用protoc ? 我想我可以将现有的shell脚本连接到antrun调用或类似的东西。

个人经验是最感激的。

您可以在协议缓冲区讨论组的协议缓冲区编译器Maven插件线程中find有关协议缓冲区存储库中可用插件的一些信息。 我的理解是,它是可用的,但缺乏testing。 我会试试看。

或者你可以使用antrun插件(snipet从上面提到的线程粘贴):

  <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <tasks> <mkdir dir="target/generated-sources"/> <exec executable="protoc"> <arg value="--java_out=target/generated-sources"/> <arg value="src/main/protobuf/test.proto"/> </exec> </tasks> <sourceRoot>target/generated-sources</sourceRoot> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.0.3</version> </dependency> </dependencies> 

接受的答案鼓励我让Google提供的插件工作。 我把我的问题中提到的分支合并到2.2.0源代码的签出,build立和安装/部署插件,并能够在我的项目中使用它,如下所示:

  <build> <plugins> <plugin> <groupId>com.google.protobuf.tools</groupId> <artifactId>maven-protoc-plugin</artifactId> <version>0.0.1</version> <executions> <execution> <id>generate-sources</id> <goals> <goal>compile</goal> </goals> <phase>generate-sources</phase> <configuration> <protoSourceRoot>${basedir}/src/main/protobuf/</protoSourceRoot> <includes> <param>**/*.proto</param> </includes> </configuration> </execution> </executions> <configuration> <protocExecutable>/usr/local/bin/protoc</protocExecutable> </configuration> </plugin> </plugins> </build> 

请注意,我将插件的版本更改为0.0.1(无-SNAPSHOT),以使其进入我的非快照第三方Nexus存储库。 因人而异。 这个插件就是这个插件一旦不再需要跳过这个插件就可以使用。

接受的解决scheme不能缩放多个原始文件。 我必须拿出我自己的:

 <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>compile-protoc</id> <phase>generate-sources</phase> <configuration> <tasks> <mkdir dir="${generated.sourceDirectory}" /> <path id="proto.path"> <fileset dir="src/main/proto"> <include name="**/*.proto" /> </fileset> </path> <pathconvert pathsep=" " property="proto.files" refid="proto.path" /> <exec executable="protoc" failonerror="true"> <arg value="--java_out=${generated.sourceDirectory}" /> <arg value="-I${project.basedir}/src/main/proto" /> <arg line="${proto.files}" /> </exec> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </build> 

Igor Petruk还有一个名为protobuf-maven-plugin的插件 。 它现在在中央回购,并与日食(m2e-1.1被推荐)很好地玩。

我只是更新了Maven插件与2.2.0 – 更新的POM附加到代码审查错误。

以下是自己构build插件的说明:

 svn co http://protobuf.googlecode.com/svn/branches/maven-plugin/tools/maven-plugin cd maven-plugin wget -O pom.xml 'http://protobuf.googlecode.com/issues/attachment?aid=8860476605163151855&name=pom.xml' mvn install 

然后你可以使用上面的mavenconfiguration。

我只是从https://github.com/dtrott/maven-protoc-plugin尝试了一个不太正式的但最近的(v 0.1.7)fork,它的工作非常好,由David Trott提供。 我testing了一些Maven模块,其中一个包含DTO风格的消息,另一个服务取决于它们。 我借用了09年10月16日发布的插件configurationMaxA,我在PATH上添加了protoc,并添加了

<temporaryProtoFileDirectory>${basedir}/target/temp</temporaryProtoFileDirectory>

之后

<protocExecutable>protoc</protocExecutable>

真正好的是,我所要做的就是从DTO模块的服务模块声明一个正常的依赖关系。 该插件能够通过查找与DTO模块打包在一起的原始文件,将其提取到临时目录并在为服务生成代码时使用,来parsing原始文件依赖关系。 而且足够聪明,不要将生成的DTO类的第二个副本与服务模块打包在一起。

有一个protobuf的maven插件。 https://www.xolstice.org/protobuf-maven-plugin/usage.html

最小的configuration

  <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.5.0</version> <configuration> <protocExecutable>/usr/local/bin/protoc</protocExecutable> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> 

我认为使用antrun来调用非Maven步骤是普遍接受的解决scheme。

你也可以试试maven-exec-plugin 。

我从David Trott的插件中分出来,编译多种语言,这使得它更加有用。 看到这里的github项目和一个关于将它与maven构build在一起的教程。