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

#609 Support application mainClass property as fallback of mainClassName #612

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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.tasks.Sync
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.application.CreateStartScripts
Expand Down Expand Up @@ -54,7 +55,7 @@ class ShadowApplicationPlugin implements Plugin<Project> {
}

protected void configureJarMainClass(Project project) {
def classNameProvider = project.provider { project.convention.plugins.application.mainClassName }
def classNameProvider = project.provider { getMainClassName() }
jar.configure { jar ->
jar.inputs.property('mainClassName', classNameProvider)
jar.doFirst {
Expand All @@ -63,6 +64,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,48 @@ class ShadowPluginSpec extends PluginSpecification {
assert result.output.contains('TestApp: Hello World! (foo)')
}

@Issue("https://github.com/johnrengelman/shadow/issues/609")
def "doesn't error when using application mainClass property"() {
given:
buildFile.text = defaultBuildScript

buildFile << """
project.ext {
aspectjVersion = '1.8.12'
}

apply plugin: 'application'

application {
mainClass.set('myapp.Main')
}

repositories {
jcenter()
}

runShadow {
args 'foo'
}

"""

file('src/main/java/myapp/Main.java') << """
package myapp;
public class Main {
public static void main(String[] args) {
System.out.println("TestApp: Hello World! (" + args[0] + ")");
}
}
""".stripIndent()

when:
BuildResult result = runner.withArguments('runShadow', '--stacktrace').build()

then: 'tests that runShadow executed and exited'
assert result.output.contains('TestApp: Hello World! (foo)')
}

private String escapedPath(File file) {
file.path.replaceAll('\\\\', '\\\\\\\\')
}
Expand Down