Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradle dsl to specify the mainClass for protocPlugin #738

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this doesn't actually work for other people, I don't think it makes sense as an example. Looks like we don't have an example already for jar files? Oh, well. servicetalk might be a fair example if we were searching for one.

// 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,13 @@ 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 +744,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 +760,10 @@ 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
10 changes: 5 additions & 5 deletions src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ class ToolsLocator {
List<String> parts = artifactParts(locator.artifact)
(groupId, artifact, version, classifier, extension) = [parts[0], parts[1], parts[2], parts[3], parts[4]]
Map<String, String> notation = [
group:groupId,
name:artifact,
version:version,
classifier:classifier ?: osdetector.classifier,
ext:extension ?: 'exe',
group:groupId,
name:artifact,
version:version,
classifier:classifier ?: osdetector.classifier,
ext:extension ?: 'exe',
]
project.dependencies.add(config.name, notation)
locator.resolve(config, "$groupId:$artifact:$version".toString())
Expand Down
Loading