-
-
Notifications
You must be signed in to change notification settings - Fork 423
Fix various issues about runShadow and polish ShadowApplicationPlugin
#1956
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
Closed
+107
−84
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 99 additions & 82 deletions
181
src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowApplicationPlugin.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,137 +1,154 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow | ||
|
|
||
| import com.github.jengelman.gradle.plugins.shadow.internal.JavaJarExec | ||
| import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar | ||
| import org.gradle.api.GradleException | ||
| import org.gradle.api.Plugin | ||
| import org.gradle.api.Project | ||
| import org.gradle.api.distribution.Distribution | ||
| import org.gradle.api.distribution.DistributionContainer | ||
| import org.gradle.api.file.CopySpec | ||
| import org.gradle.api.plugins.ApplicationPlugin | ||
| import org.gradle.api.plugins.JavaApplication | ||
| import org.gradle.api.plugins.JavaPluginExtension | ||
| import org.gradle.api.provider.Provider | ||
| import org.gradle.api.tasks.JavaExec | ||
| import org.gradle.api.tasks.Sync | ||
| import org.gradle.api.tasks.TaskProvider | ||
| import org.gradle.api.tasks.application.CreateStartScripts | ||
| import org.gradle.jvm.toolchain.JavaLauncher | ||
| import org.gradle.jvm.toolchain.JavaToolchainService | ||
|
|
||
| class ShadowApplicationPlugin implements Plugin<Project> { | ||
| /** | ||
| * A {@link Plugin} which packages and runs a project as a Java Application using the shadowed jar. | ||
| * | ||
| * Modified from | ||
| * <a href="https://github.com/gradle/gradle/blob/fdecc3c95828bb9a1c1bb6114483fe5b16f9159d/platforms/jvm/plugins-application/src/main/java/org/gradle/api/plugins/ApplicationPlugin.java">org.gradle.api.plugins.ApplicationPlugin.java</a>. | ||
| * | ||
| * @see ApplicationPlugin | ||
| */ | ||
| abstract class ShadowApplicationPlugin implements Plugin<Project> { | ||
|
|
||
| public static final String SHADOW_RUN_TASK_NAME = 'runShadow' | ||
| public static final String SHADOW_SCRIPTS_TASK_NAME = 'startShadowScripts' | ||
| public static final String SHADOW_INSTALL_TASK_NAME = 'installShadowDist' | ||
|
|
||
| private Project project | ||
| private JavaApplication javaApplication | ||
| private static final String DISTRIBUTION_NAME = ShadowBasePlugin.EXTENSION_NAME | ||
|
|
||
| @Override | ||
| void apply(Project project) { | ||
| this.project = project | ||
| this.javaApplication = project.extensions.getByType(JavaApplication) | ||
|
|
||
| DistributionContainer distributions = project.extensions.getByName("distributions") as DistributionContainer | ||
| Distribution distribution = distributions.create("shadow") | ||
|
|
||
| addRunTask(project) | ||
| addCreateScriptsTask(project) | ||
|
|
||
| configureDistSpec(project, distribution.contents) | ||
|
|
||
| configureDistribution(project) | ||
| configureJarMainClass(project) | ||
| configureInstallTask(project) | ||
| } | ||
|
|
||
| protected void configureJarMainClass(Project project) { | ||
| def classNameProvider = javaApplication.mainClass | ||
| jar.configure { jar -> | ||
| jar.inputs.property('mainClassName', classNameProvider) | ||
| jar.doFirst { | ||
| jar.manifest.attributes 'Main-Class': classNameProvider.get() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected void addRunTask(Project project) { | ||
| project.tasks.register(SHADOW_RUN_TASK_NAME, JavaExec) { task -> | ||
| task.description = "Runs this project as a JVM application using the shadow jar" | ||
| task.group = ApplicationPlugin.APPLICATION_GROUP | ||
|
|
||
| project.tasks.register(SHADOW_RUN_TASK_NAME, JavaJarExec) { run -> | ||
| def install = project.tasks.named(SHADOW_INSTALL_TASK_NAME, Sync) | ||
| run.dependsOn SHADOW_INSTALL_TASK_NAME | ||
| run.mainClass.set('-jar') | ||
| run.description = 'Runs this project as a JVM application using the shadow jar' | ||
| run.group = ApplicationPlugin.APPLICATION_GROUP | ||
| run.conventionMapping.jvmArgs = { javaApplication.applicationDefaultJvmArgs } | ||
| run.conventionMapping.jarFile = { | ||
| project.file("${install.get().destinationDir.path}/lib/${jar.get().archiveFile.get().asFile.name}") | ||
| } | ||
| configureJavaLauncher(run) | ||
| } | ||
| } | ||
| task.classpath = project.files(project.tasks.named(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME)) | ||
|
|
||
| def applicationExtension = project.extensions.getByType(JavaApplication) | ||
| def javaPluginExtension = project.extensions.getByType(JavaPluginExtension) | ||
| def javaToolchainService = project.extensions.getByType(JavaToolchainService) | ||
|
|
||
| task.mainModule.convention(applicationExtension.mainModule) | ||
| task.mainClass.convention(applicationExtension.mainClass) | ||
| task.jvmArguments.convention(project.provider { applicationExtension.applicationDefaultJvmArgs }) | ||
|
|
||
| private void configureJavaLauncher(JavaJarExec run) { | ||
| def toolchain = project.getExtensions().getByType(JavaPluginExtension.class).toolchain | ||
| JavaToolchainService service = project.getExtensions().getByType(JavaToolchainService.class) | ||
| Provider<JavaLauncher> defaultLauncher = service.launcherFor(toolchain) | ||
| run.getJavaLauncher().set(defaultLauncher) | ||
| task.modularity.inferModulePath.convention(javaPluginExtension.modularity.inferModulePath) | ||
| task.javaLauncher.convention(javaToolchainService.launcherFor(javaPluginExtension.toolchain)) | ||
| } | ||
| } | ||
|
|
||
| protected void addCreateScriptsTask(Project project) { | ||
| project.tasks.register(SHADOW_SCRIPTS_TASK_NAME, CreateStartScripts) { startScripts -> | ||
| startScripts.description = 'Creates OS specific scripts to run the project as a JVM application using the shadow jar' | ||
| startScripts.group = ApplicationPlugin.APPLICATION_GROUP | ||
| startScripts.classpath = project.files(jar) | ||
| startScripts.mainClass.set(javaApplication.mainClass) | ||
| startScripts.conventionMapping.applicationName = { javaApplication.applicationName } | ||
| startScripts.conventionMapping.outputDir = { new File(project.layout.buildDirectory.asFile.get(), 'scriptsShadow') } | ||
| startScripts.conventionMapping.defaultJvmOpts = { javaApplication.applicationDefaultJvmArgs } | ||
| startScripts.inputs.files project.objects.fileCollection().from { -> jar } | ||
| project.tasks.register(SHADOW_SCRIPTS_TASK_NAME, CreateStartScripts) { task -> | ||
| task.description = "Creates OS specific scripts to run the project as a JVM application using the shadow jar" | ||
Goooler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| task.classpath = project.files(project.tasks.named(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME)) | ||
|
|
||
| def applicationExtension = project.extensions.getByType(JavaApplication) | ||
| def javaPluginExtension = project.extensions.getByType(JavaPluginExtension) | ||
|
|
||
| // TODO: replace usages of conventionMapping. | ||
| task.mainModule.convention(applicationExtension.mainModule) | ||
| task.mainClass.convention(applicationExtension.mainClass) | ||
| task.conventionMapping.map("applicationName") { applicationExtension.applicationName } | ||
| task.conventionMapping.map("outputDir") { | ||
| project.layout.buildDirectory.dir("scriptsShadow").get().asFile | ||
| } | ||
| task.conventionMapping.map("executableDir") { applicationExtension.executableDir } | ||
| task.conventionMapping.map("defaultJvmOpts") { applicationExtension.applicationDefaultJvmArgs } | ||
|
|
||
| task.modularity.inferModulePath.convention(javaPluginExtension.modularity.inferModulePath) | ||
| } | ||
| } | ||
|
|
||
| protected void configureInstallTask(Project project) { | ||
| project.tasks.named(SHADOW_INSTALL_TASK_NAME, Sync).configure { task -> | ||
| task.doFirst { | ||
| if (task.destinationDir.directory) { | ||
| if (task.destinationDir.listFiles().size() != 0 && (!new File(task.destinationDir, 'lib').directory || !new File(task.destinationDir, 'bin').directory)) { | ||
| throw new GradleException("The specified installation directory '${task.destinationDir}' is neither empty nor does it contain an installation for '${javaApplication.applicationName}'.\n" + | ||
| def applicationExtension = project.extensions.getByType(JavaApplication) | ||
| def applicationName = project.provider { applicationExtension.applicationName } | ||
| def executableDir = project.provider { applicationExtension.executableDir } | ||
|
|
||
| task.doFirst("Check installation directory") { | ||
| def destinationDir = task.destinationDir | ||
| def children = destinationDir.list() | ||
| if (children == null) { | ||
| throw new IOException("Could not list directory ${destinationDir}") | ||
| } | ||
| if (children.length == 0) return | ||
| if (!new File(destinationDir, "lib").isDirectory() || | ||
| !new File(destinationDir, executableDir.get()).isDirectory()) { | ||
| throw new GradleException( | ||
| "The specified installation directory '${destinationDir}' is neither empty nor does it contain an installation for '${applicationName.get()}'.\n" + | ||
| "If you really want to install to this directory, delete it and run the install task again.\n" + | ||
| "Alternatively, choose a different installation directory." | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| task.doLast { | ||
| task.eachFile { | ||
| if (it.path == "bin/${javaApplication.applicationName}") { | ||
| it.mode = 0x755 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected void configureDistribution(Project project) { | ||
| def distributions = project.extensions.getByType(DistributionContainer) | ||
| distributions.register(DISTRIBUTION_NAME) { dist -> | ||
| def applicationExtension = project.extensions.getByType(JavaApplication) | ||
| dist.distributionBaseName.convention( | ||
| project.provider { | ||
| // distributionBaseName defaults to `$project.name-$distribution.name`, applicationName | ||
| // defaults to project.name | ||
| // so we append the suffix to match the default distributionBaseName. Modified from | ||
| // `ApplicationPlugin.configureDistribution()`. | ||
| "${applicationExtension.applicationName}-${DISTRIBUTION_NAME}" | ||
| } | ||
| ) | ||
| dist.contents { distSpec -> | ||
| distSpec.from(project.file("src/dist")) | ||
| distSpec.into("lib") { lib -> | ||
| lib.from(project.tasks.named(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME)) | ||
| // Reflects the value of the `Class-Path` attribute in the JAR manifest. | ||
| lib.from(project.configurations.named(ShadowBasePlugin.CONFIGURATION_NAME)) | ||
| } | ||
| // Defaults to bin dir. | ||
| distSpec.into(project.provider { applicationExtension.executableDir }) { bin -> | ||
| bin.from(project.tasks.named(SHADOW_SCRIPTS_TASK_NAME)) | ||
| bin.filePermissions { permissions -> permissions.unix('rwxr-xr-x') } | ||
| } | ||
| distSpec.with(applicationExtension.applicationDistribution) | ||
| configureDistSpec(project, distSpec) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected CopySpec configureDistSpec(Project project, CopySpec distSpec) { | ||
| def startScripts = project.tasks.named(SHADOW_SCRIPTS_TASK_NAME) | ||
|
|
||
| distSpec.with { | ||
| from(project.file("src/dist")) | ||
| return distSpec | ||
| } | ||
|
|
||
| into("lib") { | ||
| from(jar) | ||
| from(project.configurations.shadow) | ||
| } | ||
| into("bin") { | ||
| from(startScripts) | ||
| filePermissions { it.unix(493) } | ||
| protected void configureJarMainClass(Project project) { | ||
| def applicationExtension = project.extensions.getByType(JavaApplication) | ||
| project.tasks.named(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME, ShadowJar).configure { task -> | ||
| task.inputs.property('mainClassName', applicationExtension.mainClass) | ||
| task.doFirst { | ||
| task.manifest.attributes 'Main-Class': applicationExtension.mainClass.get() | ||
| } | ||
| } | ||
|
|
||
| distSpec | ||
| } | ||
|
|
||
| private TaskProvider<ShadowJar> getJar() { | ||
| project.tasks.named(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME, ShadowJar) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.