From 337d8c49eb0d2473eef9edf1cf807b8dc115e6e0 Mon Sep 17 00:00:00 2001 From: 16K <767743748@qq.com> Date: Wed, 8 Nov 2023 15:02:22 +0800 Subject: [PATCH] Gradle dsl to specify the mainClass for protocPlugin --- README.md | 5 +++++ .../google/protobuf/gradle/ExecutableLocator.groovy | 9 +++++++++ .../google/protobuf/gradle/GenerateProtoTask.groovy | 10 +++++----- .../com/google/protobuf/gradle/ToolsLocator.groovy | 5 +++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 48b27750..9955ac40 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,11 @@ protobuf {      // or      // path = 'tools/protoc-gen-grpc-java'    } + dubbo { + artifact = "org.apache.dubbo:dubbo-compiler:${dubboVersion}" + // optional (jar main-class) + // mainClass = "org.apache.dubbo.gen.grpc.reactive.ReactorDubboGrpcGenerator" + } // Any other plugins ...  } diff --git a/src/main/groovy/com/google/protobuf/gradle/ExecutableLocator.groovy b/src/main/groovy/com/google/protobuf/gradle/ExecutableLocator.groovy index b20b5b7b..7ba9cfd4 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ExecutableLocator.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ExecutableLocator.groovy @@ -46,6 +46,7 @@ class ExecutableLocator implements Named { private String artifact private String path + private String mainClass; private FileCollection artifactFiles private String simplifiedArtifactName @@ -84,6 +85,14 @@ class ExecutableLocator implements Named { return path } + String getMainClass() { + return mainClass + } + + void setMainClass(String mainClass) { + this.mainClass = mainClass + } + @PackageScope FileCollection getArtifactFiles() { Preconditions.checkState(path == null, 'Not artifact based') diff --git a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy index 4ce6d6d1..940caba1 100644 --- a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy @@ -721,11 +721,11 @@ public abstract class GenerateProtoTask extends DefaultTask { protected String computeExecutablePath(ExecutableLocator locator) { if (locator.path != null) { - return locator.path.endsWith(JAR_SUFFIX) ? createJarTrampolineScript(locator.path) : locator.path + return locator.path.endsWith(JAR_SUFFIX) ? createJarTrampolineScript(locator.path, locator.mainClass) : locator.path } File file = locator.artifactFiles.singleFile if (file.name.endsWith(JAR_SUFFIX)) { - return createJarTrampolineScript(file.getAbsolutePath()) + return createJarTrampolineScript(file.getAbsolutePath(), locator.mainClass) } if (!file.canExecute() && !file.setExecutable(true)) { @@ -742,7 +742,7 @@ public abstract class GenerateProtoTask extends DefaultTask { * @param jarAbsolutePath Absolute path to the .jar file. * @return The absolute path to the trampoline executable script. */ - private String createJarTrampolineScript(String jarAbsolutePath) { + private String createJarTrampolineScript(String jarAbsolutePath, String mainClass) { assert jarAbsolutePath.endsWith(JAR_SUFFIX) boolean isWindows = isWindows() String jarFileName = new File(jarAbsolutePath).getName() @@ -758,8 +758,8 @@ public abstract class GenerateProtoTask extends DefaultTask { // Rewrite the trampoline file unconditionally (even if it already exists) in case the dependency or versioning // changes we don't need to detect the delta (and the file content is cheap to re-generate). String trampoline = isWindows ? - "@ECHO OFF\r\n\"${escapePathWindows(javaExe)}\" -jar \"${escapePathWindows(jarAbsolutePath)}\" %*\r\n" : - "#!/bin/sh\nexec '${escapePathUnix(javaExe)}' -jar '${escapePathUnix(jarAbsolutePath)}' \"\$@\"\n" + "@ECHO OFF\r\n\"${escapePathWindows(javaExe)}\" ${mainClass ? "-cp" : "-jar"} \"${escapePathWindows(jarAbsolutePath)}\" ${mainClass} %*\r\n" : + "#!/bin/sh\nexec '${escapePathUnix(javaExe)}' ${mainClass ? "-cp" : "-jar"} '${escapePathUnix(jarAbsolutePath)}' ${mainClass} \"\$@\"\n" scriptExecutableFile.write(trampoline, US_ASCII.name()) setExecutableOrFail(scriptExecutableFile) logger.info("Resolved artifact jar: ${jarAbsolutePath}. Created trampoline file: ${scriptExecutableFile}") diff --git a/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy b/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy index de70447a..5a3c0f1f 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy @@ -96,6 +96,7 @@ class ToolsLocator { conf.visible = false conf.transitive = false } + def mainClass = locator.mainClass String groupId, artifact, version, classifier, extension OsDetector osdetector = project.extensions.getByName("osdetector") as OsDetector List parts = artifactParts(locator.artifact) @@ -104,8 +105,8 @@ class ToolsLocator { group:groupId, name:artifact, version:version, - classifier:classifier ?: osdetector.classifier, - ext:extension ?: 'exe', + classifier:classifier ?: mainClass ? null : osdetector.classifier, + ext:extension ?: mainClass ? 'jar' : 'exe', ] project.dependencies.add(config.name, notation) locator.resolve(config, "$groupId:$artifact:$version".toString())