Skip to content

Commit

Permalink
Grails Gradle Plugin: Always configure system properties of BuildSett…
Browse files Browse the repository at this point in the history
…ings when executing command like bootRun, runCommand, runScript
  • Loading branch information
rainboyan committed May 24, 2023
1 parent 8f10eff commit 3a84fb2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import java.util.regex.Pattern

import groovy.transform.CompileStatic

import grails.io.IOUtils

/**
* Build time settings and configuration
*
Expand Down Expand Up @@ -259,8 +257,7 @@ class BuildSettings {
BASE_DIR = new File(appBaseDir)
}
else {
File foundAppDir = IOUtils.findApplicationDirectoryFile()
BASE_DIR = foundAppDir ?: new File('.')
BASE_DIR = new File('.')
}

String appDir = System.getProperty(APP_DIR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ import org.gradle.process.JavaForkOptions
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.springframework.boot.gradle.dsl.SpringBootExtension
import org.springframework.boot.gradle.plugin.SpringBootPlugin
import org.springframework.boot.gradle.tasks.run.BootRun

import grails.util.BuildSettings
import grails.util.Environment
import grails.util.GrailsNameUtils
import grails.util.Metadata
Expand All @@ -61,6 +61,7 @@ import org.grails.gradle.plugin.commands.ApplicationContextCommandTask
import org.grails.gradle.plugin.commands.ApplicationContextScriptTask
import org.grails.gradle.plugin.model.GrailsClasspathToolingModelBuilder
import org.grails.gradle.plugin.run.FindMainClassTask
import org.grails.gradle.plugin.util.BuildSettings
import org.grails.gradle.plugin.util.SourceSets
import org.grails.io.support.FactoriesLoaderSupport

Expand Down Expand Up @@ -108,16 +109,19 @@ class GrailsGradlePlugin extends GroovyPlugin {
}

void apply(Project project) {
grailsAppDir = SourceSets.resolveGrailsAppDir(project)
grailsVersion = resolveGrailsVersion(project)

// Keep configure system properties First
configureGrailsBuildSettings(project)

// reset the environment to ensure it is resolved again for each invocation
Environment.reset()

if (project.tasks.findByName('compileGroovy') == null) {
super.apply(project)
}

grailsVersion = resolveGrailsVersion(project)
grailsAppDir = SourceSets.resolveGrailsAppDir(project)

excludeDependencies(project)

configureProfile(project)
Expand All @@ -136,8 +140,6 @@ class GrailsGradlePlugin extends GroovyPlugin {

registerFindMainClassTask(project)

configureGrailsBuildSettings(project)

configureFileWatch(project)

enableNative2Ascii(project, grailsVersion)
Expand Down Expand Up @@ -287,6 +289,13 @@ class GrailsGradlePlugin extends GroovyPlugin {

@CompileStatic
protected void configureSpringBootExtension(Project project) {
project.getTasks().withType(BootRun, {
systemProperty(BuildSettings.APP_BASE_DIR, project.projectDir.absolutePath)
systemProperty(BuildSettings.APP_DIR, project.file(grailsAppDir).absolutePath)
systemProperty(BuildSettings.PROJECT_TARGET_DIR, project.buildDir.absolutePath)
systemProperty(BuildSettings.PROJECT_RESOURCES_DIR, new File(project.buildDir, 'resources/main').absolutePath)
systemProperty(BuildSettings.PROJECT_CLASSES_DIR, new File(project.buildDir, 'classes/groovy/main').absolutePath)
})
}

@CompileStatic
Expand Down Expand Up @@ -316,7 +325,10 @@ class GrailsGradlePlugin extends GroovyPlugin {
@CompileStatic
protected String configureGrailsBuildSettings(Project project) {
System.setProperty(BuildSettings.APP_BASE_DIR, project.projectDir.absolutePath)
System.setProperty(BuildSettings.APP_DIR, SourceSets.resolveGrailsAppDir(project))
System.setProperty(BuildSettings.APP_DIR, project.file(grailsAppDir).absolutePath)
System.setProperty(BuildSettings.PROJECT_TARGET_DIR, project.buildDir.absolutePath)
System.setProperty(BuildSettings.PROJECT_RESOURCES_DIR, new File(project.buildDir, 'resources/main').absolutePath)
System.setProperty(BuildSettings.PROJECT_CLASSES_DIR, new File(project.buildDir, 'classes/groovy/main').absolutePath)
}

@CompileDynamic
Expand All @@ -332,6 +344,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
project.tasks.create(taskName, ApplicationContextCommandTask) {
classpath = fileCollection
command = commandName
systemProperty BuildSettings.APP_BASE_DIR, project.projectDir.absolutePath
systemProperty Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.getName())
if (project.hasProperty('args')) {
args(CommandLineParser.translateCommandline(project.args))
Expand Down Expand Up @@ -388,11 +401,6 @@ class GrailsGradlePlugin extends GroovyPlugin {

grailsVersion = grailsVersion ?: new GrailsDependenciesDependencyManagement().getGrailsVersion()

if (!grailsVersion) {
Properties grailsBuildProperties = new Properties()
grailsBuildProperties.load(BuildSettings.getResourceAsStream('/grails-build.properties'))
grailsVersion = grailsBuildProperties.getProperty('grails.version')
}
grailsVersion
}

Expand Down Expand Up @@ -506,6 +514,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
@CompileDynamic
protected JavaExec createConsoleTask(Project project, TaskContainer tasks, Configuration configuration) {
tasks.create('console', JavaExec) {
systemProperty BuildSettings.APP_BASE_DIR, project.projectDir.absolutePath
classpath = project.sourceSets.main.runtimeClasspath + configuration
mainClass.set('grails.ui.console.GrailsConsole')
}
Expand All @@ -514,6 +523,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
@CompileDynamic
protected JavaExec createShellTask(Project project, TaskContainer tasks, Configuration configuration) {
tasks.create('shell', JavaExec) {
systemProperty BuildSettings.APP_BASE_DIR, project.projectDir.absolutePath
classpath = project.sourceSets.main.runtimeClasspath + configuration
mainClass.set('grails.ui.shell.GrailsShell')
standardInput = System.in
Expand Down Expand Up @@ -646,7 +656,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
project.tasks.create('runScript', ApplicationContextScriptTask) {
classpath = project.sourceSets.main.runtimeClasspath + project.configurations.console + project.configurations.profile
systemProperty Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.getName())
systemProperty 'base.dir', project.projectDir
systemProperty BuildSettings.APP_BASE_DIR, project.projectDir
systemProperty "spring.devtools.restart.enabled", false
if (project.hasProperty('args')) {
args(CommandLineParser.translateCommandline(project.args))
Expand All @@ -661,7 +671,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
project.tasks.create('runCommand', ApplicationContextCommandTask) {
classpath = project.sourceSets.main.runtimeClasspath + project.configurations.console + project.configurations.profile
systemProperty Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.getName())
systemProperty 'base.dir', project.projectDir
systemProperty BuildSettings.APP_BASE_DIR, project.projectDir
systemProperty "spring.devtools.restart.enabled", false
if (project.hasProperty('args')) {
args(CommandLineParser.translateCommandline(project.args))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.grails.gradle.plugin.util

import groovy.transform.CompileStatic

/**
* Build time settings and configuration
*
* GrailsGradlePlugin use this BuildSettings to avoid pre init {@link grails.util.BuildSettings}
*
* @author Michael Yan
* @since 2022.1.7
*/
@CompileStatic
class BuildSettings {

/**
* The base directory of the application
*/
public static final String APP_BASE_DIR = 'base.dir'

/**
* The app directory of the application
*/
public static final String APP_DIR = 'grails.app.dir'

/**
* The name of the system property for the the project target directory.
* Must be set if Gradle build location is changed.
*/
public static final String PROJECT_TARGET_DIR = 'grails.project.target.dir'

/**
* The name of the system property for {@link #}.
*/
public static final String PROJECT_RESOURCES_DIR = 'grails.project.resource.dir'

/**
* The name of the system property for the project classes directory.
*/
public static final String PROJECT_CLASSES_DIR = 'grails.project.class.dir'

}

0 comments on commit 3a84fb2

Please sign in to comment.