We can extend the integrate-with
command in Grails to generate files for a custom IDE or build system. We must add a _Events.groovy
file to our Grails projects and then write an implementation for the eventIntegrateWithStart
event. Inside the event we must define a new closure with our code to generate files. The name of the closure must have the following pattern: binding.integrate_CustomIdentifier_
. The value for CustomIdentifier can be used as an argument for the integrate-with
command.
Suppose we want to extend integrate-with
to generate a simple Sublime Text project file. First we create a template Sublime Text project file where we define folders for a Grails application. We create the folder src/ide-support/sublimetext
and add the file grailsProject.sublimetext-project
with the following contents:
Continue reading →
We can use the integrateWith
command with Grails to generate for example IDE project files and build system files. We specify via an extra argument the type of files to be generated. We can use this command also to create a .gitignore
file with some default settings for files to be ignored in Grails projects.
$ grails integrate-with --git
Continue reading →
In a previous post we learned how we can use Hibernate native SQL queries in our Grails application. We can also execute custom SQL with Groovy SQL. We must create a new instance of groovy.sql.Sql
in our code to execute SQL code. The easiest way is to use a javax.sql.DataSource
as a constructor argument for the groovy.sql.Sql
class. In a Grails application context we already have a DataSource
and we can use it to inject it into our code. We must use the name dataSource
to reference the default datasource in a Grails application.
In the following sample we invoke a custom query (for Firebird) using Groovy SQL. Notice we define a property dataSource
in the Grails service PersonService
and Grails will automatically inject a DataSource
instance.
Continue reading →
Sometimes we want to use Hibernate native SQL in our code. For example we might need to invoke a selectable stored procedure, we cannot invoke in another way. To invoke a native SQL query we use the method createSQLQuery()
which is available from the Hibernate session object. In our Grails code we must then first get access to the current Hibernate session. Luckily we only have to inject the sessionFactory
bean in our Grails service or controller. To get the current session we invoke the getCurrentSession()
method and we are ready to execute a native SQL query. The query itself is defined as a String
value and we can use placeholders for variables, just like with other Hibernate queries.
In the following sample we create a new Grails service and use a Hibernate native SQL query to execute a selectable stored procedure with the name organisation_breadcrumbs
. This stored procedure takes one argument startId
and will return a list of results with an id, name and level column.
Continue reading →
In our Grails applications we might have fields that need the same combination of constraints. For example we want all email fields in our application to have a maximum size of 256 characters and must apply to the email constraint. If we have different classes with an email field, like domain classes and command objects, we might end of duplicating the constraints for this field. But in Grails we can combine multiple constraints for a field into a single constraint with a new name. We do this in grails-app/conf/Config.groovy
where we add the configuration property grails.gorm.default.constraints
. Here we can define global constraints with can be used in our Grails application.
Let's add a custom email constraint in our application:
Continue reading →
In the latest Grails releases we can execute our tests in so-called forked mode. This means a separate JVM is started with an isolated classpath from the Grails build system. When we want to run our tests in forked mode from within IntelliJ IDEA we get the following error: Error running forked test-app: Could not load grails build listener class (Use --stacktrace to see the full trace)
. To make running tests in forked mode work with IntelliJ IDEA we must add one of the IntelliJ IDEA supplied JAR files to the Grails classpath.
We need to search for the file grails-rt.jar
in the directory where we installed IntelliJ IDEA. For example on Mac OSX this would be Applications/IntelliJ IDEA 13.app/plugins/Grails/lib/grails-rt.jar
. We need to copy this file to the lib
directory of our Grails project. On *nix systems we can actually define a soft link to this location in the lib
directory. For example with the following command $ ln -s /Applications/IntelliJ\ IDEA\ 13.app/plugins/Grails/lib/grails-rt.jar lib/intellij-grails-rt.jar
.
Continue reading →
When we convert a List
or Set
to XML using the Grails XML marshalling support the name of the root element is either <list>
or <set>
. We can change this name by extending the org.codehaus.groovy.grails.web.converters.marshaller.xml.CollectionMarshaller
. We must override the method supports()
to denote the type of collection we want to customize the root element name for. And we must override the method getElementName()
which returns the actual name of the root element for the List
or Set
.
Let's first see the default output of a collection of Book
domain classes. In a controller we have the following code:
Continue reading →
When we use for example the compile
or war
command Grails will create files and stores them by default in the project's working directory. The location of the project working directory can be customized in our grails-app/conf/BuildConfig.groovy
configuration file. We remove the generated files with the Grails clean
command. This command will remove all compiled class files, the WAR file, cached scripts and test reports. But this doesn't remove all files in the project working directory. For example plugins or a temporary web.xml
file, which are stored in the project working directory are not removed. We must use the clean-all
command to also remove those files from the project working directory completely.
Let's take a look at the default settings in our grails-app/conf/BuildConfig.groovy
configuration file when we create a new Grails application:
Continue reading →
Since Grails 2.2 by default the run-app
command will launch the Grails application in a separate Java Virtual Machine. This is called forked Tomcat execution in Grails. This way the class path of the Grails build system and the application will not intervene and also both processes will have their own memory settings. We can see the settings in grails-app/conf/BuildConfig.groovy
where we find the configuration property grails.project.fork.run
. When we want to debug our application in an IDE like IntelliJ IDEA we cannot use the Debug command, because this will only allow us to debug the Grails build system. We will not reach breakpoints in our source code. But Grails 2.3 introduces an extra argument for the run-app
command: --debug-fork
. If we use this extra argument the JVM running the Grails application will stop and listen for a debug session to be attached and then continue. We can configure a Debug configuration in IntelliJ IDEA (or another IDE) to attach to the waiting Grails application and use breakpoints and other debugging tools like we are used to.
Suppose we have a Grails application named forked-debug and we have created a project in IDEA for this application. We click on the Select Run/Debug Configuration button and select Edit Configurations...:
Continue reading →
To generate an HTML select
we can use the Grails tag <g:select .../>
. We use the optionValue
attribute to specify a specific property we want to be used as the value. But we can also define a closure for the optionValue
attribute to further customize the value that is shown to the user.
Suppose we have a simple domain class Book
with a couple of properties. We want to combine multiple properties as the text for the HTML select
options. In the following GSP we define first a <g:select .../>
tag where we simply use the title
property. In the next <g:select .../>
tag we use a closure to combine multiple properties.
Continue reading →
Grails 2.3 added a lot of support for RESTful services. For example we can now use a respond()
method in our controllers to automatically render resources. The respond()
method accepts a resource instance as argument and a map of attributes that can be passed. We can use the includes
and excludes
keys of the map to pass which fields of our resources need to be included or excluded in the response. This way we can render partial responses based on a request parameter value.
First we start with a simple domain class Book
:
Continue reading →
If our Grails application renders XML or JSON output we can set configuration properties to enable pretty printing. This can be useful in for example in the development environment where we enable pretty printing and disable it for other environments. We set the configuration property grails.converters.default.pretty.print
with the value true
to enable pretty printing for both XML and JSON output. If we only want to pretty print XML we use the property grails.converters.xml.pretty.print
and for JSON we use grails.converters.json.pretty.print
.
First we look at the XML and JSON output when we don't enable pretty printing for a simple book resource:
Continue reading →