将协议缓冲区集成到 Maven2 构建中 [英] Integrate Protocol Buffers into Maven2 build

查看:32
本文介绍了将协议缓冲区集成到 Maven2 构建中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个现有的、相当普通的 Maven 2 项目中试验协议缓冲区.目前,我每次需要更新生成的源时都会调用一个 shell 脚本.这显然很麻烦,因为我希望在每次构建之前自动生成源代码.希望不要诉诸可耻的黑客.

I'm experimenting with Protocol Buffers in an existing, fairly vanilla Maven 2 project. Currently, I invoke a shell script every time I need to update my generated sources. This is obviously a hassle, as I would like the sources to be generated automatically before each build. Hopefully without resorting to shameful hackery.

所以,我的问题有两个:

So, my question is two-fold:

  1. 远射:是否有适用于 Maven 2 的Protocol Buffers 插件"可以自动实现上述功能?有一个 Google 代码分支,其作者似乎已经尝试实现这样的插件.不幸的是,它没有通过代码审查或合并到protobuf主干中.因此,该插件的状态未知.

  1. Long shot: is there a "Protocol Buffers plugin" for Maven 2 that can achieve the above in an automagic way? There's a branch on Google Code whose author appears to have taken a shot at implementing such a plugin. Unfortunately, it hasn't passed code review or been merged into protobuf trunk. The status of that plugin is thus unknown.

可能更现实:缺少实际插件,我还能如何从我的 Maven 2 构建中调用 protoc?我想我可以将现有的 shell 脚本连接到 antrun 调用或类似的东西中.

Probably more realistic: lacking an actual plugin, how else might I go about invoking protoc from my Maven 2 build? I suppose I may be able to wire up my existing shell script into an antrun invocation or something similar.

个人经验最受赞赏.

推荐答案

您可以在 Protocol Buffers Compiler Maven Plug-In 主题.我的理解是它可用但缺乏测试.我想试试.

You'll find some information about the plugin available in the Protocol Buffers repository in the Protocol Buffers Compiler Maven Plug-In thread on the Protocol Buffers discussion group. My understanding is that it's usable but lacking tests. I'd give it a try.

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

Or you could just use the antrun plugin (snipet pasted from the thread mentioned above):

 <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>

这篇关于将协议缓冲区集成到 Maven2 构建中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆