Grails

Grails Goodness: Saving Server Port In A File

Posted on by  
Hubert Klein Ikkink

In a previous post we learned how to save the application PID in a file when we start our Grails application. We can also save the port number the application uses in a file when we run our Grails application. We must use the class EmbeddedServerPortFileWriter and add it as a listener to the GrailsApp instance. By default the server port is saved in a file application.port. But we can pass a custom file name or File object to the constructor of the EmbeddedServerPortFileWriter class.

In the following example we use the file name application.server.port to store the port number:

Continue reading →

Grails Goodness: Go To Related Classes In IntelliJ IDEA

Posted on by  
Hubert Klein Ikkink

Normally in a Grails application we have classes that are related to each other, but are located in different directories. For example a controller with several views. Or a Grails service with corresponding specifications. In IntelliJ IDEA we can use Choose Target and IDEA will show classes, files and methods that are relevant for the current file we are editing. The keybinding on my Mac OSX is Ctrl+Cmd+Up, but can be different on your computer and operating system. We can also choose the menu option Navigate | Related symbol....

In the following example we are editing the file MessagesController. We select the action Choose Target, IntelliJ IDEA shows a popup menu with the views for this controller and the specification class:

Continue reading →

Grails Goodness: Change Locale With Request Parameter

Posted on by  
Hubert Klein Ikkink

Grails has internationalisation (i18n) support built-in. It is very easy to add messages for different locales that can be displayed to the user. The messages are stored in properties files in the directory grails-app/i18n. Grails checks the request Accept-Language header to set the default locale for the application. If we want to force a specific locale, for example for testing new messages we added to the i18n property files, we can specify the request parameter lang. We specify a locale value and the application runs with that value for new requests.

The following screenshot shows a scaffold controller for a Book domain class with a default locale en:

Continue reading →

Grails Goodness: Enable Hot Reloading For Non-Development Environments

Posted on by  
Hubert Klein Ikkink

If we run our Grails 3 application in development mode our classes and GSP's are automatically recompiled if we change the source file. We change our source code, refresh the web browser and see the results of our new code. If we run our application with another environment, like production or a custom environment, then the reloading of classes is disabled. But sometimes we have a different environment, but still want to have hot reloading of our classes and GSP's. To enable this we must use the Java system property grails.reload.enabled and reconfigure the Gradle bootRun task to pass this system property.

Let's change our Gradle build file and pass the Java system property grails.reload.enabled to the bootRun task if it is set. We use the constant Environment.RELOAD_ENABLED to reference the Java system property.

Continue reading →

Grails Goodness: Creating A Runnable Jar

Posted on by  
Hubert Klein Ikkink

Grails 3 makes it very easy to create a JAR file that is runnable with a simple $java -jar command. We must use the Grails command package or the Gradle task assemble to package our application as a so-called runnable JAR file. This JAR file has all the necessary classes to start up our Grails application.

In the following example we have a Grails application sample-app. We use the Gradle task assemble to package the application into a JAR file. The resulting JAR file can be found in the build/libs directory of our project:

Continue reading →

Grails Goodness: Get List Of Application Profiles

Posted on by  
Hubert Klein Ikkink

Grails 3 introduced the concept of application profiles to Grails. A profile contains the application structure, dependencies, commands and more to configure Grails for a specific type of application. The profiles are stored on the Grails Profile repository on Github. We can go there and see which profiles are available, but it is much easier to use the list-profiles command. With this command we get an overview of all the available profiles we can use to create a new application or plugin.

The list-profiles task is only available when we are outside a Grails application directory. So just like the create-app and create-plugin we can run list-profiles from a non-Grails project directory.

Continue reading →

Grails Goodness: Run Gradle Tasks In Grails Interactive Mode

Posted on by  
Hubert Klein Ikkink

To start Grails in interactive mode we simply type grails on the command line. This starts Grails and we get an environment to run Grails commands like compile and run-app. Since Grails 3 the underlying build system has changed from Gant to Gradle. We can invoke Gradle tasks in interactive mode with the gradle command. Just like we would use Gradle from the command line we can run the same tasks, but this time when Grails is in interactive mode. Grails will use the Gradle version that belongs to the current Grails version. We even get TAB completion for Gradle tasks.

In the next example we start Grails in interactive mode and run the Gradle task components:

Continue reading →

Grails Goodness: Update Application With Newer Grails Version

Posted on by  
Hubert Klein Ikkink

In this blog post we see how to update the Grails version of our application for a Grails 3 application. In previous Grails versions there was a special command to upgrade, but with Grails 3 it is much simpler. To update an application to a newer version in the Grails 3.0.x range we only have to change the value of the property grailsVersion in the file gradle.properties.

# gradle.properties
grailsVersion=3.0.8
gradleWrapperVersion=2.3

Continue reading →

Grails Goodness: Use A Different Logging Configuration File

Posted on by  
Hubert Klein Ikkink

Since Grails 3 the logging configuration is in a separate file. Before Grails 3 we could specify the logging configuration in grails-app/conf/Config.groovy, since Grails 3 it is in the file grails-app/conf/logback.groovy. We also notice that since Grails 3 the default logging framework implementation is Logback. We can define a different Logback configuration file with the environment configuration property logging.config. We can set this property in grails-app/conf/application.yml, as Java system property (-Dlogging.config=<location>) or environment variable (LOGGING_CONFIG). Actually all rules for external configuration of Spring Boot apply for the configuration property logging.config.

In the following example configuration file we have a different way of logging in our Grails application. We save it as grails-app/conf/logback-grails.groovy:

Continue reading →

Grails Goodness: Change Base Name For External Configuration Files

Posted on by  
Hubert Klein Ikkink

With Grails 3 we get the Spring Boot mechanism for loading external configuration files. The default base name for configuration files is application. Grails creates for example the file grails-app/conf/application.yml to store configuration values if we use the create-app command. To change the base name from application to something else we must specify a value for the Java system property spring.config.name.

In the following example we start Grails with the value config for the Java system property spring.config.name. So now Grails looks for file names like config.yml, config.properties, config-{env}.properties and config-{env}.yml in the default locations config directory, root directory on the filesystem and in the class path.

Continue reading →

Grails Goodness: Use Different External Configuration Files

Posted on by  
Hubert Klein Ikkink

A Grails 3 application uses the same mechanism to load external configuration files as a Spring Boot application. This means the default locations are the root directory or config/ directory in the class path and on the file system. If we want to specify a new directory to search for configuration files or specify the configuration files explicitly we can use the Java system property spring.config.location.

In the following example we have a configuration application.yml in the settings directory. The default base name for a configuration file is application, so we use that base name in our example. In this sample we use a YAML configuration file where we override the property sample.config for the Grails production environment.

Continue reading →

shadow-left