Enhance compiler configuration (#13342)

* Enhance compiler configuration

* comment
This commit is contained in:
Albumen Kevin 2023-11-10 11:27:17 +08:00 committed by GitHub
parent 2b62e88974
commit 865f68bdc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 13 deletions

View File

@ -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>

View File

@ -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();

View File

@ -0,0 +1,2 @@
dubbo.version=${dubbo.version}
protoc.version=${protobuf-java.version}

View File

@ -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