Search code examples
javamavenprotocol-buffersgrpcgrpc-java

Maven and java: how to generate code from protobuf files in test directory?


My question is very similar to this question but for maven and java.

I am testing grpc, and want to put to a simple helloworld.proto in the test/proto folder.

However the file doesn't generate a java file (unlike the proto file in /src/main/proto).

So my question is how to generate code for proto in the test folder?


Solution

  • First, follow the documentation to use the org.xolstice.maven.plugins protobuf-maven-plugin.

    Alternatively, you can copy the example pom.xml (this is pinned to the v1.19.0 release; consider using whatever the latest tag is). This pom is used by the helloworld example, among others.

    Then add the test-compile and test-compile-custom goals for the protobuf-maven-plugin. This will cause files in src/test/proto to be generated.

          <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.5.1</version>
            <configuration>
              <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
              <pluginId>grpc-java</pluginId>
              <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
            </configuration>
            <executions>
              <execution>
                <goals>
                  <goal>compile</goal>
                  <goal>compile-custom</goal>
                  <goal>test-compile</goal>
                  <goal>test-compile-custom</goal>
                </goals>
              </execution>
            </executions>
          </plugin>