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

Support the mainClass property in the Application Plugin Extension #631

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ 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.MavenPlugin
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Sync
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.application.CreateStartScripts
import org.gradle.util.GradleVersion

class ShadowApplicationPlugin implements Plugin<Project> {

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

protected void configureJarMainClass(Project project) {
def classNameProvider = project.provider { project.convention.plugins.application.mainClassName }
def classNameProvider = getMainClass(project)
jar.configure { jar ->
jar.inputs.property('mainClassName', classNameProvider)
jar.doFirst {
Expand All @@ -64,6 +67,15 @@ class ShadowApplicationPlugin implements Plugin<Project> {
}
}

private static Provider<String> getMainClass(Project project) {
def application = (JavaApplication) project.extensions.application
if (GradleVersion.current() >= GradleVersion.version("6.4")) {
return application.mainClass.orElse(project.provider { application.mainClassName })
} else {
return project.provider { application.mainClassName }
}
}

protected void addRunTask(Project project) {
ApplicationPluginConvention pluginConvention = (
ApplicationPluginConvention) project.convention.plugins.application
Expand Down Expand Up @@ -91,7 +103,7 @@ class ShadowApplicationPlugin implements Plugin<Project> {
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.mainClassName = { getMainClass(project).get() }
startScripts.conventionMapping.applicationName = { pluginConvention.applicationName }
startScripts.conventionMapping.outputDir = { new File(project.buildDir, 'scriptsShadow') }
startScripts.conventionMapping.defaultJvmOpts = { pluginConvention.applicationDefaultJvmArgs }
Expand Down