@@ -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' , FindMainClassTask ))
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' , FindMainClassTask ))
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,72 @@ 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
+ }
682
+
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
+ })
673
695
674
- cacheFile?. text
675
- })
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
+ }
705
+ else {
706
+ // we need to set the overridden value on both
707
+ extraProperties. set(' mainClass' , overriddenMainClass)
708
+ springBootExtension. mainClass. set(overriddenMainClass)
709
+ }
676
710
}
677
711
678
712
project. tasks. withType(BootArchive ). configureEach { BootArchive bootTask ->
679
713
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
- })
714
+ bootTask. mainClass. convention(GrailsGradlePlugin . getMainClassProvider(project))
688
715
}
689
716
690
717
project. tasks. withType(BootRun ). configureEach { BootRun it ->
691
718
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
- })
719
+ it. mainClass. convention(GrailsGradlePlugin . getMainClassProvider(project))
700
720
}
701
721
702
722
project. tasks. withType(ResolveMainClassName ). configureEach {
703
723
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
- })
724
+ it. configuredMainClassName. convention(GrailsGradlePlugin . getMainClassProvider(project))
718
725
}
719
726
} else if (! FindMainClassTask . class. isAssignableFrom(existingTask. class)) {
720
727
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 +804,24 @@ class GrailsGradlePlugin extends GroovyPlugin {
797
804
@CompileDynamic
798
805
protected void configureRunScript (Project project ) {
799
806
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))
807
+ def runTask = project. tasks. register(' runScript' , ApplicationContextScriptTask )
808
+ project. afterEvaluate {
809
+ runTask. configure {
810
+ SourceSet mainSourceSet = SourceSets . findMainSourceSet(project)
811
+ it. classpath = mainSourceSet. runtimeClasspath + project. configurations. getByName(' console' )
812
+ it. systemProperty Environment . KEY , System . getProperty(Environment . KEY , Environment . DEVELOPMENT . getName())
813
+ List<Object > args = []
814
+ def otherArgs = project. findProperty(' args' )
815
+ if (otherArgs) {
816
+ args. addAll(CommandLineParser . translateCommandline(otherArgs as String ))
817
+ }
818
+
819
+ def appClassProvider = GrailsGradlePlugin . getMainClassProvider(project)
820
+
821
+ it. doFirst {
822
+ args << appClassProvider. get()
823
+ it. args(args)
824
+ }
807
825
}
808
826
}
809
827
}
@@ -812,13 +830,26 @@ class GrailsGradlePlugin extends GroovyPlugin {
812
830
@CompileDynamic
813
831
protected void configureRunCommand (Project project ) {
814
832
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))
833
+ def runTask = project. tasks. register(' runCommand' , ApplicationContextCommandTask )
834
+ project. afterEvaluate {
835
+ runTask. configure {
836
+ SourceSet mainSourceSet = SourceSets . findMainSourceSet(project)
837
+ it. classpath = mainSourceSet. runtimeClasspath + project. configurations. getByName(' console' )
838
+ it. systemProperty Environment . KEY , System . getProperty(Environment . KEY , Environment . DEVELOPMENT . getName())
839
+
840
+ List<Object > args = []
841
+ def otherArgs = project. findProperty(' args' )
842
+ if (otherArgs) {
843
+ args. addAll(CommandLineParser . translateCommandline(otherArgs as String ))
844
+ }
845
+
846
+ def appClassProvider = GrailsGradlePlugin . getMainClassProvider(project)
847
+
848
+ it. doFirst {
849
+ args << appClassProvider. get()
850
+ it. args(args)
851
+ }
852
+
822
853
}
823
854
}
824
855
}
0 commit comments