Configuring Protobuf to Java compiler in maven


May 20, 2012 1 minutes read

Here are few steps to configure protobuf-to-java compilation in your maven project:

Install google protobuf compiler on your computer.

<properties>
    <!-- You may need to specify path to protobuf compiler -->
    <protocCommand>/opt/local/bin/protoc</protocCommand>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>com.github.igor-petruk.protobuf</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.5.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>2.4.1</version>
    </dependency>
</dependencies>

Create protobuf definition files within /src/main/protobuf/ directory. You may want to specify the following options in .proto files.

package com.example;
option java_multiple_files = true;
option java_outer_classname = "Messages";

mvn generate-sources and you’re done!

See Also

Java-protobuf-format: Easy Protobuf-to-JSON Serialization in Java

If you need Google Protobuf-to-JSON serialization (and vice versa) there is quick solution. Protobuf-java-format library provides serialization of protobuf-generated java classes to number of formats: JSON, XML, HTML, Java property file format, Smile, CoachDB.

Here is a step-by-step instruction.

GWT is about to add Native JSON function support in version 2.1

GWT 2.1 has reached Milestone 2. One of the new features in version 2.1 is support for browser’s native JSON function in JSONParser class.

logo   Never miss a story, subscribe to our newsletter