Home

Awesome

Protoc Generator Java Optional Build pipeline

A Java Protoc plugin extending generated java classes with null safe setOrClear and getOptional methods. Also works for protobuf primitive variables with the optional keyword.

Quick usage

Using protoc binary

You must have protoc binary installed in your system and have to download protoc-gen-java-optional executable based on platform from GitHub releases or at Files section in maven-central.

With defaults:

protoc --proto-path=./protos/ --java_out=./out/directory --plugin=protoc-gen-java-optional=./path/to/plugin/protoc-gen-java-optional.exe --java-optional_out=./out/directory ./protos/sample.proto

With some parameters:

protoc --proto-path=./protos/ --java_out=./out/directory --plugin=protoc-gen-java-optional=./path/to/plugin/protoc-gen-java-optional.exe --java-optional_out=setter_optional=true,use_primitive_optionals=true:./out/directory ./protos/sample.proto

Using protobuf-maven-plugin


<build>
  <extensions>
    <extension>
      <groupId>kr.motd.maven</groupId>
      <artifactId>os-maven-plugin</artifactId>
      <version>${os-maven-plugin.version}</version>
    </extension>
  </extensions>
  <plugins>
    <plugin>
      <groupId>org.xolstice.maven.plugins</groupId>
      <artifactId>protobuf-maven-plugin</artifactId>
      <version>${protobuf-maven-plugin.version}</version>
      <executions>
        <execution>
          <id>protobuf</id>
          <goals>
            <goal>compile</goal>
          </goals>
          <phase>generate-sources</phase>
          <configuration>
            <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
            <protoSourceRoot>${project.basedir}/src/test/resources/</protoSourceRoot>
            <useArgumentFile>true</useArgumentFile>
            <protocPlugins>
              <protocPlugin>
                <id>java-optional</id>
                <groupId>org.grpcmock</groupId>
                <artifactId>protoc-gen-java-optional</artifactId>
                <version>1.10.0</version>
                <mainClass>org.grpcmock.protoc.plugin.OptionalGenerator</mainClass>
              </protocPlugin>
            </protocPlugins>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

This plugin does not support passing java protoc plugin parameters until https://github.com/xolstice/protobuf-maven-plugin/issues/56 is solved, so default options will be used.

Using protoc-jar-maven-plugin

This maven plugin is not supported as it does not support running multiple output types as a single protoc command. This protoc plugin needs to be run together with the java_out target in order to extend generated classes.

Configuration parameters