From a204b412fd7f575ba53af9a3c10e42b33051f704 Mon Sep 17 00:00:00 2001 From: Mattias Reichel Date: Mon, 13 Jan 2025 16:17:06 +0100 Subject: [PATCH] Fix `grails-plugin.xml` generation and `importJavaTime` setting (#388) * fix: set default `importJavaTime` value to `false` The `importJavaTime` setting modifies the import behavior for Java Date/Time classes by applying a config script to the Groovy compiler. This causes issues in Grails plugins, as the config script interferes with the generation of the `META-INF/grails-plugin.xml` file. By defaulting `importJavaTime` to `false`, these issues are avoided until a more robust solution can be implemented. * fix: honor the `importJavaTime` setting This fix addresses a problem where the `importJavaTime` setting was not honored as it was used in the evaluation phase, before the project got a chance to change it. * build: add the `grails-bom` The `grails-bom` is no longer an `api` dependency in `grails-core`. * fix: support multiple compiler configs to be merged This commit will enable more than one compiler configuration to be used by merging them. It will also merge the compiler configs for generating `grails-plugin.xml` and importing Java Date/Time. --- build.gradle | 2 + .../gradle/plugin/core/GrailsExtension.groovy | 2 +- .../plugin/core/GrailsGradlePlugin.groovy | 37 ++++++++++--------- .../core/GrailsPluginGradlePlugin.groovy | 30 +++++---------- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/build.gradle b/build.gradle index a874db2a..dfd59c96 100644 --- a/build.gradle +++ b/build.gradle @@ -75,6 +75,8 @@ java { } dependencies { + implementation platform("org.grails:grails-bom:$grailsVersion") + // compile grails-gradle-plugin with the Groovy version provided by Gradle // to ensure build compatibility with Gradle, currently Groovy 3.0.x // see: https://docs.gradle.org/current/userguide/compatibility.html#groovy diff --git a/src/main/groovy/org/grails/gradle/plugin/core/GrailsExtension.groovy b/src/main/groovy/org/grails/gradle/plugin/core/GrailsExtension.groovy index 87fa60ea..890f91b7 100644 --- a/src/main/groovy/org/grails/gradle/plugin/core/GrailsExtension.groovy +++ b/src/main/groovy/org/grails/gradle/plugin/core/GrailsExtension.groovy @@ -60,7 +60,7 @@ class GrailsExtension { /** * Whether java.time.* package should be a default import package */ - boolean importJavaTime = true + boolean importJavaTime = false /** * Configure the reloading agent diff --git a/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy b/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy index c15240c9..0606bfb5 100644 --- a/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy +++ b/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy @@ -143,6 +143,16 @@ class GrailsGradlePlugin extends GroovyPlugin { configureRunCommand(project) configurePathingJar(project) + + def configScriptTask = project.tasks.create('configScript') + def configFile = project.layout.buildDirectory.file('config.groovy') + configScriptTask.outputs.file(configFile) + addJavaTimeImport(project, configScriptTask) + + project.tasks.withType(GroovyCompile).configureEach { GroovyCompile task -> + task.dependsOn('configScript') + task.groovyOptions.configurationScript = project.tasks.named('configScript').get().outputs.files.singleFile + } } protected void excludeDependencies(Project project) { @@ -289,26 +299,19 @@ class GrailsGradlePlugin extends GroovyPlugin { } } } + } - GrailsExtension grailsExt = project.extensions.getByType(GrailsExtension) - project.tasks.withType(GroovyCompile).configureEach { groovyCompileTask -> + protected void addJavaTimeImport(Project project, Task configScriptTask) { + configScriptTask.doLast { + GrailsExtension grailsExt = project.extensions.getByType(GrailsExtension) if (grailsExt.importJavaTime) { - groovyCompileTask.doFirst { - def configScriptStream = getClass().getResourceAsStream("/GrailsCompilerConfig.groovy") - if (configScriptStream != null) { - def tempConfigScriptFile = File.createTempFile("build/GrailsCompilerConfig", ".groovy") - tempConfigScriptFile.mkdirs() - tempConfigScriptFile.deleteOnExit() - - def existingScript = groovyCompileTask.groovyOptions.configurationScript - if (existingScript) { - tempConfigScriptFile << existingScript.text - } - - tempConfigScriptFile.text = configScriptStream.text - groovyCompileTask.groovyOptions.configurationScript = tempConfigScriptFile - } + outputs.files.singleFile << ''' + configuration.with { + def importCustomizer = new org.codehaus.groovy.control.customizers.ImportCustomizer() + importCustomizer.addStarImports('java.time') + addCompilationCustomizers(importCustomizer) } + '''.stripIndent(16) } } } diff --git a/src/main/groovy/org/grails/gradle/plugin/core/GrailsPluginGradlePlugin.groovy b/src/main/groovy/org/grails/gradle/plugin/core/GrailsPluginGradlePlugin.groovy index 90aa0b66..992c87fb 100644 --- a/src/main/groovy/org/grails/gradle/plugin/core/GrailsPluginGradlePlugin.groovy +++ b/src/main/groovy/org/grails/gradle/plugin/core/GrailsPluginGradlePlugin.groovy @@ -256,32 +256,22 @@ class GrailsPluginGradlePlugin extends GrailsGradlePlugin { } } - @CompileDynamic protected void configureProjectNameAndVersionASTMetadata(Project project) { - def configScriptTask = project.tasks.create('configScript') - - def configFile = project.file("$project.buildDir/config.groovy") - configScriptTask.outputs.file(configFile) - def projectName = project.name def projectVersion = project.version + def configScriptTask = project.tasks.named('configScript').get() configScriptTask.inputs.property('name', projectName) configScriptTask.inputs.property('version', projectVersion) configScriptTask.doLast { - configFile.parentFile.mkdirs() - configFile.text = """ -withConfig(configuration) { - inline(phase: 'CONVERSION') { source, context, classNode -> - classNode.putNodeMetaData('projectVersion', '$projectVersion') - classNode.putNodeMetaData('projectName', '$projectName') - classNode.putNodeMetaData('isPlugin', 'true') - } -} -""" - } - project.tasks.getByName('compileGroovy').dependsOn(configScriptTask) - project.compileGroovy { - groovyOptions.configurationScript = configFile + outputs.files.singleFile << """ + withConfig(configuration) { + inline(phase: 'CONVERSION') { source, context, classNode -> + classNode.putNodeMetaData('projectVersion', '$projectVersion') + classNode.putNodeMetaData('projectName', '$projectName') + classNode.putNodeMetaData('isPlugin', 'true') + } + } + """.stripIndent(12) } }