Maven Protocol Buffers Plugin generates Java, C++ or Python sources from .proto files using the protoc tool. The following examples describe the basic usage of the Plugin.
The following default directory structure of the project is assumed:
./ +- pom.xml +- src/ +- main/ +- proto/ +- message.proto +- test/ +- proto/ +- test_message.proto
Protocol buffer definitions are looked up under src/main/proto/ directory by default. Any subdirectories under src/main/proto/ are treated as package structure for protobuf definition imports. Similarly, protobuf definitions for use in tests are looked up under src/test/proto/ directory by default.
A minimal configuration to invoke this plugin would look like:
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocExecutable>/usr/local/bin/protoc</protocExecutable> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> ... </plugins> </build> <dependencies> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.4.0</version> </dependency> ... </dependencies> ... </project>
The following conditions need to be met:
As soon as everything is set up, execute the following goals to build the project:
mvn clean install
This plugin simply invokes protoc binary, passing all necessary arguments on the command line. In some configurations with a very large number of protobuf definitions this may be a problem, due to the limits on command line length in the host operating system. Starting with protoc version 3.5.0 this can be worked around by passing the following option:
<plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> ... <useArgumentFile>true</useArgumentFile> </configuration> ... </plugin>
Unfortunately, for versions of protoc below 3.5.0, the only available option is to split the compilation into smaller chunks by decomposing the project into modules.
The plugin configuration is similar to compiling into Java, with the following alterations:
It is possible to run Maven Protocol Buffers Plugin goals from the command line, even if the plugin is not configured in the project:
mvn protobuf:compile -DprotocExecutable="C:/Java/protobuf-2.4.1/bin/protoc.exe" mvn protobuf:test-compile -DprotocExecutable="C:/Java/protobuf-2.4.1/bin/protoc.exe"
If a protobuf toolchain is configured in the project, then the toolchains plugin needs to be executed first and there is no need to specify the protocExecutable parameter.
mvn toolchains:toolchain protobuf:compile mvn toolchains:toolchain protobuf:test-compile
Normally this plugin invokes protoc compilation on every execution, but this can be overridden by the following configuration option:
<plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> ... <checkStaleness>true</checkStaleness> </configuration> ... </plugin>
If the project is built on NFS, the following setting may also be needed:
<plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> ... <checkStaleness>true</checkStaleness> <staleMillis>10000</staleMillis> </configuration> ... </plugin>
It is possible to output binary FileDescriptorSet files containing all the descriptor metadata for generated classes. Descriptor sets are written by passing the --descriptor_set_out and --include_imports arguments to protoc.
Generated descriptor sets can optionally be attached to the build as artifacts. The default type and extension for descriptor sets is protobin, and the plugin extensions need to be enabled in order to support the correct resolution of those dependencies in downstream projects.
The following plugin options configure descriptor set output: