Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To compile Protocol Buffers with Maven, add the Protocol Buffers Maven Plugin to your pom.xml, make protoc available, add the matching Java runtime dependency, and run the plugin’s compile goal. Put application schemas in src/main/proto; add test-compile only if your tests have their own .proto files.

Configure Maven to generate Java from your schemas

The Maven Protocol Buffers Plugin runs protoc and generates source code during the build. Its documented default locations are src/main/proto for application schemas and src/test/proto for test schemas. Subdirectories beneath those paths can mirror package-like directory structures used for imports. See the plugin usage guide.

Add the plugin and runtime dependency to your project. The version fields below are deliberately not filled in: the usage guide’s examples use plugin version 0.6.1 and protobuf-java version 3.4.0, but those are historical examples, not current version recommendations. Check Maven Central for released versions and select compatible compiler and runtime versions before using this configuration.

<build>
  <plugins>
    <plugin>
      <groupId>org.xolstice.maven.plugins</groupId>
      <artifactId>protobuf-maven-plugin</artifactId>
      <version>REPLACE_WITH_VERIFIED_RELEASE</version>
      <configuration>
        <!-- Optional: set this if protoc is not on PATH. -->
        <protocExecutable>/path/to/protoc</protocExecutable>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
            <!-- Add test-compile only if src/test/proto contains schemas. -->
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

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

Replace both version markers with actual compatible versions; they are explanatory text, not valid Maven versions. The plugin documentation recommends using the same version for protoc and protobuf-java where possible. The plugin itself is separate from Maven’s default lifecycle, so its execution must be declared. The compile goal defaults to the generate-sources phase, so an explicit <phase> element is generally unnecessary.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make the Protocol Buffers compiler available

The plugin invokes protoc. Choose one provisioning method and ensure the compiler is available wherever the Maven build runs, including CI agents:

  • Use PATH: install or otherwise provision protoc so the build environment can find it.
  • Set protocExecutable: configure the plugin with the executable’s path, as in the XML example.
  • Use Maven toolchains: the plugin guide documents a toolchain approach for selecting the compiler.

The plugin API lists compile as generating main sources and using dependency artifacts that contain .proto files as import paths. It also adds proto files as project resources. Goal details are listed in the compile goal API reference.

Run the build and generate test schemas only when needed

With the plugin execution declared, run your normal Maven build, such as mvn compile, to invoke the bound protobuf:compile goal during generate-sources. To invoke the goal directly, use mvn protobuf:compile.

If test code defines schemas under src/test/proto, add test-compile to the execution’s goals. That separate goal generates test sources; it is unnecessary when tests consume only application-generated classes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose another output language or a custom generator when appropriate

The plugin documentation lists goals for generating C++, C#, JavaScript, Python, and other outputs, as well as custom generators. Use the goal that matches the project’s intended language rather than assuming the Java configuration produces every target. The available goals and their behavior are listed in the plugin API index.

For a custom protoc generator, the plugin supports Java plugins resolved as Maven artifacts and native plugins. Use compile-custom for main schemas and test-compile-custom for test schemas; Java plugin configuration identifies artifact coordinates and the plugin’s main class. Check the generator’s own current version and compatibility. See the custom generator example.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common build problems

  • Maven cannot find protoc: verify it is on the build environment’s PATH, set protocExecutable, or configure the documented Maven toolchain.
  • Generated Java does not compile: check compatibility between the protoc compiler and protobuf-java runtime. The usage guide recommends matching their versions where possible.
  • The command line is too long: for protoc 3.5.0 or newer, the guide documents the plugin’s useArgumentFile option. With older compiler versions, split compilation into smaller chunks, for example by using separate Maven modules.
  • The plugin keeps regenerating unchanged output: review the documented checkStaleness setting. Builds on NFS may also need an appropriate staleMillis value.
  • Test schemas are not generated: add the separate test-compile goal to the plugin execution.

Because version availability can change, distinguish a released artifact from a snapshot when choosing the plugin version. The usage and custom-generator pages are dated 2018; Sonatype Central lists 0.6.1, while the repository’s master POM shows 0.7.0-SNAPSHOT. A snapshot POM does not establish a newer stable release, so verify the release in Maven Central before pinning it. No build result is implied by this configuration.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.