Skip to content

Commit

Permalink
Fix grails-plugin.xml generation and importJavaTime setting (#388)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
matrei authored Jan 13, 2025
1 parent e62f68b commit a204b41
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit a204b41

Please sign in to comment.