Skip to content

Commit

Permalink
Gradle dsl to specify the mainClass for protocPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
016K committed Nov 8, 2023
1 parent 4ec03ff commit 337d8c4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ExecutableLocator implements Named {

private String artifact
private String path
private String mainClass;

private FileCollection artifactFiles
private String simplifiedArtifactName
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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()
Expand All @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> parts = artifactParts(locator.artifact)
Expand All @@ -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())
Expand Down

0 comments on commit 337d8c4

Please sign in to comment.