Skip to content

Commit

Permalink
Merge pull request #821 from eskatos/eskatos/remove-convention-usages
Browse files Browse the repository at this point in the history
Prefer using plugin extensions over deprecated conventions
  • Loading branch information
johnrengelman authored Feb 24, 2023
2 parents e522620 + 1d186f8 commit 8b26576
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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.ApplicationPluginConvention
import org.gradle.api.plugins.JavaApplication
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.provider.Provider
Expand All @@ -26,12 +25,12 @@ class ShadowApplicationPlugin implements Plugin<Project> {
public static final String SHADOW_INSTALL_TASK_NAME = 'installShadowDist'

private Project project
private ApplicationPluginConvention pluginConvention
private JavaApplication javaApplication

@Override
void apply(Project project) {
this.project = project
this.pluginConvention = (ApplicationPluginConvention) project.convention.plugins.application
this.javaApplication = project.extensions.getByType(JavaApplication)

DistributionContainer distributions = project.extensions.getByName("distributions")
Distribution distribution = distributions.create("shadow")
Expand All @@ -46,7 +45,7 @@ class ShadowApplicationPlugin implements Plugin<Project> {
}

protected void configureJarMainClass(Project project) {
def classNameProvider = project.provider { getMainClassName() }
def classNameProvider = javaApplication.mainClass
jar.configure { jar ->
jar.inputs.property('mainClassName', classNameProvider)
jar.doFirst {
Expand All @@ -55,26 +54,15 @@ class ShadowApplicationPlugin implements Plugin<Project> {
}
}

private Object getMainClassName() {
def mainClassName = project.convention.plugins.application.mainClassName
if (Objects.nonNull(mainClassName)) {
return mainClassName
}

return project.extensions.getByType(JavaApplication.class).mainClass.get()
}

protected void addRunTask(Project project) {
ApplicationPluginConvention pluginConvention = (
ApplicationPluginConvention) project.convention.plugins.application

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 = { pluginConvention.applicationDefaultJvmArgs }
run.conventionMapping.jvmArgs = { javaApplication.applicationDefaultJvmArgs }
run.conventionMapping.jarFile = {
project.file("${install.get().destinationDir.path}/lib/${jar.get().archivePath.name}")
}
Expand All @@ -90,40 +78,34 @@ class ShadowApplicationPlugin implements Plugin<Project> {
}

protected void addCreateScriptsTask(Project project) {
ApplicationPluginConvention pluginConvention =
(ApplicationPluginConvention) project.convention.plugins.application

project.tasks.register(SHADOW_SCRIPTS_TASK_NAME, CreateStartScripts) { startScripts ->
startScripts.unixStartScriptGenerator.template = project.resources.text.fromString(this.class.getResource("internal/unixStartScript.txt").text)
startScripts.windowsStartScriptGenerator.template = project.resources.text.fromString(this.class.getResource("internal/windowsStartScript.txt").text)
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.conventionMapping.mainClassName = { pluginConvention.mainClassName }
startScripts.conventionMapping.applicationName = { pluginConvention.applicationName }
startScripts.conventionMapping.mainClassName = { javaApplication.mainClass.get() }
startScripts.conventionMapping.applicationName = { javaApplication.applicationName }
startScripts.conventionMapping.outputDir = { new File(project.buildDir, 'scriptsShadow') }
startScripts.conventionMapping.defaultJvmOpts = { pluginConvention.applicationDefaultJvmArgs }
startScripts.conventionMapping.defaultJvmOpts = { javaApplication.applicationDefaultJvmArgs }
startScripts.inputs.files project.objects.fileCollection().from { -> jar }
}
}

protected void configureInstallTask(Project project) {
ApplicationPluginConvention pluginConvention =
(ApplicationPluginConvention) project.convention.plugins.application

project.tasks.named(SHADOW_INSTALL_TASK_NAME).configure { installTask ->
installTask.doFirst { Sync task ->
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 '${pluginConvention.applicationName}'.\n" +
throw new GradleException("The specified installation directory '${task.destinationDir}' is neither empty nor does it contain an installation for '${javaApplication.applicationName}'.\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."
)
}
}
}
installTask.doLast { Sync task ->
project.ant.chmod(file: "${task.destinationDir.absolutePath}/bin/${pluginConvention.applicationName}", perm: 'ugo+x')
project.ant.chmod(file: "${task.destinationDir.absolutePath}/bin/${javaApplication.applicationName}", perm: 'ugo+x')
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.gradle.api.attributes.Bundling
import org.gradle.api.attributes.Category
import org.gradle.api.attributes.LibraryElements
import org.gradle.api.attributes.Usage
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.configuration.project.ProjectConfigurationActionContainer
import org.gradle.util.GradleVersion

Expand Down Expand Up @@ -55,7 +55,7 @@ class ShadowJavaPlugin implements Plugin<Project> {
}

protected void configureShadowTask(Project project) {
JavaPluginConvention convention = project.convention.getPlugin(JavaPluginConvention)
SourceSetContainer sourceSets = project.extensions.getByType(SourceSetContainer)
project.tasks.register(SHADOW_JAR_TASK_NAME, ShadowJar) { shadow ->
shadow.group = SHADOW_GROUP
shadow.description = 'Create a combined JAR of project and runtime dependencies'
Expand All @@ -80,7 +80,7 @@ class ShadowJavaPlugin implements Plugin<Project> {
manifest.attributes 'Class-Path': libs.findAll { it }.join(' ')
}
}
shadow.from(convention.sourceSets.main.output)
shadow.from(sourceSets.main.output)
shadow.configurations = [project.configurations.findByName('runtimeClasspath') ?
project.configurations.runtimeClasspath : project.configurations.runtime]
shadow.exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'module-info.class')
Expand Down

0 comments on commit 8b26576

Please sign in to comment.