m Grails

Key Points

  1. Grails CRUD scaffolding and GORM is IBM i DFU on steroids - but more - any database, any Java runtime environment
  2. Where it fits, Grails is the fastest way to build Web apps or microservices using the right templates
  3. Grails generated apps provide a good starting point to understand how Web and REST microservices are built
  4. Grails generates Spring Boot applications and REST services
  5. Grails can build traditional MVC web apps or REST microservices using templates and generators
  6. Grails can generate simple front-ends for REST microservices - Angular, React, Web
  7. Grails profiles define which options an application or service will use
  8. Grails uses Groovy - a Java superset and a dynamic JVM language
  9. Grails v4x will use Groovy v3x when available in 2020
  10. Groovy v3x will support latest Java versions ( Lamba expressions, modules etc )
  11. I should look at running Grails REST service in Docker container similar to a Spring Boot REST service
  12. Tracking Grails releases in Github ( milestones etc ) https://github.com/grails/grails-core/releases   


References

Reference_description_with_linked_URLs_________________________Notes________________________________________________________________


https://grails.orgGrails
Grails community support slack channel ***Slack channel for community support
grails work gdocs folder
grails gdoc work 1 
my grails tutorial docs folder


https://start.grails.org/Grails Forge to start a Grails rpoject build ***
http://docs.grails.org/snapshot/guide/index.htmlGrails 5.0 documentation
https://grails.org/blog/2021-08-12-grails-5-rc1-release.htmlGrails 5.0 release notes
https://groovy-lang.org/releasenotes/groovy-3.0.html#releasenotesGroovy 3.0 release notes
https://docs.grails.org/5.0.0.RC1/guide/introduction.html#whatsNewGrails 5.0 What's new
https://github.com/grails/grails-coreGrails Github repo


https://github.com/grails/grails-core/releases/tag/v4.1.0.M1

http://docs.grails.org/4.1.0.M1/guide/introduction.html#whatsNew

grails experimental downloads for 4.1, Groovy 3
https://grails.org/blog/2021-01-22-grails-exciting-updates.htmlGrails blog - version 5x concepts **
https://grails.org/blog/index.htmlutm_campaign=2GM%20Community%
20Engagement&utm_medium=email&_hsmi=95220434&_hsenc=p2ANqtz--Gie_w6ROGkVF89tuvgvf0TvA5zGNNJOLiSLxRPsYRscSTXn6f_b5b9sjFqnW-HpRXh-Y5pJ7MiE3jgwUVf2JwyXRBrQ&utm_content=95220434&utm_source=hs_email
Grails blog
https://grails.org/blog/2020-09-10-grails-state-of-union.htmlGrails 2020 update
https://github.com/grails/grails-core/wiki/Grails-Engineering-Meeting-Notes-
(02-25-2021)?utm_campaign=2GM%20Community%20Engagement&utm_medium=email&_
hsmi=115559784&_hsenc=p2ANqtz-8qopnSpNyrjkWR__C92PWS4RkVv-Im9q7dBpFDI5UQFzxCXp8JpkW360PBl5IVRpHPw2ekUH1LAolxmLDSuJYidvOIijiADr-xlVlKxhaMZnfp2aA&utm_content=115559784&utm_source=hs_email
Grails Engineering Meeting notes - 2021
https://github.com/grails/grails-core/releasesGrails releases repo including Milestones *
https://github.com/grails/grails-core/releases?utm_campaign=2GM%20Community%20Engagement&utm_medium=email&
_hsmi=115559784&_hsenc=p2ANqtz9llp1Jy4j3NCMN5PT1JAD1Y3EZDM6uKp2KvKpT_
VB4IRn0GEdoU8nxblwgu9FXpxqs8bi7u3olDXXr7iJ9KQBkv0iyY3Kuqsl1-Pv9MtvikczwHS8&utm_content=115559784&utm_source=hs_email
Grails core releases repo
https://repo.grails.org/grails/webapp/#/artifacts/browse/tree/General/libs-snapshots/org/grails/grails-core/5.0.0.M1Grails releases - regular AND beta **


http://docs.grails.org/latest/guide/introduction.htmlGrails 4.0 online doc

http://docs.grails.org/latest/guide/single.pdf

https://drive.google.com/open?id=1mMT9PXJMt1LhKMlNIXYdvK17w9_sfSYk

Grails 4.0 single pdf
https://objectcomputing.com/products/grails/grails-roadmapGrails roadmap to v4.0


https://grailsthreebook.com/assets/pdf/grails3book-sample-preview.pdf

https://drive.google.com/open?id=16CCCp9JIztLSy2dDVJl153aogU9klgDX

Grails 3 book - sample - rest api profile

https://www.slideshare.net/paulbowler/grails-patterns-and-practices

https://drive.google.com/open?id=1GluRzUKpndHmqx4C3a87H8o0nOTln50W

Grails Patterns and Practices slide deck

https://www.sapient.com/content/dam/sapient/sapientglobalmarkets/pdf/thought-leadership/MarProg_Grails_WP_Web.pdf

https://drive.google.com/open?id=165wtwVmVXBQU2JX5uQ2QyS6NXlceS8BU

Grails Overview and Best Practices Guide - v2.0
https://opensourceconnections.com/blog/uploads/2010/01/
Introduction_to_Groovy_and_Grails.pdf
Grails and Groovy Details - 2011

https://drive.google.com/open?id=0BxqKQGV-b4WQVHptM01wVHZQOE0

https://drive.google.com/open?id=0BxqKQGV-b4WQRE52M2ozU2RTR1k

Jim Mason - 2009 - Books and Authors Web App in Grails in 1 hour

Lab loading instructions doc



Grails 4 CRUD scaffolding video Grails 4 CRUD scaffolding video **
https://www.djamware.com/post/5dee230f730254515e53f7df/grails-4-tutorial-easy-to-build-crud-web-applicationSimple Grails Scaffolding Tutorial
https://drive.google.com/open?id=1CcNKF5Mab8po1R9G_BSrYQ-uYjf2qDM5Vogella - 2010 - Grails Web Blog app - decent example on v2.0
https://drive.google.com/file/d/1fL5Q9LH5WB8XsQw71JP_8GoCgjeQt7us/viewGrails Angular CRUD app example - manage customers - older
Grails CRUD generation adding Okta authorization labGrails CRUD generation adding Okta authorization lab



Grails REST api example - see docs
https://github.com/budjb/grails-jaxrsGrails REST Jax-RS generator plugin
http://budjb.github.io/grails-jaxrs/3.x/latest/Grails REST Jax-RS generator plugin documentation
https://grails.org/plugins/tag/jax-rsGrails REST Jax-RS plugin list


https://guides.grails.org/grails-vs-nodejs/guide/index.htmlTutorial Migrate React Node.js app to Grails backend


https://groups.google.com/forum/#!topic/grails-dev-discuss/CNmd8BRuf28

https://plugins.grails.org/plugin/grails/db-reverse-engineer

https://github.com/grails-plugins/grails-db-reverse-engineer/issues

https://grails-plugins.github.io/grails-db-reverse-engineer/

Grails DB reverse engineering plugin does not work after Grails 3.1 - 2017

NO LONGER SUPPORTED




GORM
https://docs.grails.org/latest/guide/GORM.htmlGrails GORM overview

http://gorm.grails.org/6.1.x/hibernate/manual/

GORM documentation


https://github.com/smallnest/gen

Generates Go lang structs from many SQL databases

could create converter to generate simple domain classes for GORM



Grails MVC apps

https://www.djamware.com/post/5dee230f730254515e53f7df/grails-4-tutorial-easy-to-build-crud-web-application

Simple Grails Scaffolding Tutorial pdf

Simple Grails Scaffolding Tutorial
Grails Angular Tutorial pdfGrails Angular Tutorial - manual Angular coding
https://guides.grails.org/creating-your-first-grails-app/guide/index.htmlGrails MVC app with Scaffolding
https://www.baeldung.com/grails-mvc-applicationTutorial on domain, service, controller, Angular app - all manual
https://medium.com/@victortorres/create-your-first-grails-application-without-scaffold-7e4c85c76156Tutorial - manually create Grails Web app without scaffolding


Grails Concepts & Articles




Why don't more developers use Grails in 2023?

not understanding it, not aware of it

want to do custom client js coding ( eg React / Redux etc vs GSP templates extended)

don't know how to generate high performance Grails apps

don't know how to create custom Grails templates for different app & services profiles








Key Concepts



Grails Training

grails v4x books - authors 4 days
http://docs.grails.org/latest/

https://www.djamware.com/post/5d385e00fd741853d22abf5c/building-crud-web-application-using-grails-4-and-mongodb-easily

grails mvc app using spring services - mode coding
https://www.baeldung.com/grails-mvc-application

https://www.google.com/search?client=firefox-b-1-d&q=grails+version+4+tutorials


Grails and Okta

https://developer.okta.com/blog/2018/04/19/okta-with-grails


Grails CRUD app with Okta

https://developer.okta.com/blog/2018/06/04/okta-with-grails-part2


Grails with Spring Security - default

https://www.slideshare.net/JesusPerezFranco/spring-security-5?from_action=save


Why use Grails & Groovy?



https://www.quora.com/Why-is-it-that-there-are-less-people-using-Groovy-and-Grails-for-web-development

I've used Spring + Hibernate for around 4 years. I've been using Grails for the last 2 years. Here's my take:

Grails is a high productivity framework, i.e. it has some features that make it easier to build typical web functionality. Some things are easier in grails:

* Building a Xml/Json REST API.
* Integrating with Spring Security
* Integrating with Less CSS or Coffeescript.
* Integrating with Quartz
* Creating scaffoldings for basic crud functionality

All these things are easier because of abstraction. Grails offers a simpler way by abstracting all these things which happen a lot in web apps.

However once you are using a higher level of abstraction, accessing the lower apis becomes slightly difficult. I don't say it is not possible. It is possible, and there is a lot of documentation on anything you want to do, but sometimes you end up wasting a lot of time debugging or researching for something that wouldn't have been so difficult in Spring/Java because of the Grails layer you must interact through.

The good news though, is that you write a lot less code, and that is definitely worth the difference, because code is read more than it is written.


Why don't more developers use Grails in 2023?

  • not understanding it, not aware of it
  • want to do more custom client js coding ( eg React / Redux etc vs GSP templates extended) which is justifiable for some front end apps
  • don't know how to generate high performance Grails apps using GraalVM and compiled groovy
  • don't know how to create custom Grails templates for different app & services profiles
  • don't know how to generate apps from profiles for containers and Kubernetes in the cloud


Grails download & install

http://grails.org/download.html


Milestone releases are not available from the download site, email OCI directly.

You can find 4.1.0.M3 at

https://github.com/grails/grails-core/releases/tag/v4.1.0.M3.


Grails Application Forge

https://start.grails.org/


Manual Grails install

http://docs.grails.org/latest/guide/gettingStarted.html#downloadingAndInstalling

Set the GRAILS_HOME environment variable to the location where you extracted the zip

  • On Unix/Linux based systems this is typically a matter of adding something like the following export GRAILS_HOME=/path/to/grails to your profile

Then add the bin directory to your PATH variable:

  • On Unix/Linux based systems this can be done by adding   export PATH="$PATH:$GRAILS_HOME/bin"to your profile

Run version check

  • grails version


Create shell script to set Grails version


setGrails41.sh

export GRAILS_HOME=`/Library/Grails/grails-4.1.0.M1`
export PATH=$GRAILS_HOME/bin:$PATH

groovy -version

  


Grails beta versions 

Check for Grails versions released in Github here ( including milestones ) -  https://github.com/grails/grails-core/releases

For a given release / milestone - check the Github issues to see what's important that's still open - https://github.com/grails/grails-core/issues

https://github.com/grails/grails-core/releases/tag/v4.1.0.M1

http://docs.grails.org/4.1.0.M1/guide/introduction.html#whatsNew

install from Zip file

ensure GRAILS_HOME, GROOVY_HOME, JAVA_HOME are set

ensure any CLASSPATH and path updates are correct

to validate, run:

grails -version 


Grails docs on skwebteam

grails4-update-Slide_Deck_Introducing_Grails_4_Webinar.pdf

grails-181115-tutorial-guestbook-java-web-app-vogella.com-.pdf

gorm-basics-tutorial-relations-maps.pptx

grails-best-practices-web-profile-2016-MarProg_Grails_WP_Web.pdf

grails-web-mvc-tutorial-v4-mongodb-2019-vgood.pdf

grails-plugins.github.io-DB Reverse Engineering Plugin - Reference Documentation.pdf


Grails v4.0 Features

https://objectcomputing.com/products/grails/grails-roadmap


  • Groovy 2.5
  • GORM 7.0 (Hibernate 5.2 minimum, Java 8 baseline)
  • Java 8 Baseline
  • Java 11 Support
  • Spring 5.1
  • Spring Boot 2.1
  • Micronaut Integration



Grails 5x Plans 

https://grails.org/blog/2021-01-22-grails-exciting-updates.html


A major highlight of this release is the update to Apache Groovy 3, which includes support for Java 15 and significant other improvements around static compilation. We have also upgraded to the latest stable releases of some third-party libraries, including:

  • Apache Groovy 3, which offers several enhancements to the language, including GDK improvements, a new parrot parser – far more flexible than the previous versions – and a collection of other new features and capabilities.

  • SpringBoot 2.4: This release adds a significant number of new features and improvements, such as improvements to the config file processing, a new startup actuator endpoint to help identify beans that are taking longer than expected to start, and an update to Spring Framework 5.3. For more information, check out SpringBoot 2.4 release notes.

  • Gradle 6, which brings faster incremental Java and Groovy compilation, better version ordering, many bug fixes, and more. Refer to Gradle Release Notes for more information.


We will be focusing on improving the performance of the Grails application and user experience. In addition, we are planning a number of enhancements including:

  • Making GSP more modular and independent of the Grails framework
  • Transaction support in GORM for MongoDB
  • Native support for web-sockets
  • Continuously improving Grails documentation (especially around plugins, Micronaut configuration, HTTP Client, and other features)
  • Better integration with Micronaut, such as declarative HTTP Client
  • Better support for Kafka, including creating Kafka listeners through a plugin and more


Grails JAX-RS plugin documentation ( for Grails v3x )

http://budjb.github.io/grails-jaxrs/3.x/latest/guide/introduction.html

The jaxrs project is a set of Grails plugins that supports the development of RESTful web services based on the Java API for RESTful Web Services (JSR 311: JAX-RS).

It is targeted at developers who want to structure the web service layer of an application in a JSR 311 compatible way but still want to continue to use Grails' powerful features such as GORM, automated XML and JSON marshalling, Grails services, Grails filters and so on. This plugin is an alternative to Grails' built-in mechanism for implementing RESTful web services.


Features

  • Makes the JSR 311 (JAX-RS) available to Grails applications for developing RESTful web services.
  • New Grails artefact types, Resource and Provider, for JAX-RS classes.
    • JAX-RS Resource classes under grails-app/resources are auto-detected and can be modified at runtime.
    • JAX-RS Provider classes under grails-app/providers are auto-detected and can be modified at runtime.
  • Extended Grails command line interface
    • Create new resources and unit test templates via grails create-resource <resource name>.
    • Generate ready-to-use resources from domain objects via grails generate-resources <domain class name>.
  • Scaffolding
    • Generate RESTful service interfaces for Grails domain objects.
    • Content negotiation support for XML and JSON representations.
  • Ability to use any Grails feature within JAX-RS resources and providers such as:
  • Entity providers
    • Domain object providers that convert between Grails domain objects and XML or JSON representations.
    • Support classes for developing custom entity providers.
    • Support for content negotiation based on the Accept request header.
  • Easy integration testing of JAX-RS resources and providers.
  • Plugin users may choose between Jersey and Restlet as JAX-RS implementations by means of configuration.
  • jaxrs applications can be deployed to Google App Engine (GAE).



Tutorial Migrate React Node.js app to Grails backend

https://guides.grails.org/grails-vs-nodejs/guide/index.html

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 1.7 or greater installed with JAVA_HOME configured appropriately


To get started do the following:

or

The Grails guides repositories contain two folders:

  • initial Initial project. Often a simple Grails app with some additional code to give you a head-start.

  • complete A completed example. It is the result of working through the steps presented by the guide and applying those changes to the initial folder.

To complete the guide, go to the initial folder

  • cd into grails-guides/grails-vs-nodejs/initial

and follow the instructions in the next sections.


You can go right to the completed example if you cd into grails-guides/grails-vs-nodejs/complete


The completed sample project for this article can be found at:


https://github.com/grails-guides/grails-vs-nodejs/tree/master/complete

create react profile app

Every Grails project begins with a single create-app command. For the purposes of following along with this guide, you may choose to install Grails via the official website, or using sdkman (recommended). However, there is no need to install the framework on your machine to create your Grails app - instead, let’s browse to http://start.grails.org and create our application using the Grails Application Forge.

Choose the latest version of Grails (3.3.0 as of the time of writing) and select the react profile.


With the use of application profiles, Grails allows you to build modern web applications. There are profiles to facilitate the construction of REST APIs or Web applications with a Javascript front-end

start client and server apps

Once you’ve downloaded your application, expand it into a directory of your choice, cd into the project, and run the following two commands (in two separate terminal sessions):

~ ./gradlew server:bootRun   //Windows users use "gradlew.bat"

//in a second terminal session
~ ./gradlew client:bootRun

gradle wrapper can eliminate need to install Grails locally

The gradlew command launches the Gradle "wrapper”, which is provided by the Gradle build tool that is used in all Grails projects since Grails 3.0. The wrapper is a special script that actually download and install the Gradle build tool (if necessary) before running your commands. Gradle will then download all needed dependencies (including Grails) and install them in your project (caching them for future use as well). This is why you don’t need to install Grails on your machine: if your project includes the Gradle wrapper, it will handle that for you.

gradle is similar to npm for build management

It doesn’t provide the CLI that npm offers but it fulfills a similar purpose in dependency management and build-processing. When a Gradle command (or "task") is run, Gradle will first download all dependencies listed in the project’s build.gradle file, similar to running npm install.


What about the server and client portion of those two commands? Because we’re using the react profile, Grails has actually created two separate “apps” for us - the backend Grails application, and the React application (which in turn is generated via create-react-app). Gradle treats these two apps as independent subprojects, with the above names. This is called a multi-project build.

When running a Gradle “task” from the project root directory, anything after ./gradlew [project_name]: will match a task specific to that subproject. The bootRun task is configured in both projects to start the respective app.

Where does bootRun come from? This Gradle task is inherited from the Spring Boot framework, upon which Grails is based. Of course create-react-app projects don’t have such a task by default. The React profile provides the client:bootRun task as a wrapper around the npm/yarn start script. This allows you to use advanced Gradle features like running both server and client in parallel mode with one command. For developers, running ../gradlew client:bootRun is the same as running npm start (or yarn start) in a stock create-react-app project, and in fact you can run the client app exactly that way if you have npm/yarn installed on your machine.

Once the gradlew commands have completed downloading dependencies and launching their respective apps, you should be able to browse to http://localhost:8080 to see the Grails backend application, and http://localhost:3000 to view the React app.

Data source setup - postgresql



GORM - Grails Object Relational Management

http://gorm.grails.org/6.1.x/hibernate/manual/#quickStartGuide


A domain class can be created with the create-domain-class command if you are using Grails, or if you are not using Grails you can just create the .groovy file manually:

grails create-domain-class helloworld.Person

This will create a class at the location grails-app/domain/helloworld/Person.groovy such as the one below:

package helloworld

class Person {
}

If you have the configured the dataSource.dbCreate property and set it to "update", "create" or "create-drop", GORM will automatically generate/modify the database tables for you.

You can customize the class by adding properties:

class Person {
    String name
    Integer age
    Date lastVisit
}

Once you have a domain class try and manipulate it with console command in Grails by typing:

grails console

This loads an interactive GUI where you can run Groovy commands with access to the Spring ApplicationContext, GORM, etc.

Or if you are not using Grails here is a unit test template (using Spock) that can be run to test out the examples:

import spock.lang.*
import grails.gorm.annotation.Entity
import grails.transaction.Rollback
import org.grails.orm.hibernate.HibernateDatastore
import org.springframework.transaction.PlatformTransactionManager

class ExampleSpec extends Specification {

    @Shared @AutoCleanup HibernateDatastore hibernateDatastore
    @Shared PlatformTransactionManager transactionManager

    void setupSpec() {
       hibernateDatastore = new HibernateDatastore(Person)
       transactionManager = hibernateDatastore.getTransactionManager()
    }

    @Rollback
    void "test execute GORM standalone in a unit test"() {
       // your logic here
    }
}

@Entity
class Person {
    ...
}

3.1. Basic CRUD

Try performing some basic CRUD (Create/Read/Update/Delete) operations.

3.1.1. Create

To create a domain class use Map constructor to set its properties and call the save() method:

def p = new Person(name: "Fred", age: 40, lastVisit: new Date())
p.save()

The save() method will persist your class to the database using the underlying Hibernate ORM layer.

The save() method is defined by the GormEntity trait.

3.1.2. Read

GORM transparently adds an implicit id property to your domain class which you can use for retrieval:

def p = Person.get(1)
assert 1 == p.id

This uses the static get(id) method that expects a database identifier to read the Person object back from the database.

You can also load an object in a read-only state by using the read(id) method:

def p = Person.read(1)

In this case the underlying Hibernate engine will not do any dirty checking and the object will not be persisted. Note that if you explicitly call the save() method then the object is placed back into a read-write state.

In addition, you can also load a proxy for an instance by using the load(id) method:

def p = Person.load(1)

This incurs no database access until a method other than getId() is called. Hibernate then initializes the proxied instance, or throws an exception if no record is found for the specified id.

3.1.3. Update

To update an instance, change some properties and then call save() again:

def p = Person.get(1)
p.name = "Bob"
p.save()

3.1.4. Delete

To delete an instance use the delete() method:

def p = Person.get(1)
p.delete()



GORM DB Reverse Engineering Plugin - no longer supported

https://grails-plugins.github.io/grails-db-reverse-engineer/grails3v4/index.html

Burt Beckwith

Grails DB reverse engineering plugin does not work after Grails 3.1 - 2017

https://groups.google.com/forum/#!topic/grails-dev-discuss/CNmd8BRuf28

DB Reverse Tutorial

https://grails-plugins.github.io/grails-db-reverse-engineer/grails3v4/index.html#tutorial

last version of  DB reverse engineering plugin

db-reverse-engineer-4.0.0-manual.pdf

early version of DB reverse engineering manual with books authors output classes

grails-db-reverse-engineering-plugin-v51-manual.pdf


Books and authors tables

books authors tables
create table author (   id bigint not  auto_increment  null,   version bigint not null,   name varchar(255) not null,  email varchar(255) null,  primary key (id)) ENGINE=InnoDB;

create table book (   id bigint not  auto_increment  null,   version bigint not null,   title varchar(255) not null,  type varchar(255), description varchar(2048),  sold bigint null,  primary key (id)) ENGINE=InnoDB;

create table author_books (   author_id bigint not null,   book_id bigint not null,   primary key (author_id, book_id)) ENGINE=InnoDB;

alter table author_books add index FKauthor_books_2_books (book_id), add constraint FKauthor_books_2_books foreign key (book_id) references book (id);

alter table author_books add index FKauthor_books_2_authors (author_id), add constraint FKauthor_books_2_authors foreign key (author_id) references author(id);



Books and authors output classes from DB plugin

books and authors classes
class Author { 
String name
String email
String phone
static hasMany = [books: Book]
};


class Book { 
String title
String publisher
BigDecimal price
String description 
Integer quantitySold
static hasMany = [authors: Author]
static belongsTo = Author
};


class Author_Books { 
String name
String email
static hasMany = [books: Book]
};



Final version of Books and authors output classes





Example of DB Reverse Engineering Output


Yes GORM. The reverse engineering for Grails is a little simpler because all that is needed are Domain classes and the framework handles all the hibernate mappings. So for example a DB table called Application would only need a single Domain class instead of a Domain, DAO and XML Mapping file. Here is an example with one-to-many and many-to-one relationships