Configuration of the Dubbo Maven Plugin

Configuration of the Dubbo Maven Plugin

This article mainly explains the configuration of the Dubbo Maven Plugin.

The current Dubbo Maven Plugin supports the following features:

  • Dubbo Maven Plugin For Protobuf: Generates Stub code for Dubbo service interfaces
  • Dubbo Maven Plugin For Native Image: Generates necessary Metadata information for Native Image based on AOT mechanism

Dubbo Maven Plugin For Protobuf

How to use Dubbo Maven Plugin to generate Protobuf Stub code

1. Write the .proto file

greeter.proto

  1. syntax = "proto3";
  2. option java_multiple_files = true;
  3. option java_package = "org.apache.dubbo.demo";
  4. option java_outer_classname = "DemoServiceProto";
  5. option objc_class_prefix = "DEMOSRV";
  6. package demoservice;
  7. // The demo service definition.
  8. service DemoService {
  9. rpc SayHello (HelloRequest) returns (HelloReply) {}
  10. }
  11. // The request message containing the user's name.
  12. message HelloRequest {
  13. string name = 1;
  14. }
  15. // The response message containing the greetings
  16. message HelloReply {
  17. string message = 1;
  18. }

2. Include the dubbo-maven-plugin dependency

  1. <plugin>
  2. <groupId>org.apache.dubbo</groupId>
  3. <artifactId>dubbo-maven-plugin</artifactId>
  4. <version>${dubbo.version}</version>
  5. <executions>
  6. <execution>
  7. <goals>
  8. <goal>compile</goal>
  9. </goals>
  10. </execution>
  11. </executions>
  12. </plugin>

3. Example of generated file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.dubbo.demo;
  18. import java.util.concurrent.CompletableFuture;
  19. import java.util.concurrent.atomic.AtomicBoolean;
  20. public final class DemoServiceDubbo {
  21. private static final AtomicBoolean registered = new AtomicBoolean();
  22. private static Class<?> init() {
  23. Class<?> clazz = null;
  24. try {
  25. clazz = Class.forName(DemoServiceDubbo.class.getName());
  26. if (registered.compareAndSet(false, true)) {
  27. org.apache.dubbo.common.serialize.protobuf.support.ProtobufUtils.marshaller(
  28. org.apache.dubbo.demo.HelloReply.getDefaultInstance());
  29. org.apache.dubbo.common.serialize.protobuf.support.ProtobufUtils.marshaller(
  30. org.apache.dubbo.demo.HelloRequest.getDefaultInstance());
  31. }
  32. } catch (ClassNotFoundException e) {
  33. }
  34. return clazz;
  35. }
  36. private DemoServiceDubbo() {
  37. }
  38. public static final String SERVICE_NAME = "org.apache.dubbo.demo.DemoService";
  39. /**
  40. * Code generated for Dubbo
  41. */
  42. public interface IDemoService extends org.apache.dubbo.rpc.model.DubboStub {
  43. static Class<?> clazz = init();
  44. org.apache.dubbo.demo.HelloReply sayHello(org.apache.dubbo.demo.HelloRequest request);
  45. CompletableFuture<org.apache.dubbo.demo.HelloReply> sayHelloAsync(org.apache.dubbo.demo.HelloRequest request);
  46. }
  47. }

Configuration List

Dubbo Maven Plugin For Protobuf supports the following configurations:

Configuration ParameterRequiredDescriptionDefault ValueExample
dubboVersiontrueDubbo version number, used to find the dubbo-compiler component${dubbo.version}3.3.0
dubboGenerateTypetrueType of the generated filetritri or tri-reactor
protocExecutablefalsePath to the executable protoc; if not configured, it will be downloaded from Maven repositoryprotoc
protocArtifactfalseThe protocol used to locate protoc in Maven repository, if not configured, it will automatically detect the OS type and downloadcom.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
protoSourceDirtrueDirectory for proto files${basedir}/src/main/proto./proto
outputDirtrueTarget directory for generated files${project.build.directory}/generated-sources/protobuf/java${basedir}/src/main/java
protocPluginDirectoryfalseDirectory for temporary storage of protoc${project.build.directory}/protoc-plugins./target/protoc-plugins

Dubbo Maven Plugin For Native Image

Example

  1. <plugin>
  2. <groupId>org.apache.dubbo</groupId>
  3. <artifactId>dubbo-maven-plugin</artifactId>
  4. <version>${dubbo.version}</version>
  5. <configuration>
  6. <mainClass>com.example.nativedemo.NativeDemoApplication</mainClass>
  7. </configuration>
  8. <executions>
  9. <execution>
  10. <phase>process-sources</phase>
  11. <goals>
  12. <goal>dubbo-process-aot</goal>
  13. </goals>
  14. </execution>
  15. </executions>
  16. </plugin>

Configuration List

Dubbo Maven Plugin For Native Image supports the following configurations:

Configuration ParameterRequiredDescriptionDefault ValueExample
mainClasstrueName of the main classcom.example.nativedemo.NativeDemoApplication

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)