Integrate Protocol Buffers into Maven2 build

前端 未结 9 678
悲哀的现实
悲哀的现实 2021-01-30 02:18

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

相关标签:
9条回答
  • 2021-01-30 02:53

    There is a maven plugin for protobuf. https://www.xolstice.org/protobuf-maven-plugin/usage.html

    The minimal config

     <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>
    
    0 讨论(0)
  • 2021-01-30 02:57

    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.

    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>
    
    0 讨论(0)
  • 2021-01-30 02:57

    I just updated the maven plugin to work with 2.2.0 -- the updated pom are attached to the code review bug.

    Here are the instructions to build the plugin yourself:

    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
    

    You can then use the maven config above.

    0 讨论(0)
  • 2021-01-30 03:02

    The accepted answer encouraged me to get the Google-provided plugin to work. I merged the branch mentioned in my question into a checkout of 2.2.0 source code, built and installed/deployed the plugin, and was able to use it in my project as follows:

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

    Note that I changed the plugin's version to 0.0.1 (no -SNAPSHOT) in order to make it go into my non-snapshot thirdparty Nexus repository. YMMV. The takeaway is that this plugin will be usable once it's no longer necessary to jump through hoops in order to get it going.

    0 讨论(0)
  • 2021-01-30 03:05

    There's also great plugin by Igor Petruk named protobuf-maven-plugin. It's in central repo now and plays nicely with eclipse (m2e-1.1 is recommended).

    0 讨论(0)
  • 2021-01-30 03:05

    I just tried a less official but very recent (v 0.1.7) fork from https://github.com/dtrott/maven-protoc-plugin and it worked very well, courtesy of David Trott. I tested it with a couple of Maven modules one of which contained DTO-style messages and the other a service depending on them. I borrowed the plugin configuration MaxA posted on Oct 16 '09, I had protoc on my PATH and I added

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

    right after

    <protocExecutable>protoc</protocExecutable>.

    What is really nice is that all I had to do is to declare a normal dependency from the service module on the DTO module. The plugin was able to resolve proto files dependencies by finding the proto files packaged with the DTO module, extracting them to a temporary directory and using while generating code for the service. And it was smart enough not to package a second copy of the generated DTO classes with the service module.

    0 讨论(0)
提交回复
热议问题