Skip to content

Commit

Permalink
Grace: fixed know issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rainboyan committed Jan 24, 2024
1 parent 73f5d4c commit 601f695
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 97 deletions.
89 changes: 46 additions & 43 deletions grace-boot/src/main/groovy/grails/boot/Grails.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,50 +421,53 @@ protected void configureDirectoryWatcher(DirectoryWatcher directoryWatcher, Stri

protected void printRunStatus(ConfigurableApplicationContext applicationContext) {
try {
GrailsApplication app = applicationContext.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication.class);
String protocol = app.getConfig().getProperty("server.ssl.key-store") != null ? "https" : "http";
if (applicationContext.getParent() != null) {
applicationContext.publishEvent(
new ApplicationPreparedEvent(
this,
new String[0],
(ConfigurableApplicationContext) applicationContext.getParent())
);
}
String contextPath = app.getConfig().getProperty("server.servlet.context-path", "");
String hostName = app.getConfig().getProperty("server.address", "localhost");
int port = 8080;
if (applicationContext instanceof WebServerApplicationContext) {
port = ((WebServerApplicationContext) applicationContext).getWebServer().getPort();
}
String hostAddress = "localhost";
try {
hostAddress = InetAddress.getLocalHost().getHostAddress();
}
catch (UnknownHostException e) {
getApplicationLog().warn("The host name could not be determined, using `localhost` as fallback");
Log log = getApplicationLog();
if (log.isDebugEnabled()) {
GrailsApplication app = applicationContext.getBean(GrailsApplication.APPLICATION_ID, GrailsApplication.class);
String protocol = app.getConfig().getProperty("server.ssl.key-store") != null ? "https" : "http";
if (applicationContext.getParent() != null) {
applicationContext.publishEvent(
new ApplicationPreparedEvent(
this,
new String[0],
(ConfigurableApplicationContext) applicationContext.getParent())
);
}
String contextPath = app.getConfig().getProperty("server.servlet.context-path", "");
String hostName = app.getConfig().getProperty("server.address", "localhost");
int port = 8080;
if (applicationContext instanceof WebServerApplicationContext) {
port = ((WebServerApplicationContext) applicationContext).getWebServer().getPort();
}
String hostAddress = "localhost";
try {
hostAddress = InetAddress.getLocalHost().getHostAddress();
}
catch (UnknownHostException e) {
log.warn("The host name could not be determined, using `localhost` as fallback");
}
StringBuilder sb = new StringBuilder();
sb.append("%n----------------------------------------------------------------------------------------------");
sb.append("%n Application: %s");
sb.append("%n Version: %s");
sb.append("%n Environment: %s");
sb.append("%n Local: %s://%s:%s%s");
sb.append("%n External: %s://%s:%s%s");
sb.append("%n----------------------------------------------------------------------------------------------");
sb.append("%n");
log.debug(String.format(sb.toString(),
app.getConfig().getProperty("info.app.name"),
app.getConfig().getProperty("info.app.version"),
Environment.getCurrent().getName(),
protocol,
hostName,
port,
contextPath,
protocol,
hostAddress,
port,
contextPath));
}
StringBuilder sb = new StringBuilder();
sb.append("%n----------------------------------------------------------------------------------------------");
sb.append("%n Application: %s");
sb.append("%n Version: %s");
sb.append("%n Environment: %s");
sb.append("%n Local: %s://%s:%s%s");
sb.append("%n External: %s://%s:%s%s");
sb.append("%n----------------------------------------------------------------------------------------------");
sb.append("%n");
getApplicationLog().info(String.format(sb.toString(),
app.getConfig().getProperty("info.app.name"),
app.getConfig().getProperty("info.app.version"),
Environment.getCurrent().getName(),
protocol,
hostName,
port,
contextPath,
protocol,
hostAddress,
port,
contextPath));
}
catch (Exception ignored) {
}
Expand Down
17 changes: 13 additions & 4 deletions grace-boot/src/main/groovy/grails/boot/GrailsBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-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.
Expand All @@ -15,22 +15,24 @@
*/
package grails.boot;

import io.micronaut.spring.context.MicronautApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

/**
* Fluent API for constructing Grails instances.
* Simple extension of {@link SpringApplicationBuilder}.
*
* @author Graeme Rocher
* @author Michael Yan
* @since 3.0.6
*/
public class GrailsBuilder extends SpringApplicationBuilder {

private MicronautApplicationContext micronautContext;
private ConfigurableApplicationContext micronautContext;

public GrailsBuilder(Class<?>... sources) {
super(sources);
Expand All @@ -43,7 +45,14 @@ protected SpringApplication createSpringApplication(ResourceLoader resourceLoade

public GrailsBuilder enableMicronaut() {
if (ClassUtils.isPresent("io.micronaut.spring.context.MicronautApplicationContext", getClass().getClassLoader())) {
this.micronautContext = new MicronautApplicationContext();
try {
Class<?> clazz = getClass().getClassLoader().loadClass("io.micronaut.spring.context.MicronautApplicationContext");
this.micronautContext = (ConfigurableApplicationContext) ReflectionUtils.accessibleConstructor(clazz).newInstance();
}
catch (Exception e) {
throw new IllegalStateException(
"Can't enable Micronaut as the parent application context of Grails", e);
}
}
else {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
public final class GrailsVersion {

private static final Pattern VERSION_PATTERN = Pattern.compile("((\\d+)(\\.\\d+)+)(-(\\p{Alpha}+)-(\\w+))?(-(SNAPSHOT|\\d{14}([-+]\\d{4})?))?");
private static final Pattern VERSION_PATTERN = Pattern.compile("((\\d+)(\\.\\d+)+)(-(\\p{Alpha}+)(\\w+))?(-(SNAPSHOT|\\d{14}([-+]\\d{4})?))?");

private final String version;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ private void initializeForSearchLocation(String searchLocation) {
this.classSearchDirectories.add(directory.getCanonicalPath());
}
}
File[] appDirectories = new File(searchLocationPlusSlash + "app")
.listFiles(file -> file.isDirectory() && !file.isHidden());

if (appDirectories != null) {
for (File directory : appDirectories) {
this.classSearchDirectories.add(directory.getCanonicalPath());
}
}
}
catch (IOException ignored) {
}
Expand Down Expand Up @@ -180,6 +188,11 @@ public Resource findResourceForClassName(String className) {
this.classNameToResourceCache.put(className, resource);
break;
}
resource = resolveExceptionSafe("app/domain/**/" + className + ext);
if (resource != null && resource.exists()) {
this.classNameToResourceCache.put(className, resource);
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import groovy.transform.CompileStatic
import groovy.transform.InheritConstructors
import org.apache.groovy.groovysh.Groovysh
import org.apache.groovy.groovysh.InteractiveShellRunner
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.tools.shell.IO
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationContextFactory
Expand Down Expand Up @@ -86,6 +87,7 @@ class GrailsShell extends Grails {
bindingCustomizers?.each { customizer -> customizer.customize(binding) }

Groovysh groovysh = new Groovysh(binding, new IO()) {
CompilerConfiguration configuration = CompilerConfiguration.DEFAULT

@Override
void displayWelcomeBanner(InteractiveShellRunner runner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class GlobalGrailsPluginTransformation implements ASTTransformation, Compilation
Map<String, Object> pluginProperties = info.getProperties()
String pluginVersion = pluginProperties['version'] ?: projectVersion
String grailsVersion = pluginProperties['grailsVersion'] ?: getClass().getPackage().getImplementationVersion() + ' > *'
String excludes = pluginProperties['pluginExcludes']
Object excludes = pluginProperties['pluginExcludes']
if (excludes instanceof List) {
pluginExcludes.clear()
pluginExcludes.addAll(excludes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ class GrailsGradlePlugin extends GroovyPlugin {

@CompileDynamic
private void applyBomImport(DependencyManagementExtension dme, Project project) {
String grailsVersion = resolveGrailsVersion(project)
String springBootVersion = resolveSpringBootVersion(project)
dme.imports({
mavenBom("org.grails:grails-bom:${grailsVersion}")
mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
mavenBom("org.graceframework:grace-bom:${grailsVersion}")
})
dme.setApplyMavenExclusions(false)
}
Expand All @@ -215,7 +216,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
}

void addDefaultProfile(Project project, Configuration profileConfig) {
project.dependencies.add(PROFILE_CONFIGURATION, "org.grails.profiles:${System.getProperty('grails.profile') ?: defaultProfile}:")
project.dependencies.add(PROFILE_CONFIGURATION, "org.graceframework.profiles:${System.getProperty('grails.profile') ?: defaultProfile}:")
}

@CompileDynamic
Expand All @@ -226,7 +227,7 @@ class GrailsGradlePlugin extends GroovyPlugin {

Task buildPropertiesTask = project.tasks.create('buildProperties')
Map<String, Object> buildPropertiesContents = [
'grails.env': Environment.isSystemSet() ? Environment.current.name : Environment.PRODUCTION.name,
'grails.env': Environment.isSystemSet() ? Environment.current.getName() : Environment.PRODUCTION.getName(),
'info.app.name': project.name,
'info.app.version': project.version instanceof Serializable ? project.version : project.version.toString(),
'info.app.grailsVersion': grailsVersion]
Expand Down Expand Up @@ -331,7 +332,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
project.tasks.create(taskName, ApplicationContextCommandTask) {
classpath = fileCollection
command = commandName
systemProperty Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.name)
systemProperty Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.getName())
if (project.hasProperty('args')) {
args(CommandLineParser.translateCommandline(project.args))
}
Expand Down Expand Up @@ -383,7 +384,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
}

protected String resolveGrailsVersion(Project project) {
def grailsVersion = project.findProperty('grailsVersion')
def grailsVersion = project.findProperty('grailsVersion') ?: project.findProperty('graceVersion')

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

Expand Down Expand Up @@ -470,8 +471,8 @@ class GrailsGradlePlugin extends GroovyPlugin {
TaskContainer tasks = project.tasks

String grailsEnvSystemProperty = System.getProperty(Environment.KEY)
tasks.withType(Test).each systemPropertyConfigurer.curry(grailsEnvSystemProperty ?: Environment.TEST.name)
tasks.withType(JavaExec).each systemPropertyConfigurer.curry(grailsEnvSystemProperty ?: Environment.DEVELOPMENT.name)
tasks.withType(Test).each systemPropertyConfigurer.curry(grailsEnvSystemProperty ?: Environment.TEST.getName())
tasks.withType(JavaExec).each systemPropertyConfigurer.curry(grailsEnvSystemProperty ?: Environment.DEVELOPMENT.getName())
}

protected void configureConsoleTask(Project project) {
Expand Down Expand Up @@ -557,9 +558,9 @@ class GrailsGradlePlugin extends GroovyPlugin {

taskContainer.getByName(sourceSet.processResourcesTaskName) { AbstractCopyTask task ->
GrailsExtension grailsExt = project.extensions.getByType(GrailsExtension)
boolean native2ascii = grailsExt.native2ascii
boolean native2ascii = grailsExt.isNative2ascii()
task.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
if (native2ascii && grailsExt.native2asciiAnt && !taskContainer.findByName('native2ascii')) {
if (native2ascii && grailsExt.isNative2asciiAnt() && !taskContainer.findByName('native2ascii')) {
File destinationDir = ((ProcessResources) task).destinationDir
Task native2asciiTask = createNative2AsciiTask(taskContainer, project.file("${grailsAppDir}/i18n"), destinationDir)
task.dependsOn(native2asciiTask)
Expand All @@ -581,7 +582,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
filter(ReplaceTokens, tokens: replaceTokens)
}
}
else if (!grailsExt.native2asciiAnt) {
else if (!grailsExt.isNative2asciiAnt()) {
task.from(sourceSet.resources) {
include '**/*.properties'
filter(ReplaceTokens, tokens: replaceTokens)
Expand Down Expand Up @@ -644,7 +645,7 @@ class GrailsGradlePlugin extends GroovyPlugin {
if (project.tasks.findByName('runScript') == null) {
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.name)
systemProperty Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.getName())
if (project.hasProperty('args')) {
args(CommandLineParser.translateCommandline(project.args))
}
Expand All @@ -655,9 +656,19 @@ class GrailsGradlePlugin extends GroovyPlugin {
@CompileDynamic
protected void configureRunCommand(Project project) {
if (project.tasks.findByName('runCommand') == null) {
def findMainClass = project.tasks.findByName('findMainClass')
findMainClass.doLast {
ExtraPropertiesExtension extraProperties = (ExtraPropertiesExtension) project.getExtensions().getByName('ext')
def mainClassName = extraProperties.get('mainClassName')
if (mainClassName) {
project.tasks.withType(ApplicationContextCommandTask) { ApplicationContextCommandTask task ->
task.args mainClassName
}
}
}
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.name)
systemProperty Environment.KEY, System.getProperty(Environment.KEY, Environment.DEVELOPMENT.getName())
if (project.hasProperty('args')) {
args(CommandLineParser.translateCommandline(project.args))
}
Expand Down Expand Up @@ -699,7 +710,7 @@ class GrailsGradlePlugin extends GroovyPlugin {

GrailsExtension grailsExt = project.extensions.getByType(GrailsExtension)

if (grailsExt.pathingJar && Os.isFamily(Os.FAMILY_WINDOWS)) {
if (grailsExt.isPathingJar() && Os.isFamily(Os.FAMILY_WINDOWS)) {
project.tasks.withType(JavaExec) { JavaExec task ->
if (task.name in ['console', 'shell']
|| task instanceof ApplicationContextCommandTask
Expand Down
Loading

0 comments on commit 601f695

Please sign in to comment.