Protobuf-java maven配置

1. maven pom片断

          <!-- protobuf-java for maven plugin
                http://stackoverflow.com/questions/1578456/integrate-protocol-buffers-into-maven2-build

              Execute failed: java.io.IOException: Cannot run program "protoc":
              complile and install compiler the protoc:
                 # git clone https://github.com/google/protobuf
                 # ./autogen.sh
                 # ./configure
                 # make && make check
                 # sudo make install
              After all, compile a proto to java (== means two minus signs):
                 # protoc -I=$SRC_DIR ==java_out=$DST_DIR user.proto
            -->
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <tasks>
                                <mkdir dir="src/main/java/avro/channel/protobuf"/>
                                <exec failonerror="true" executable="protoc">
                                    <arg value="--java_out=src/main/java/avro/channel/protobuf"/>
                                    <arg value="src/avro/user.proto"/>
                                </exec>
                            </tasks>
                            <sourceRoot>src/main/java/avro/channel/protobuf</sourceRoot>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>${protobuf-java.version}</version>
        </dependency>
    </dependencies>

2. user.proto

option java_package = "avro.channel.protobuf";
option java_outer_classname = "Reg";

message User {
    required string ipaddr = 1;
    optional string cookie = 2;
    optional int64 token = 3;
}

3. 安装protobuf

1) 下载

https://github.com/google/protobuf/releases

$ wget --no-check-certificate https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz

2) 安装

$ tar -zxf protobuf-2.6.1.tar.gz

$ cd protobuf-2.6.1/

$ ./configure

$ make check

sudo make install

3) ubuntu下

$ sudo cd /etc/ld.so.conf.d/

$ sudo touch libprotobuf.conf

$ sudo echo "/usr/local/lib" >> libprotobuf.conf

$ sudo ldconfig

4) 检验

$ protoc --version

libprotoc 2.6.1
05-11 13:28