简体   繁体   中英

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?

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

Alternatively, you can copy theexample 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM