@@ -29,6 +29,7 @@ import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension
29
29
import org.apache.tools.ant.filters.EscapeUnicode
30
30
import org.apache.tools.ant.filters.ReplaceTokens
31
31
import org.gradle.api.Action
32
+ import org.gradle.api.GradleException
32
33
import org.gradle.api.NamedDomainObjectProvider
33
34
import org.gradle.api.Plugin
34
35
import org.gradle.api.Project
@@ -164,6 +165,18 @@ class GrailsGradlePlugin extends GroovyPlugin {
164
165
}
165
166
}
166
167
168
+ private static Provider<String > getMainClassProvider (Project project ) {
169
+ Provider<FindMainClassTask > findMainClassTask = project. tasks. named(' findMainClass' , FindMainClassTask )
170
+ project. provider {
171
+ File cacheFile = findMainClassTask. get(). mainClassCacheFile. orNull?. asFile
172
+ if (! cacheFile?. exists()) {
173
+ return null
174
+ }
175
+
176
+ cacheFile?. text
177
+ }
178
+ }
179
+
167
180
private void configureGroovyCompiler (Project project ) {
168
181
Provider<Directory > sourceConfigFiles = project. layout. buildDirectory. dir(' groovyCompilerConfiguration' )
169
182
Provider<RegularFile > groovyCompilerConfigFile = project. layout. buildDirectory. file(' grailsGroovyCompilerConfig.groovy' )
@@ -433,13 +446,22 @@ class GrailsGradlePlugin extends GroovyPlugin {
433
446
for (ctxCommand in applicationContextCommands) {
434
447
String taskName = GrailsNameUtils . getLogicalPropertyName(ctxCommand, ' Command' )
435
448
String commandName = GrailsNameUtils . getScriptName(GrailsNameUtils . getLogicalName(ctxCommand, ' Command' ))
436
- if (project. tasks. findByName(taskName) == null ) {
437
- project. tasks. create(taskName, ApplicationContextCommandTask ) {
438
- classpath = fileCollection
439
- command = commandName
440
- systemProperty Environment . KEY , System . getProperty(Environment . KEY , Environment . DEVELOPMENT . getName())
441
- if (project. hasProperty(' args' )) {
442
- args(CommandLineParser . translateCommandline(project. args))
449
+ if (! project. tasks. names. contains(taskName)) {
450
+ project. tasks. register(taskName, ApplicationContextCommandTask ). configure {
451
+ it. classpath = fileCollection
452
+ it. command = commandName
453
+ it. systemProperty(Environment . KEY , System . getProperty(Environment . KEY , Environment . DEVELOPMENT . getName()))
454
+ List<Object > args = []
455
+ def otherArgs = project. findProperty(' args' )
456
+ if (otherArgs) {
457
+ args. addAll(CommandLineParser . translateCommandline(otherArgs as String ))
458
+ }
459
+
460
+ def appClassProvider = GrailsGradlePlugin . getMainClassProvider(project)
461
+
462
+ it. doFirst {
463
+ args << appClassProvider. get()
464
+ it. args(args)
443
465
}
444
466
}
445
467
}
@@ -564,57 +586,46 @@ class GrailsGradlePlugin extends GroovyPlugin {
564
586
}
565
587
566
588
NamedDomainObjectProvider<Configuration > consoleConfiguration = project. configurations. register(' console' )
567
- def consoleTask = createConsoleTask(project, tasks, consoleConfiguration)
568
- def shellTask = createShellTask(project, tasks, consoleConfiguration)
569
-
570
- tasks. named(' findMainClass' ). configure {
571
- it. doLast {
572
- def extraProperties = project. getExtensions(). getByType(ExtraPropertiesExtension )
573
- if (! extraProperties. has(' mainClassName' )) {
574
- return // disabled because we don't expect to run a grails app (likely a plugin)
575
- }
576
-
577
- def mainClassName = extraProperties. get(' mainClassName' )
578
- if (mainClassName) {
579
- consoleTask. get(). args mainClassName
580
- shellTask. get(). args mainClassName
581
- project. tasks. withType(ApplicationContextCommandTask ) { ApplicationContextCommandTask task ->
582
- task. args mainClassName
583
- }
584
- }
585
- project. tasks. withType(ApplicationContextScriptTask ) { ApplicationContextScriptTask task ->
586
- task. args mainClassName
587
- }
588
- }
589
- }
590
-
591
- consoleTask. configure {
592
- it. dependsOn(tasks. named(' classes' ), tasks. named(' findMainClass' ))
593
- }
594
-
595
- shellTask. configure {
596
- it. dependsOn(tasks. named(' classes' ), tasks. named(' findMainClass' ))
597
- }
589
+ createConsoleTask(project, tasks, consoleConfiguration)
590
+ createShellTask(project, tasks, consoleConfiguration)
598
591
}
599
592
}
600
593
601
594
@CompileDynamic
602
595
protected TaskProvider<JavaExec > createConsoleTask (Project project , TaskContainer tasks , NamedDomainObjectProvider<Configuration > configuration ) {
603
596
def consoleTask = tasks. register(' console' , JavaExec )
604
- consoleTask. configure {
605
- it. classpath = project. sourceSets. main. runtimeClasspath + configuration. get()
606
- it. mainClass. set(' grails.ui.console.GrailsSwingConsole' )
597
+ project. afterEvaluate {
598
+ consoleTask. configure {
599
+ it. dependsOn(tasks. named(' classes' ), tasks. named(' findMainClass' ))
600
+ it. classpath = project. sourceSets. main. runtimeClasspath + configuration. get()
601
+ it. mainClass. set(' grails.ui.console.GrailsSwingConsole' )
602
+
603
+ def appClass = GrailsGradlePlugin . getMainClassProvider(project)
604
+
605
+ it. doFirst {
606
+ it. args(appClass. get())
607
+ }
608
+ }
607
609
}
608
610
consoleTask
609
611
}
610
612
611
613
@CompileDynamic
612
614
protected TaskProvider<JavaExec > createShellTask (Project project , TaskContainer tasks , NamedDomainObjectProvider<Configuration > configuration ) {
613
615
def shellTask = tasks. register(' shell' , JavaExec )
614
- shellTask. configure {
615
- it. classpath = project. sourceSets. main. runtimeClasspath + configuration. get()
616
- it. mainClass. set(' grails.ui.shell.GrailsShell' )
617
- it. standardInput = System . in
616
+ project. afterEvaluate {
617
+ shellTask. configure {
618
+ it. dependsOn(tasks. named(' classes' ), tasks. named(' findMainClass' ))
619
+ it. classpath = project. sourceSets. main. runtimeClasspath + configuration. get()
620
+ it. mainClass. set(' grails.ui.shell.GrailsShell' )
621
+ it. standardInput = System . in
622
+
623
+ def appClass = GrailsGradlePlugin . getMainClassProvider(project)
624
+
625
+ it. doFirst {
626
+ it. args(appClass. get())
627
+ }
628
+ }
618
629
}
619
630
shellTask
620
631
}
@@ -629,6 +640,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
629
640
}
630
641
}
631
642
643
+ @CompileDynamic
632
644
protected void registerFindMainClassTask (Project project ) {
633
645
TaskContainer taskContainer = project. tasks
634
646
@@ -644,77 +656,71 @@ class GrailsGradlePlugin extends GroovyPlugin {
644
656
}
645
657
}
646
658
647
- // Set it on the extensions & spring boot extension "just" to be complete in case any other tasks use these values
648
659
project. afterEvaluate {
649
- def extraProperties = project. extensions. getByType(ExtraPropertiesExtension )
650
- extraProperties. set(' mainClassName' , project. provider {
651
- if (extraProperties. has(' mainClassName' )) {
652
- return extraProperties. get(' mainClassName' )
653
- }
654
-
655
- File cacheFile = findMainClassTask. get(). mainClassCacheFile. orNull?. asFile
656
- if (! cacheFile. exists()) {
657
- return null
660
+ // Support overrides - via mainClass property
661
+ def propertyMainClassName = project. findProperty(' mainClass' )
662
+ if (propertyMainClassName) {
663
+ findMainClassTask. configure {
664
+ it. mainClassName. set(propertyMainClassName)
658
665
}
666
+ }
659
667
660
- cacheFile?. text
661
- })
662
-
668
+ // Support overrides - via mainClass springboot extension
663
669
def springBootExtension = project. extensions. getByType(SpringBootExtension )
664
- springBootExtension. mainClass. set(project. provider {
665
- if (springBootExtension. mainClass. isPresent()) {
666
- return springBootExtension. mainClass. get() as String
670
+ String springBootMainClassName = springBootExtension. mainClass. getOrNull()
671
+ if (springBootMainClassName) {
672
+ findMainClassTask. configure {
673
+ it. mainClassName. set(springBootMainClassName)
667
674
}
675
+ }
668
676
669
- File cacheFile = findMainClassTask . get() . mainClassCacheFile . orNull ?. asFile
670
- if (! cacheFile . exists() ) {
671
- return null
677
+ if (springBootMainClassName && propertyMainClassName) {
678
+ if (springBootMainClassName != propertyMainClassName ) {
679
+ throw new GradleException ( " If overriding the mainClass, the property 'mainClass' and the springboot.mainClass must be set to the same value " )
672
680
}
681
+ }
673
682
674
- cacheFile?. text
675
- })
683
+ def extraProperties = project. extensions. getByType(ExtraPropertiesExtension )
684
+ def overriddenMainClass = propertyMainClassName ?: springBootMainClassName
685
+ if (! overriddenMainClass) {
686
+ // the findMainClass task needs to set these values
687
+ extraProperties. set(' mainClassName' , project. provider {
688
+ File cacheFile = findMainClassTask. get(). mainClassCacheFile. orNull?. asFile
689
+ if (! cacheFile?. exists()) {
690
+ return null
691
+ }
692
+
693
+ cacheFile?. text
694
+ })
695
+
696
+ springBootExtension. mainClass. set(project. provider {
697
+ File cacheFile = findMainClassTask. get(). mainClassCacheFile. orNull?. asFile
698
+ if (! cacheFile?. exists()) {
699
+ return null
700
+ }
701
+
702
+ cacheFile?. text
703
+ })
704
+ } else {
705
+ // we need to set the overridden value on both
706
+ extraProperties. set(' mainClass' , overriddenMainClass)
707
+ springBootExtension. mainClass. set(overriddenMainClass)
708
+ }
676
709
}
677
710
678
711
project. tasks. withType(BootArchive ). configureEach { BootArchive bootTask ->
679
712
bootTask. dependsOn(findMainClassTask)
680
- bootTask. mainClass. convention(project. provider {
681
- File cacheFile = findMainClassTask. get(). mainClassCacheFile. orNull?. asFile
682
- if (! cacheFile. exists()) {
683
- return null
684
- }
685
-
686
- cacheFile?. text
687
- })
713
+ bootTask. mainClass. convention(GrailsGradlePlugin . getMainClassProvider(project))
688
714
}
689
715
690
716
project. tasks. withType(BootRun ). configureEach { BootRun it ->
691
717
it. dependsOn(findMainClassTask)
692
- it. mainClass. convention(project. provider {
693
- File cacheFile = findMainClassTask. get(). mainClassCacheFile. orNull?. asFile
694
- if (! cacheFile. exists()) {
695
- return null
696
- }
697
-
698
- cacheFile?. text
699
- })
718
+ it. mainClass. convention(GrailsGradlePlugin . getMainClassProvider(project))
700
719
}
701
720
702
721
project. tasks. withType(ResolveMainClassName ). configureEach {
703
722
it. dependsOn(findMainClassTask)
704
-
705
- it. configuredMainClassName. set(project. provider {
706
- def springBootExtension = project. extensions. getByType(SpringBootExtension )
707
- if (springBootExtension. mainClass. isPresent()) {
708
- return springBootExtension. mainClass. get() as String
709
- }
710
-
711
- File cacheFile = findMainClassTask. get(). mainClassCacheFile. orNull?. asFile
712
- if (! cacheFile. exists()) {
713
- return null
714
- }
715
-
716
- cacheFile?. text
717
- })
723
+ it. configuredMainClassName. convention(GrailsGradlePlugin . getMainClassProvider(project))
718
724
}
719
725
} else if (! FindMainClassTask . class. isAssignableFrom(existingTask. class)) {
720
726
project. logger. warn(' Grails Projects typically register a findMainClass task to force the MainClass resolution for Spring Boot. This task already exists so this will not occur.' )
@@ -797,13 +803,24 @@ class GrailsGradlePlugin extends GroovyPlugin {
797
803
@CompileDynamic
798
804
protected void configureRunScript (Project project ) {
799
805
if (! project. tasks. names. contains(' runScript' )) {
800
- project. tasks. register(' runScript' , ApplicationContextScriptTask ). configure {
801
- SourceSet mainSourceSet = SourceSets . findMainSourceSet(project)
802
- it. classpath = mainSourceSet. runtimeClasspath + project. configurations. getByName(' console' )
803
- it. systemProperty Environment . KEY , System . getProperty(Environment . KEY , Environment . DEVELOPMENT . getName())
804
- def argsProperty = project. findProperty(' args' )
805
- if (argsProperty) {
806
- it. args(CommandLineParser . translateCommandline(argsProperty))
806
+ def runTask = project. tasks. register(' runScript' , ApplicationContextScriptTask )
807
+ project. afterEvaluate {
808
+ runTask. configure {
809
+ SourceSet mainSourceSet = SourceSets . findMainSourceSet(project)
810
+ it. classpath = mainSourceSet. runtimeClasspath + project. configurations. getByName(' console' )
811
+ it. systemProperty(Environment . KEY , System . getProperty(Environment . KEY , Environment . DEVELOPMENT . getName()))
812
+ List<Object > args = []
813
+ def otherArgs = project. findProperty(' args' )
814
+ if (otherArgs) {
815
+ args. addAll(CommandLineParser . translateCommandline(otherArgs as String ))
816
+ }
817
+
818
+ def appClassProvider = GrailsGradlePlugin . getMainClassProvider(project)
819
+
820
+ it. doFirst {
821
+ args << appClassProvider. get()
822
+ it. args(args)
823
+ }
807
824
}
808
825
}
809
826
}
@@ -812,13 +829,26 @@ class GrailsGradlePlugin extends GroovyPlugin {
812
829
@CompileDynamic
813
830
protected void configureRunCommand (Project project ) {
814
831
if (! project. tasks. names. contains(' runCommand' )) {
815
- project. tasks. register(' runCommand' , ApplicationContextCommandTask ). configure {
816
- SourceSet mainSourceSet = SourceSets . findMainSourceSet(project)
817
- it. classpath = mainSourceSet. runtimeClasspath + project. configurations. getByName(' console' )
818
- it. systemProperty Environment . KEY , System . getProperty(Environment . KEY , Environment . DEVELOPMENT . getName())
819
- def argsProperty = project. findProperty(' args' )
820
- if (argsProperty) {
821
- it. args(CommandLineParser . translateCommandline(argsProperty))
832
+ def runTask = project. tasks. register(' runCommand' , ApplicationContextCommandTask )
833
+ project. afterEvaluate {
834
+ runTask. configure {
835
+ SourceSet mainSourceSet = SourceSets . findMainSourceSet(project)
836
+ it. classpath = mainSourceSet. runtimeClasspath + project. configurations. getByName(' console' )
837
+ it. systemProperty(Environment . KEY , System . getProperty(Environment . KEY , Environment . DEVELOPMENT . getName()))
838
+
839
+ List<Object > args = []
840
+ def otherArgs = project. findProperty(' args' )
841
+ if (otherArgs) {
842
+ args. addAll(CommandLineParser . translateCommandline(otherArgs as String ))
843
+ }
844
+
845
+ def appClassProvider = GrailsGradlePlugin . getMainClassProvider(project)
846
+
847
+ it. doFirst {
848
+ args << appClassProvider. get()
849
+ it. args(args)
850
+ }
851
+
822
852
}
823
853
}
824
854
}
0 commit comments