42

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

所以,我的问题有两个:

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

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

个人经验最受赞赏。

4

9 回答 9

44

您可以在Protocol Buffers 讨论组的Protocol Buffers Compiler Maven Plug-In线程中找到有关 Protocol Buffers 存储库中可用插件的一些信息。我的理解是它可用但缺乏测试。我会试一试。

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

 <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>
于 2009-10-16T14:58:27.943 回答
23

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

  <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 存储库。YMMV。要点是,一旦不再需要跳过箍来启动它,这个插件就可以使用了。

于 2009-10-16T15:55:42.233 回答
21

接受的解决方案不适用于多个 proto 文件。我不得不想出我自己的:

<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>
于 2010-06-09T14:58:12.920 回答
10

Igor Petruk 还有一个很棒的插件,名为protobuf-maven-plugin。它现在在中央存储库中,并且与 eclipse 配合得很好(推荐使用 m2e-1.1)。

于 2012-02-20T08:42:26.663 回答
4

我刚刚更新了 maven 插件以与 2.2.0 一起使用——更新的 pom 附加到代码审查错误中。

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

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

然后,您可以使用上面的 Maven 配置。

于 2009-11-19T01:01:32.150 回答
4

我刚刚从https://github.com/dtrott/maven-protoc-plugin尝试了一个不太官方但最近的(v 0.1.7)分支,它运行得非常好,由 David Trott 提供。我用几个 Maven 模块对其进行了测试,其中一个包含 DTO 样式的消息,另一个包含依赖于它们的服务。我借用了 2009 年 10 月 16 日发布的插件配置 MaxA,我的 PATH 上有 protoc,我添加了

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

紧接着

<protocExecutable>protoc</protocExecutable>.

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

于 2010-12-22T09:52:44.500 回答
4

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

最小配置

 <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>
于 2016-06-08T10:20:10.550 回答
0

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

你也可以试试maven-exec-plugin

于 2009-10-16T14:44:40.233 回答
0

我从 David Trott 那里分叉了插件,并让它编译多种语言,这使它更有用。请参阅此处的 github 项目和将其与 Maven 构建集成的教程

于 2011-09-11T20:04:31.957 回答