This is an automated email from the ASF dual-hosted git repository.

crazyhzm pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3 by this push:
     new 865f68bdc5 Enhance compiler configuration (#13342)
865f68bdc5 is described below

commit 865f68bdc562c8f1144888f1900671fe3af42ac2
Author: Albumen Kevin <[email protected]>
AuthorDate: Fri Nov 10 11:27:17 2023 +0800

    Enhance compiler configuration (#13342)
    
    * Enhance compiler configuration
    
    * comment
---
 dubbo-maven-plugin/pom.xml                         | 21 ++++++
 .../plugin/protoc/DubboProtocCompilerMojo.java     | 79 ++++++++++++++++++++--
 .../src/main/resources/version-matrix.properties   |  2 +
 dubbo-plugin/dubbo-compiler/README.md              | 18 ++---
 4 files changed, 107 insertions(+), 13 deletions(-)

diff --git a/dubbo-maven-plugin/pom.xml b/dubbo-maven-plugin/pom.xml
index 254e9f0324..24dec2d1ac 100644
--- a/dubbo-maven-plugin/pom.xml
+++ b/dubbo-maven-plugin/pom.xml
@@ -27,6 +27,11 @@
   <packaging>maven-plugin</packaging>
   <description>Dubbo Maven Plugin</description>
 
+  <properties>
+    <dubbo.version>${revision}</dubbo.version>
+    <protobuf-java.version>3.25.0</protobuf-java.version>
+  </properties>
+
   <dependencies>
     <dependency>
       <groupId>org.apache.maven</groupId>
@@ -66,6 +71,15 @@
       <version>2.11.0</version>
     </dependency>
 
+    <!-- Takes no effect for this dependency. To notify github dependencies 
bot to update ${protobuf-java.version} property.  -->
+    <dependency>
+      <groupId>com.google.protobuf</groupId>
+      <artifactId>protobuf-java</artifactId>
+      <version>${protobuf-java.version}</version>
+      <scope>test</scope>
+      <optional>true</optional>
+    </dependency>
+
     <dependency>
       <groupId>org.sonatype.plexus</groupId>
       <artifactId>plexus-build-api</artifactId>
@@ -80,6 +94,12 @@
   </dependencies>
 
   <build>
+    <resources>
+      <resource>
+        <filtering>true</filtering>
+        <directory>src/main/resources</directory>
+      </resource>
+    </resources>
     <plugins>
       <plugin>
         <artifactId>maven-plugin-plugin</artifactId>
@@ -101,6 +121,7 @@
           </execution>
         </executions>
       </plugin>
+
     </plugins>
   </build>
 </project>
diff --git 
a/dubbo-maven-plugin/src/main/java/org/apache/dubbo/maven/plugin/protoc/DubboProtocCompilerMojo.java
 
b/dubbo-maven-plugin/src/main/java/org/apache/dubbo/maven/plugin/protoc/DubboProtocCompilerMojo.java
index e2c88b6c7e..64fe3e923a 100644
--- 
a/dubbo-maven-plugin/src/main/java/org/apache/dubbo/maven/plugin/protoc/DubboProtocCompilerMojo.java
+++ 
b/dubbo-maven-plugin/src/main/java/org/apache/dubbo/maven/plugin/protoc/DubboProtocCompilerMojo.java
@@ -17,6 +17,7 @@ package org.apache.dubbo.maven.plugin.protoc;
 
 import 
org.apache.dubbo.maven.plugin.protoc.command.DefaultProtocCommandBuilder;
 import org.apache.dubbo.maven.plugin.protoc.enums.DubboGenerateTypeEnum;
+
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -31,8 +32,10 @@ import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectHelper;
 import org.apache.maven.repository.RepositorySystem;
@@ -46,25 +49,33 @@ import org.sonatype.plexus.build.incremental.BuildContext;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Locale;
+import java.util.Properties;
 import java.util.Set;
-import java.util.Arrays;
 
 import static java.lang.String.format;
 import static java.util.Collections.emptyMap;
 import static java.util.Collections.singleton;
 import static org.codehaus.plexus.util.FileUtils.getFiles;
 
-@Mojo(name = "dubbo-protoc-compiler")
+@Mojo(
+    name = "compile",
+    defaultPhase = LifecyclePhase.GENERATE_SOURCES,
+    requiresDependencyResolution = ResolutionScope.COMPILE,
+    threadSafe = true
+)
 public class DubboProtocCompilerMojo extends AbstractMojo {
     @Parameter(property = "protoSourceDir", defaultValue = 
"${basedir}/src/main/proto")
     private File protoSourceDir;
     @Parameter(property = "outputDir", defaultValue = 
"${project.build.directory}/generated-sources/protobuf/java")
     private File outputDir;
-    @Parameter(required = true, property = "dubboVersion", defaultValue = 
"${dubbo.version}")
+    @Parameter(required = false, property = "dubboVersion")
     private String dubboVersion;
     @Parameter(required = true, readonly = true, defaultValue = 
"${project.remoteArtifactRepositories}")
     private List<ArtifactRepository> remoteRepositories;
@@ -72,9 +83,11 @@ public class DubboProtocCompilerMojo extends AbstractMojo {
     private String protocExecutable;
     @Parameter(required = false, property = "protocArtifact")
     private String protocArtifact;
+    @Parameter(required = false, property = "protocVersion")
+    private String protocVersion;
     @Parameter(required = false, defaultValue = 
"${project.build.directory}/protoc-plugins")
     private File protocPluginDirectory;
-    @Parameter(required = true, property = "dubboGenerateType", defaultValue = 
"dubbo3")
+    @Parameter(required = true, property = "dubboGenerateType", defaultValue = 
"tri")
     private String dubboGenerateType;
     @Parameter(defaultValue = "${project}", readonly = true)
     protected MavenProject project;
@@ -98,6 +111,31 @@ public class DubboProtocCompilerMojo extends AbstractMojo {
     private final DubboProtocPluginWrapperFactory 
dubboProtocPluginWrapperFactory = new DubboProtocPluginWrapperFactory();
 
     public void execute() throws MojoExecutionException, MojoFailureException {
+        Properties versionMatrix = new Properties();
+        ClassLoader loader = Thread.currentThread().getContextClassLoader();
+        InputStream stream = 
loader.getResourceAsStream("version-matrix.properties");
+        try {
+            versionMatrix.load(stream);
+        } catch (IOException e) {
+            getLog().warn("Unable to load default version matrix", e);
+        }
+
+        if (dubboVersion == null) {
+            dubboVersion = versionMatrix.getProperty("dubbo.version");
+        }
+        if (protocVersion == null) {
+            protocVersion = versionMatrix.getProperty("protoc.version");
+        }
+        if (protocArtifact == null) {
+            final String osName = System.getProperty("os.name");
+            final String osArch = System.getProperty("os.arch");
+
+            final String detectedName = normalizeOs(osName);
+            final String detectedArch = normalizeArch(osArch);
+
+            protocArtifact = "com.google.protobuf:protoc:" + protocVersion + 
":exe:" + detectedName + '-' + detectedArch;
+        }
+
         if (protocExecutable == null && protocArtifact != null) {
             final Artifact artifact = createProtocArtifact(protocArtifact);
             final File file = resolveBinaryArtifact(artifact);
@@ -133,6 +171,39 @@ public class DubboProtocCompilerMojo extends AbstractMojo {
         }
     }
 
+    private static String normalizeOs(String value) {
+        value = normalize(value);
+        if (value.startsWith("linux")) {
+            return "linux";
+        }
+        if (value.startsWith("mac") || value.startsWith("osx")) {
+            return "osx";
+        }
+        if (value.startsWith("windows")) {
+            return "windows";
+        }
+
+        return "unknown";
+    }
+
+    private static String normalize(String value) {
+        if (value == null) {
+            return "";
+        }
+        return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", "");
+    }
+
+    private static String normalizeArch(String value) {
+        value = normalize(value);
+        if (value.matches("^(x8664|amd64|ia32e|em64t|x64)$")) {
+            return "x86_64";
+        }
+        if ("aarch64".equals(value)) {
+            return "aarch_64";
+        }
+        return "unknown";
+    }
+
     public void linkProtoFilesToMaven() {
         linkProtoSources();
         linkGeneratedFiles();
diff --git a/dubbo-maven-plugin/src/main/resources/version-matrix.properties 
b/dubbo-maven-plugin/src/main/resources/version-matrix.properties
new file mode 100644
index 0000000000..10b01bab6e
--- /dev/null
+++ b/dubbo-maven-plugin/src/main/resources/version-matrix.properties
@@ -0,0 +1,2 @@
+dubbo.version=${dubbo.version}
+protoc.version=${protobuf-java.version}
diff --git a/dubbo-plugin/dubbo-compiler/README.md 
b/dubbo-plugin/dubbo-compiler/README.md
index f3e13e5a1a..3234723fb9 100644
--- a/dubbo-plugin/dubbo-compiler/README.md
+++ b/dubbo-plugin/dubbo-compiler/README.md
@@ -39,18 +39,18 @@ message HelloReply {
     now dubbo support his own protoc plugin base on dubbo-maven-plugin
 
 ```xml
-  <plugin>
+<plugin>
     <groupId>org.apache.dubbo</groupId>
     <artifactId>dubbo-maven-plugin</artifactId>
-    <version>3.3.0</version>
-    <configuration>
-        <dubboVersion>3.3.0</dubboVersion>
-        <dubboGenerateType>dubbo3</dubboGenerateType>
-        <protocExecutable>protoc</protocExecutable>
-        
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
-    </configuration>
+    <version>${dubbo.version}</version>
+    <executions>
+        <execution>
+            <goals>
+                <goal>compile</goal>
+            </goals>
+        </execution>
+    </executions>
 </plugin>
-
 ```
 
 #### 3.generate file

Reply via email to