A few days ago Grails 3.0 was officially released. Grails is now based on Spring Boot, the build system changed from Gant to Gradle and significant parts of the framework have been rewritten. In this post we will have a look at all the major changes introduced by Grails 3.
Grails 3.0 uses the Gradle Build System for build related tasks such as compilation, runnings tests and producing binary distrubutions of your project. It is recommended to use Gradle 2.2 or above with Grails 3.0.
Read more about gradle and spring boot here.
Watch this video : https://www.youtube.com/watch?v=aro3_RZqgtU
The first two folders (build and gradle) are related to Gradle, the new build system in Grails 3. As the name implies, build is the directory where build related files like compiled classes and assembled packages are located. The gradle directory contains theGradle Wrapper that allows you to build the project without a local Gradle installation.
The content of the conf folder has also been changed. The default format for configuration files is now YAML. If you prefer to write your configuration in Groovy, you can still create a grails-app/conf/application.groovy configuration file manually.
Logback is now the default logging provider. The logging configuration (previously part of Config.groovy) has been moved toconf/logback.groovy.
Note that the conf folder is not a source directory anymore.
init is a new folder in Grails 3. It contains Bootstrap.groovy (same content as in previous Grails versions) and the new Application main class (we will look into this in the Spring Boot section).
The structure of the src folder has been changed to match Gradle conventions. Additional Java and Groovy files are now located in:
src/main/java
src/main/groovy
src/test/java
src/test/groovy
build.gradle and gradle.properties contain the build configuration. BuildConfig.groovy from previous Grails versions does no longer exist.
Traits
In Grails components like Controllers or Domain classes always had some magically attached functionality. For example, in Controllers you can use methods like render(), redirect() or getParams() without subclassing another class. In Grails 3 these features have been rewritten to make use of Traits introduced by Groovy 2.3.
Certain Traits are automatically attached to Controllers, Services, Tag libraries and so on to make framework methods available. For example, a Controller automatically gets the following Traits: TagLibraryInvoker,AsyncController, RestResponder, Controller.
The cool thing with Traits is that you can easily add them to your own classes.
For example: Assume you want to access the request and params objects outside a Grails Controller. All you have to do now is adding the WebAttributes trait to your class:
Interceptors
Grails 3 introduced standalone Interceptors. Interceptors can intercept incoming requests to perform common tasks (e.g. logging, authentication, etc.).
A new Interceptor can be created using create-interceptor command:
grails create-interceptor MyInterceptor
A newly created Interceptor looks like this:
Interceptors replace Filters used by previous Grails versions. Filters still work in Grails 3 for backwards compatibility. However, they are considered deprecated now.
If you are aware of Spring web MVC, you can easily see the similarities to Springs Handler Interceptor.
Gradle
As mentioned before, Grails 3 uses Gradle instead of Gant as build system. Gradle is used for tasks like compilation, running tests and packaging the application.
When a Grails command like grails clean is executed, the job is delegated to the corresponding Gradle task (e.g. gradle clean). The Gradle-Wrapper shipped with Grails 3 is used for this.
If you want to use a local installation of Gradle you can execute the Gradle task directly with your own Gradle version. Gradle 2.2 or newer is recommended.
The following table shows Grails commands and their corresponding Gradle tasks:
BuildConfig.groovy from previous Grails versions has been completely replaced by the Gradle configuration (build.gradle). Third party dependencies are now defined in build.gradle:
Short summary
Grails 3 comes with major changes. The code basis changed to Spring Boot, Gant was replaced with Gradle, existing features were reimplemented using Traits and new features like Profiles and Interceptors were added.
With all those changes it can become quite challenging to upgrade an existing Grails 2.x application to Grails 3 (all Plugins need to be updated first). If you plan to Upgrade to Grails 3, you should have a look at theGrails 3 upgrade guide.
Grails 3.0 uses the Gradle Build System for build related tasks such as compilation, runnings tests and producing binary distrubutions of your project. It is recommended to use Gradle 2.2 or above with Grails 3.0.
Read more about gradle and spring boot here.
Watch this video : https://www.youtube.com/watch?v=aro3_RZqgtU
The first two folders (build and gradle) are related to Gradle, the new build system in Grails 3. As the name implies, build is the directory where build related files like compiled classes and assembled packages are located. The gradle directory contains theGradle Wrapper that allows you to build the project without a local Gradle installation.
The content of the conf folder has also been changed. The default format for configuration files is now YAML. If you prefer to write your configuration in Groovy, you can still create a grails-app/conf/application.groovy configuration file manually.
Logback is now the default logging provider. The logging configuration (previously part of Config.groovy) has been moved toconf/logback.groovy.
Note that the conf folder is not a source directory anymore.
init is a new folder in Grails 3. It contains Bootstrap.groovy (same content as in previous Grails versions) and the new Application main class (we will look into this in the Spring Boot section).
The structure of the src folder has been changed to match Gradle conventions. Additional Java and Groovy files are now located in:
src/main/java
src/main/groovy
src/test/java
src/test/groovy
build.gradle and gradle.properties contain the build configuration. BuildConfig.groovy from previous Grails versions does no longer exist.
Spring 4.1 and Spring Boot
Spring Boot is the new basis for Grails. According to Graeme Rocher Grails 3 is nothing less than a ground up rewrite on top of Spring Boot.
Like Spring Boot applications Grails 3 projects come with an Application class that has a standard main()method. This means you can start a Grails 3 application simply by running the main class from your IDE.
The default Application class looks like this:
Spring Boot is the new basis for Grails. According to Graeme Rocher Grails 3 is nothing less than a ground up rewrite on top of Spring Boot.
Like Spring Boot applications Grails 3 projects come with an Application class that has a standard main()method. This means you can start a Grails 3 application simply by running the main class from your IDE.
The default Application class looks like this:
1.
class
Application
extends
GrailsAutoConfiguration {
2.
static
void
main(String[] args) {
3.
GrailsApp.run(Application)
4.
}
5.
}
Note that the war file you get when packaging the application can now be executed using the java -jarcommand:
java -jar build/libs/myApplication-0.1.war
This runs the main method which starts the application using an embedded Tomcat server. Of course you can still deploy the war file on an application server of your choice like in previous Grails versions.
The Application class acts as Spring configuration class. So, we can use Spring's @Bean annotation to define custom beans. Methods like onStartup() or onShutdown() can be overwritten if you want to execute custom code at certain application events.
java -jar build/libs/myApplication-0.1.war
This runs the main method which starts the application using an embedded Tomcat server. Of course you can still deploy the war file on an application server of your choice like in previous Grails versions.
The Application class acts as Spring configuration class. So, we can use Spring's @Bean annotation to define custom beans. Methods like onStartup() or onShutdown() can be overwritten if you want to execute custom code at certain application events.
01.
class
Application
extends
GrailsAutoConfiguration {
02.
03.
...
04.
05.
@Bean
06.
MyBean myBeanId() {
07.
return
new
MyBeanImpl();
08.
}
09.
10.
@Override
11.
void
onStartup(Map<String, Object> event) {
12.
super.onStartup(event)
13.
// custom startup code..
14.
}
15.
}
Traits
In Grails components like Controllers or Domain classes always had some magically attached functionality. For example, in Controllers you can use methods like render(), redirect() or getParams() without subclassing another class. In Grails 3 these features have been rewritten to make use of Traits introduced by Groovy 2.3.
Certain Traits are automatically attached to Controllers, Services, Tag libraries and so on to make framework methods available. For example, a Controller automatically gets the following Traits: TagLibraryInvoker,AsyncController, RestResponder, Controller.
The cool thing with Traits is that you can easily add them to your own classes.
For example: Assume you want to access the request and params objects outside a Grails Controller. All you have to do now is adding the WebAttributes trait to your class:
01.
class
MyCustomComponent
implements
WebAttributes {
02.
03.
public
MyCustomComponent() {
04.
05.
// make use of WebAttributes methods like getWebRequest() or getParams()
06.
println
"base url: "
+ webRequest.baseUrl
07.
println
"params: "
+ params
08.
...
09.
}
10.
}
Interceptors
Grails 3 introduced standalone Interceptors. Interceptors can intercept incoming requests to perform common tasks (e.g. logging, authentication, etc.).
A new Interceptor can be created using create-interceptor command:
grails create-interceptor MyInterceptor
A newly created Interceptor looks like this:
01.
class
MyInterceptor {
02.
03.
boolean
before() {
04.
// executed before a request is processed by a controller
05.
true
06.
}
07.
08.
boolean
after() {
09.
// executed after a request is processed by a controller
10.
true
11.
}
12.
13.
void
afterView() {
14.
// executed after the view has been rendered
15.
}
16.
17.
}
Interceptors replace Filters used by previous Grails versions. Filters still work in Grails 3 for backwards compatibility. However, they are considered deprecated now.
If you are aware of Spring web MVC, you can easily see the similarities to Springs Handler Interceptor.
Gradle
As mentioned before, Grails 3 uses Gradle instead of Gant as build system. Gradle is used for tasks like compilation, running tests and packaging the application.
When a Grails command like grails clean is executed, the job is delegated to the corresponding Gradle task (e.g. gradle clean). The Gradle-Wrapper shipped with Grails 3 is used for this.
If you want to use a local installation of Gradle you can execute the Gradle task directly with your own Gradle version. Gradle 2.2 or newer is recommended.
The following table shows Grails commands and their corresponding Gradle tasks:
Grails command | Gradle Task |
clean | clean |
compile | classes |
package | assemble |
run-app | run |
test-app | test |
war | assemble |
BuildConfig.groovy from previous Grails versions has been completely replaced by the Gradle configuration (build.gradle). Third party dependencies are now defined in build.gradle:
1.
dependencies {
2.
compile
'org.grails.plugins:hibernate'
3.
compile
'org.grails.plugins:cache'
4.
compile
'org.hibernate:hibernate-ehcache'
5.
runtime
'org.grails.plugins:asset-pipeline'
6.
runtime
'org.grails.plugins:scaffolding'
7.
...
8.
}
For more details, you can have a look at Dependency Management Guide in the Gradle documentation.
Profiles
Whenever you run the create-app command Grails 3 will use a Profile to create a new app.
A Profile encapsulates project structure, commands, templates and plugins. By default Grails 3 uses the webProfile, which creates an app like shown in the screenshot above.
To create a project with a different Profile, you can use the --profile parameter:
grails create-app myapp --profile=web-plugin
Grails 3 comes with three different Profiles:
Profiles
Whenever you run the create-app command Grails 3 will use a Profile to create a new app.
A Profile encapsulates project structure, commands, templates and plugins. By default Grails 3 uses the webProfile, which creates an app like shown in the screenshot above.
To create a project with a different Profile, you can use the --profile parameter:
grails create-app myapp --profile=web-plugin
Grails 3 comes with three different Profiles:
- web for standard Grails web applications
- web-plugin for web application plugins
- web-micro a minimal micro service application
File Location Differences
The location of certain files have changed or been replaced with other files in Grails 3.0. The following table lists old default locations and their respective new locations:
Old Location | New Location | Description |
---|---|---|
grails-app/conf/BuildConfig.groovy | build.gradle | Build time configuration is now defined in a Gradle build file |
grails-app/conf/Config.groovy | grails-app/conf/application.groovy | Renamed for consistency with Spring Boot |
grails-app/conf/UrlMappings.groovy | grails-app/controllers/UrlMappings.groovy | Moved since grails-app/conf is not a source directory anymore |
grails-app/conf/BootStrap.groovy | grails-app/init/BootStrap.groovy | Moved since grails-app/conf is not a source directory anymore |
scripts | src/main/scripts | Moved for consistency with Gradle |
src/groovy | src/main/groovy | Moved for consistency with Gradle |
src/java | src/main/groovy | Moved for consistency with Gradle |
test/unit | src/test/groovy | Moved for consistency with Gradle |
test/integration | src/integration-test/groovy | Moved for consistency with Gradle |
web-app | src/main/webapp | Moved for consistency with Gradle |
*GrailsPlugin.groovy | src/main/groovy | The plugin descriptor moved to a source directory |
New Files Not Present in Grails 2.x
The reason it is best to create a new application and copy your original sources to it is because there are a number of new files that are not present in Grails 2.x by default. These include:
File | Description |
---|---|
build.gradle | The Gradle build descriptor located in the root of the project |
gradle.properties | Properties file defining the Grails and Gradle versions |
grails-app/conf/logback.groovy | Logging previously defined in Config.groovy is now defined using Logback |
grails-app/conf/application.yml | Configuration can now also be defined using YAML |
grails-app/init/PACKAGE_PATH/Application.groovy | The Application class used By Spring Boot to start the application |
Files Not Present in Grails 3.x
Some files that were previously created by Grails 2.x are no longer created. These have either been removed or an appropriate replacement added. The following table lists files no longer in use:
File | Description |
---|---|
application.properties | The application name and version is now defined in build.gradle |
grails-app/conf/DataSource.groovy | Merged together into application.yml |
lib | Dependency resolution should be used to resolve JAR files |
web-app/WEB-INF/applicationContext.xml | Removed, beans can be defined in grails-app/conf/spring/resources.groovy |
src/templates/war/web.xml | Grails 3.0 no longer requires web.xml. Customizations can be done via Spring |
web-app/WEB-INF/sitemesh.xml | Removed, sitemesh filter no longer present. |
web-app/WEB-INF/tld | Removed, can be restored in src/main/webapp |
Short summary
Grails 3 comes with major changes. The code basis changed to Spring Boot, Gant was replaced with Gradle, existing features were reimplemented using Traits and new features like Profiles and Interceptors were added.
With all those changes it can become quite challenging to upgrade an existing Grails 2.x application to Grails 3 (all Plugins need to be updated first). If you plan to Upgrade to Grails 3, you should have a look at theGrails 3 upgrade guide.
0 comments:
Post a Comment