Grails Goodness: Using Wrapper for Running Grails Commands Without Grails Installation
Since Grails 2.1 we can create a Grails wrapper. The wrapper allows developer to run Grails commands in a project without installing Grails first. The wrapper concept is also available in other projects from the Groovy ecosystem like Gradle or Griffon. A wrapper is a shell script for Windows, OSX or Linux named grailsw.bat
or grailsw
and a couple of JAR files to automatically download a specific version of Grails. We can check in the shell scripts and supporting files into a version control system and make it part of the project. Developers working on the project simply check out the code and execute the shell script. If there is no Grails installation available then it will be downloaded. To create the shell scripts and supporting files someone on the project must run the wrapper command for the first time. This developer must have a valid Grails installation. The files that are generated can then be added to version control and from then one developers can use the grailsw
or grailsw.bat
shell scripts.
$ grails wrapper
| Wrapper installed successfully
$
In the root of the project we have two new files grailsw
and grailsw.bat
. Windows users can uss grailsw.bat
and on other operating systems we use grailsw
. Also a new directory wrapper
is created with three files:
grails-wrapper-runtime-2.2.0.jar
grails-wrapper.properties
springloaded-core-1.1.1.jar
When we run the grailsw
or grailsw.bat
scripts for the first time we see how Grails is downloaded and installed into the $USER_HOME/.grails/wrapper
directory. The following output shows that the file is downloaded and extracted when we didn’t run the grailsw
script before:
$ ./grailsw --version
Downloading http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/grails-2.2.0.zip to /Users/mrhaki/.grails/wrapper/grails-2.2.0-download.zip
.....................................................................................
................................................................
Extracting /Users/mrhaki/.grails/wrapper/grails-2.2.0-download.zip to /Users/mrhaki/.grails/wrapper/2.2.0
Grails version: 2.2.0
When we want to use a new version of Grails one of the developers needs to run to run $ grails upgrade
followed by $ grails wrapper
with the new Grails version. Notice this developer needs to have a locally installed Grails installation of the version we want to create a wrapper for. The newly generated files can be checked in to version control and all developers on the project will have the new Grails version when they run the grails
or grailsw.bat
shell scripts.
$ ./grailsw --version
Downloading http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/grails-2.2.1.zip to /Users/mrhaki/.grails/wrapper/grails-2.2.1-download.zip
.....................................................................................
...
................................................................
Extracting /Users/mrhaki/.grails/wrapper/grails-2.2.1-download.zip to /Users/mrhaki/.grails/wrapper/2.2.1
Grails version: 2.2.1
We can change the download location of Grails to for example a company intranet URL. In the wrapper/
directory we see the file grails-wrapper.properties
. The file has one property wrapper.dist.url
, which by default refers to http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/
. We can change this to another URL, add the change to version control so other developers will get the change automatically. And when the grailsw
shell script is executed the download location will be another URL. To set a different download URL when generating the wrapper we can use the command-line option –distributionUrl:
$ grails wrapper --distributionUrl=http://company.intranet/downloads/grails-releases/
If we don’t like the default name for the directory to store the supporting files we can use the command-line option –wrapperDir. The files are then stored in the given directory and the grailsw
and grailsw.bat
shell scripts will contain the given directory name. Written with Grails 2.2.0 and 2.2.1 Original article