如何使用protobuf-maven-plugin为protoc获取外部.proto文件?
Hey there! I get exactly where you're coming from—you're comfy with Maven, diving into gRPC/Protobuf for the first time, and you need to work with external .proto files (like Envoy's v2 API definitions) for your EDS service. Let's walk through the most reliable ways to make this work with the protobuf-maven-plugin:
1. Pull External Protos via Maven Dependencies (Best for Official APIs like Envoy)
Most official proto collections (including Envoy's API) are packaged as Maven artifacts, which is the easiest route. Here's how to set this up:
Step 1: Add Required Dependencies to pom.xml
First, include the Envoy API dependency (match the version to your Envoy instance) plus the core gRPC dependencies:
<dependencies> <!-- Envoy v2 API proto dependency --> <dependency> <groupId>io.envoyproxy.envoy</groupId> <artifactId>envoy-api</artifactId> <version>v1.22.0</version> <!-- Swap this for your Envoy version --> <type>pom</type> </dependency> <!-- Core gRPC dependencies --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty-shaded</artifactId> <version>1.44.0</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> <version>1.44.0</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> <version>1.44.0</version> </dependency> </dependencies>
Step 2: Configure the protobuf-maven-plugin
Set up the plugin to pull the external protos from the Maven dependency, generate Java and gRPC code, and ensure Maven recognizes the generated sources:
<build> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <!-- Match protoc version to your gRPC setup --> <protocArtifact>com.google.protobuf:protoc:3.19.4:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.44.0:exe:${os.detected.classifier}</pluginArtifact> <!-- Define which external protos to pull from the Envoy dependency --> <protocInputs> <protocInput> <groupId>io.envoyproxy.envoy</groupId> <artifactId>envoy-api</artifactId> <version>v1.22.0</version> <includes> <include>envoy/api/v2/**/*.proto</include> <include>envoy/api/v2/core/**/*.proto</include> <!-- Add any other Envoy proto paths you need for EDS --> </includes> </protocInput> </protocInputs> <!-- Output directories for generated code --> <outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory> <grpcOutputDirectory>${project.build.directory}/generated-sources/protobuf/grpc-java</grpcOutputDirectory> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> <!-- Make sure Maven picks up the generated code as source files --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${project.build.directory}/generated-sources/protobuf/java</source> <source>${project.build.directory}/generated-sources/protobuf/grpc-java</source> </sources> </configuration> </execution> </executions> </plugin> </plugins> </build>
2. Use Local External Protos (For Custom/Unpublished Files)
If you have external protos that aren't available as Maven artifacts, drop them into a directory like src/main/protobuf/external in your project, then update the plugin's protoPaths to include that directory:
<configuration> <!-- ...keep your existing plugin config... --> <protoPaths> <protoPath>${project.basedir}/src/main/protobuf</protoPath> <protoPath>${project.basedir}/src/main/protobuf/external</protoPath> </protoPaths> <includes> <include>**/*.proto</include> </includes> </configuration>
Protoc will now search all specified protoPath directories for any referenced .proto files.
3. Critical Things to Remember
- Version Compatibility: Double-check that your
protocversion,grpc-javaplugin version, and Envoy API version all play nice together—mismatches cause weird compilation errors. - Import Paths Match: When you import external protos in your custom .proto files, use the full canonical path (e.g.,
import "envoy/api/v2/endpoint.proto";) instead of local relative paths. This matches how the files are structured in the Maven dependency. - Source Directory Setup: Don't skip the build-helper-maven-plugin step—without it, Maven won't recognize the generated code as part of your project, and you'll get "cannot find symbol" errors during compilation.
Once everything is set up, run mvn clean compile and the plugin will handle pulling/generating all the code you need for your Envoy EDS service.
内容的提问来源于stack exchange,提问作者JakeRobb

