@@ -17,119 +17,119 @@ import org.gradle.jvm.application.scripts.TemplateBasedScriptGenerator
17
17
import org.gradle.jvm.toolchain.JavaToolchainService
18
18
19
19
abstract class ShadowApplicationPlugin : Plugin <Project > {
20
- private lateinit var project: Project
21
- private lateinit var javaApplication: JavaApplication
20
+ private lateinit var project: Project
21
+ private lateinit var javaApplication: JavaApplication
22
22
23
- override fun apply (project : Project ) {
24
- this .project = project
25
- this .javaApplication = project.extensions.getByType(JavaApplication ::class .java)
23
+ override fun apply (project : Project ) {
24
+ this .project = project
25
+ this .javaApplication = project.extensions.getByType(JavaApplication ::class .java)
26
26
27
- addRunTask()
28
- addCreateScriptsTask()
29
- configureDistSpec()
30
- configureJarMainClass()
31
- configureInstallTask()
32
- }
27
+ addRunTask()
28
+ addCreateScriptsTask()
29
+ configureDistSpec()
30
+ configureJarMainClass()
31
+ configureInstallTask()
32
+ }
33
33
34
- protected open fun configureJarMainClass () {
35
- val classNameProvider = javaApplication.mainClass
36
- shadowJar.configure { jar ->
37
- jar.inputs.property(" mainClassName" , classNameProvider)
38
- jar.doFirst {
39
- jar.manifest.attributes[" Main-Class" ] = classNameProvider.get()
40
- }
41
- }
34
+ protected open fun configureJarMainClass () {
35
+ val classNameProvider = javaApplication.mainClass
36
+ shadowJar.configure { jar ->
37
+ jar.inputs.property(" mainClassName" , classNameProvider)
38
+ jar.doFirst {
39
+ jar.manifest.attributes[" Main-Class" ] = classNameProvider.get()
40
+ }
42
41
}
42
+ }
43
43
44
- protected open fun addRunTask () {
45
- project.tasks.register(SHADOW_RUN_TASK_NAME , JavaJarExec ::class .java) {
46
- val install = project.tasks.named(SHADOW_INSTALL_TASK_NAME , Sync ::class .java)
47
- it.dependsOn(install)
48
- it.mainClass.set(" -jar" )
49
- it.description = " Runs this project as a JVM application using the shadow jar"
50
- it.group = ApplicationPlugin .APPLICATION_GROUP
51
- it.conventionMapping.map(" jvmArgs" ) { javaApplication.applicationDefaultJvmArgs }
52
- it.jarFile.fileProvider(
53
- project.providers.provider {
54
- project.file(" ${install.get().destinationDir.path} /lib/${shadowJar.get().archiveFile.get().asFile.name} " )
55
- },
56
- )
57
- val toolchain = project.extensions.getByType(JavaPluginExtension ::class .java).toolchain
58
- val defaultLauncher = project.extensions.getByType(JavaToolchainService ::class .java)
59
- .launcherFor(toolchain)
60
- it.javaLauncher.set(defaultLauncher)
61
- }
44
+ protected open fun addRunTask () {
45
+ project.tasks.register(SHADOW_RUN_TASK_NAME , JavaJarExec ::class .java) {
46
+ val install = project.tasks.named(SHADOW_INSTALL_TASK_NAME , Sync ::class .java)
47
+ it.dependsOn(install)
48
+ it.mainClass.set(" -jar" )
49
+ it.description = " Runs this project as a JVM application using the shadow jar"
50
+ it.group = ApplicationPlugin .APPLICATION_GROUP
51
+ it.conventionMapping.map(" jvmArgs" ) { javaApplication.applicationDefaultJvmArgs }
52
+ it.jarFile.fileProvider(
53
+ project.providers.provider {
54
+ project.file(" ${install.get().destinationDir.path} /lib/${shadowJar.get().archiveFile.get().asFile.name} " )
55
+ },
56
+ )
57
+ val toolchain = project.extensions.getByType(JavaPluginExtension ::class .java).toolchain
58
+ val defaultLauncher = project.extensions.getByType(JavaToolchainService ::class .java)
59
+ .launcherFor(toolchain)
60
+ it.javaLauncher.set(defaultLauncher)
62
61
}
62
+ }
63
63
64
- protected open fun addCreateScriptsTask () {
65
- project.tasks.register(SHADOW_SCRIPTS_TASK_NAME , CreateStartScripts ::class .java) {
66
- (it.unixStartScriptGenerator as TemplateBasedScriptGenerator ).template =
67
- project.resources.text.fromString(this ::class .java.requireResourceAsText(" internal/unixStartScript.txt" ))
68
- (it.windowsStartScriptGenerator as TemplateBasedScriptGenerator ).template =
69
- project.resources.text.fromString(this ::class .java.requireResourceAsText(" internal/windowsStartScript.txt" ))
70
- it.description = " Creates OS specific scripts to run the project as a JVM application using the shadow jar"
71
- it.group = ApplicationPlugin .APPLICATION_GROUP
72
- it.classpath = project.files(shadowJar)
73
- it.conventionMapping.map(" mainClassName" ) { javaApplication.mainClass.get() }
74
- it.conventionMapping.map(" applicationName" ) { javaApplication.applicationName }
75
- it.conventionMapping.map(" outputDir" ) { project.layout.buildDirectory.dir(" scriptsShadow" ).get().asFile }
76
- it.conventionMapping.map(" defaultJvmOpts" ) { javaApplication.applicationDefaultJvmArgs }
77
- it.inputs.files(project.files(shadowJar))
78
- }
64
+ protected open fun addCreateScriptsTask () {
65
+ project.tasks.register(SHADOW_SCRIPTS_TASK_NAME , CreateStartScripts ::class .java) {
66
+ (it.unixStartScriptGenerator as TemplateBasedScriptGenerator ).template =
67
+ project.resources.text.fromString(this ::class .java.requireResourceAsText(" internal/unixStartScript.txt" ))
68
+ (it.windowsStartScriptGenerator as TemplateBasedScriptGenerator ).template =
69
+ project.resources.text.fromString(this ::class .java.requireResourceAsText(" internal/windowsStartScript.txt" ))
70
+ it.description = " Creates OS specific scripts to run the project as a JVM application using the shadow jar"
71
+ it.group = ApplicationPlugin .APPLICATION_GROUP
72
+ it.classpath = project.files(shadowJar)
73
+ it.conventionMapping.map(" mainClassName" ) { javaApplication.mainClass.get() }
74
+ it.conventionMapping.map(" applicationName" ) { javaApplication.applicationName }
75
+ it.conventionMapping.map(" outputDir" ) { project.layout.buildDirectory.dir(" scriptsShadow" ).get().asFile }
76
+ it.conventionMapping.map(" defaultJvmOpts" ) { javaApplication.applicationDefaultJvmArgs }
77
+ it.inputs.files(project.files(shadowJar))
79
78
}
79
+ }
80
80
81
- protected open fun configureInstallTask () {
82
- project.tasks.named(SHADOW_INSTALL_TASK_NAME , Sync ::class .java).configure { task ->
83
- val applicationName = project.providers.provider { javaApplication.applicationName }
81
+ protected open fun configureInstallTask () {
82
+ project.tasks.named(SHADOW_INSTALL_TASK_NAME , Sync ::class .java).configure { task ->
83
+ val applicationName = project.providers.provider { javaApplication.applicationName }
84
84
85
- task.doFirst {
86
- if (
87
- ! task.destinationDir.listFiles().isNullOrEmpty() &&
88
- (
89
- ! task.destinationDir.resolve(" lib" ).isDirectory ||
90
- ! task.destinationDir.resolve(" bin" ).isDirectory
91
- )
92
- ) {
93
- throw GradleException (
94
- " The specified installation directory '${task.destinationDir} ' is neither empty nor does it contain an installation for '${applicationName.get()} '.\n " +
95
- " If you really want to install to this directory, delete it and run the install task again.\n " +
96
- " Alternatively, choose a different installation directory." ,
97
- )
98
- }
99
- }
100
- task.doLast {
101
- task.eachFile {
102
- if (it.path == " bin/${applicationName.get()} " ) {
103
- it.mode = 0x755
104
- }
105
- }
106
- }
85
+ task.doFirst {
86
+ if (
87
+ ! task.destinationDir.listFiles().isNullOrEmpty() &&
88
+ (
89
+ ! task.destinationDir.resolve(" lib" ).isDirectory ||
90
+ ! task.destinationDir.resolve(" bin" ).isDirectory
91
+ )
92
+ ) {
93
+ throw GradleException (
94
+ " The specified installation directory '${task.destinationDir} ' is neither empty nor does it contain an installation for '${applicationName.get()} '.\n " +
95
+ " If you really want to install to this directory, delete it and run the install task again.\n " +
96
+ " Alternatively, choose a different installation directory." ,
97
+ )
98
+ }
99
+ }
100
+ task.doLast {
101
+ task.eachFile {
102
+ if (it.path == " bin/${applicationName.get()} " ) {
103
+ it.mode = 0x755
104
+ }
107
105
}
106
+ }
108
107
}
108
+ }
109
109
110
- protected open fun configureDistSpec () {
111
- project.extensions.getByType(DistributionContainer ::class .java)
112
- .register(ShadowBasePlugin .DISTRIBUTION_NAME ) { distributions ->
113
- distributions.contents { contents ->
114
- contents.from(project.file(" src/dist" ))
115
- contents.into(" lib" ) { lib ->
116
- lib.from(shadowJar)
117
- lib.from(project.configurations.named(ShadowBasePlugin .CONFIGURATION_NAME ))
118
- }
119
- contents.into(" bin" ) { bin ->
120
- bin.from(project.tasks.named(SHADOW_SCRIPTS_TASK_NAME ))
121
- bin.filePermissions { it.unix(493 ) }
122
- }
123
- }
124
- }
125
- }
110
+ protected open fun configureDistSpec () {
111
+ project.extensions.getByType(DistributionContainer ::class .java)
112
+ .register(ShadowBasePlugin .DISTRIBUTION_NAME ) { distributions ->
113
+ distributions.contents { contents ->
114
+ contents.from(project.file(" src/dist" ))
115
+ contents.into(" lib" ) { lib ->
116
+ lib.from(shadowJar)
117
+ lib.from(project.configurations.named(ShadowBasePlugin .CONFIGURATION_NAME ))
118
+ }
119
+ contents.into(" bin" ) { bin ->
120
+ bin.from(project.tasks.named(SHADOW_SCRIPTS_TASK_NAME ))
121
+ bin.filePermissions { it.unix(493 ) }
122
+ }
123
+ }
124
+ }
125
+ }
126
126
127
- protected val shadowJar: TaskProvider <ShadowJar >
128
- get() = project.tasks.named(ShadowJavaPlugin .SHADOW_JAR_TASK_NAME , ShadowJar ::class .java)
127
+ protected val shadowJar: TaskProvider <ShadowJar >
128
+ get() = project.tasks.named(ShadowJavaPlugin .SHADOW_JAR_TASK_NAME , ShadowJar ::class .java)
129
129
130
- companion object {
131
- const val SHADOW_RUN_TASK_NAME : String = " runShadow"
132
- const val SHADOW_SCRIPTS_TASK_NAME : String = " startShadowScripts"
133
- const val SHADOW_INSTALL_TASK_NAME : String = " installShadowDist"
134
- }
130
+ companion object {
131
+ const val SHADOW_RUN_TASK_NAME : String = " runShadow"
132
+ const val SHADOW_SCRIPTS_TASK_NAME : String = " startShadowScripts"
133
+ const val SHADOW_INSTALL_TASK_NAME : String = " installShadowDist"
134
+ }
135
135
}
0 commit comments