Skip to content

Commit a95f303

Browse files
committed
Merge branch '8.0.x' into feature/taglib-method-actions
2 parents 9910256 + fc747cc commit a95f303

23 files changed

Lines changed: 180 additions & 2 deletions

File tree

grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsExtension.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class GrailsExtension {
4343
this.project = project
4444
this.pluginDefiner = new PluginDefiner(project)
4545
this.indy = project.objects.property(Boolean).convention(false)
46+
this.preserveParameterNames = project.objects.property(Boolean).convention(true)
4647
}
4748

4849
/**
@@ -114,6 +115,12 @@ class GrailsExtension {
114115
this.indy.set(enabled)
115116
}
116117

118+
/**
119+
* Keep method and constructor parameter names in class files, allowing frameworks such as Spring to use parameter
120+
* names for dependency resolution, including autowiring by name without requiring annotations such as @Qualifier.
121+
*/
122+
final Property<Boolean> preserveParameterNames
123+
117124
DependencyHandler getPlugins() {
118125
if (pluginDefiner == null) {
119126
pluginDefiner = new PluginDefiner(project)

grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,17 @@ class GrailsGradlePlugin implements Plugin<Project> {
242242
// Configure indy and log status after evaluation so user's grails { } block has been applied
243243
GrailsExtension grailsExtension = project.extensions.findByType(GrailsExtension)
244244
project.afterEvaluate {
245-
boolean indyEnabled = grailsExtension?.indy?.getOrElse(false) ?: false
245+
boolean indyEnabled = grailsExtension.indy.getOrElse(false)
246+
Boolean preserveParameterNames = grailsExtension.preserveParameterNames.getOrNull()
247+
246248
project.tasks.withType(GroovyCompile).configureEach { GroovyCompile c ->
247249
c.groovyOptions.optimizationOptions.indy = indyEnabled
250+
251+
if (preserveParameterNames != null) {
252+
c.groovyOptions.parameters = preserveParameterNames
253+
}
248254
}
255+
249256
if (!indyEnabled) {
250257
project.logger.info('Grails: Groovy invokedynamic (indy) is disabled to improve performance (see issue #15293).')
251258
project.logger.info(' To enable invokedynamic: grails { indy = true } in build.gradle')
@@ -528,7 +535,7 @@ ${importStatements}
528535

529536
protected GrailsExtension registerGrailsExtension(Project project) {
530537
if (project.extensions.findByName('grails') == null) {
531-
project.extensions.add('grails', new GrailsExtension(project))
538+
project.extensions.create('grails', GrailsExtension, project)
532539
}
533540
}
534541

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.grails.gradle.plugin.core
20+
21+
class GrailsGradlePreserveParametersSpec extends GradleSpecification {
22+
23+
def "Grails extension is created with default preserveParameterNames = true"() {
24+
given:
25+
setupTestResourceProject('preserve-params-default')
26+
27+
when:
28+
def result = executeTask('inspectPreserveParam')
29+
30+
then:
31+
result.output.contains("HAS_PRESERVE_PARAM_ENABLED=true")
32+
}
33+
34+
def "preserveParameterNames can be configured to false via grails block"() {
35+
given:
36+
setupTestResourceProject('preserve-params-disabled')
37+
38+
when:
39+
def result = executeTask('inspectPreserveParam')
40+
41+
then:
42+
result.output.contains("HAS_PRESERVE_PARAM_ENABLED=false")
43+
}
44+
45+
def "preserveParameterNames falls back to true convention when assigned null"() {
46+
given:
47+
setupTestResourceProject('preserve-params-null')
48+
49+
when:
50+
def result = executeTask('inspectPreserveParam')
51+
52+
then:
53+
// For Gradle properties, assigning null does not set the value to null as the final resolved value.
54+
// It clears/unsets the explicit value. Once the explicit value is absent, getOrNull() resolves to
55+
// the property’s convention value, which is true.
56+
result.output.contains("HAS_PRESERVE_PARAM_ENABLED=true")
57+
}
58+
59+
def "preserveParameterNames provider with no value leaves GroovyCompile parameters unchanged"() {
60+
given:
61+
setupTestResourceProject('preserve-params-provider-null')
62+
63+
when:
64+
def result = executeTask('inspectPreserveParam')
65+
66+
then:
67+
// when actually assigned via a provider null, the value will not be set and will remain the default value, which
68+
// is false for GroovyCompile
69+
result.output.contains("HAS_PRESERVE_PARAM_ENABLED=false")
70+
}
71+
72+
def "GroovyCompile tasks get parameters = true when preserveParameterNames is enabled"() {
73+
given:
74+
setupTestResourceProject('preserve-params-enabled')
75+
76+
when:
77+
def result = executeTask('inspectPreserveParam')
78+
79+
then:
80+
result.output.contains("HAS_PRESERVE_PARAM_ENABLED=true")
81+
}
82+
83+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins {
2+
id 'org.apache.grails.gradle.grails-app'
3+
}
4+
5+
tasks.register('inspectPreserveParam') {
6+
doLast {
7+
def compileTasks = tasks.withType(GroovyCompile)
8+
def paramsEnabled = compileTasks.every { it.groovyOptions.parameters }
9+
println "HAS_PRESERVE_PARAM_ENABLED=${paramsEnabled}"
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
grailsVersion=__PROJECT_VERSION__

grails-gradle/plugins/src/test/resources/test-projects/preserve-params-default/grails-app/conf/application.yml

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'test-preserve-params-default'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
plugins {
2+
id 'org.apache.grails.gradle.grails-app'
3+
}
4+
5+
grails {
6+
preserveParameterNames = false
7+
}
8+
9+
tasks.register('inspectPreserveParam') {
10+
doLast {
11+
def compileTasks = tasks.withType(GroovyCompile)
12+
def paramsEnabled = compileTasks.every { it.groovyOptions.parameters }
13+
println "HAS_PRESERVE_PARAM_ENABLED=${paramsEnabled}"
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
grailsVersion=__PROJECT_VERSION__

grails-gradle/plugins/src/test/resources/test-projects/preserve-params-disabled/grails-app/conf/application.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)