6 Application Profiles

Table of Contents

6 Application Profiles

When you create a Grails application with the create-app command by default the "web" profile is used:

grails create-app myapp

You can specify a different profile with the profile argument:

grails create-app myapp --profile=rest-api

Profiles encapsulate the project commands, templates and plugins that are designed to work for a given profile. The source for the profiles can be found on Github, whilst the profiles themselves are published as JAR files to the Grails central repository.

To find out what profiles are available use the list-profiles command:

$ grails list-profiles

For more information on a particular profile use the profile-info command:

$ grails profile-info rest-api

Profile Repositories

By default Grails will resolve profiles from the Grails central repository. However, you can override what repositories will be searched by specifying repositories in the USER_HOME/.grails/settings.groovy file.

If you want profiles to be resolved with a custom repository in addition to the Grails central repository, you must specify Grails central in the file as well:

Grails uses Aether to resolve profiles, as a Gradle instance is not yet available when the create-app command is executed. This means that you can also define repositories and more advanced configuration (proxies, authentication etc.) in your USER_HOME/.m2/settings.xml file if you wish.

It is also possible to store simple credentials for profile repositories directly in the USER_HOME/.grails/settings.groovy file.

With the default values specified, the command to create an application using that profile becomes:

$ grails create-app myapp --profile=myprofile

6.1 Creating Profiles

The idea behind creating a new profile is that you can setup a default set of commands and plugins that are tailored to a particular technology or organisation.

To create a new profile you can use the create-profile command which will create a new empty profile that extends the base profile:

$ grails create-profile mycompany

The above command will create a new profile in the "mycompany" directory where the command is executed. If you start interactive mode within the directory you will get a set of commands for creating profiles:

With the above configuration in place you can publish the profile to your local repository with gradle install:

$ gradle install

Your profile is now usable with the create-app command:

$ grails create-app myapp --profile mycompany

With the above command the application will be created with the "mycompany" profile which includes an additional dependency on the "myplugin" plugin and also includes the "hibernate" and "asset-pipeline" features (more on features later).

Note that if you customize the dependency coordinates of the profile (group, version etc.) then you may need to use the fully qualified coordinates to create an application:

$ grails create-app myapp --profile com.mycompany:mycompany:1.0.1

6.2 Profile Inheritance

One profile can extend one or many different parent profiles. To define profile inheritance you can modify the build.gradle of a profile and define the profile dependences. For example typically you want to extend the base profile:

dependencies {
runtime project(':base')
}

By inheriting from a parent profile you get the following benefits:

When the create-app command is executed the parent profile’s skeleton is copied first

Dependencies and build.gradle is merged from the parent(s)

The application.yml file is merged from the parent(s)

CLI commands from the parent profile are inherited

Features from the parent profile are inherited

To define the order of inheritance ensure that your dependencies are declared in the correct order. For example:

dependencies {
runtime project(':plugin')
runtime project(':web')
}

In the above snippet the skeleton from the "plugin" profile is copied first, followed by the "web" profile. In addition, the "web" profile overrides commands from the "plugin" profile, whilst if the dependency order was reversed the "plugin" profile would override the "web" profile.

6.3 Publishing Profiles

Publishing Profiles to the Grails Central Repository

Any profile created with the create-profile command already comes configured with a grails-profile-publish plugin defined in build.gradle:

apply plugin: "org.grails.grails-profile-publish"

To publish a profile using this plugin to the Grails central repository first upload the source to Github (closed source profiles will not be accepted). Then register for an account on Bintray and configure your keys as follows in the profile’s build.gradle file:

The githubSlug argument should point to the path to your Github repository. For example if your repository is located at https://github.com/foo/bar then your githubSlug is foo/bar

With this in place you can run gradle publishProfile to publish your profile:

$ gradle publishProfile

The profile will be uploaded to Bintray. You can then go the Grails profiles repository and request to have your profile included by clicking "Include My Package" button on Bintray’s interface (you must be logged in to see this).

Publishing Profiles to an Internal Repository

The aforementioned grails-profile-publish plugin configures Gradle’s Maven Publish plugin. In order to publish to an internal repository all you need to do is define the repository in build.gradle. For example:

Commands defined in YAML must define one or many steps. Each step is a command in itself. The available step types are:

render - To render a template to a given destination (as seen in the previous example)

mkdir - To make a directory specified by the location parameter

execute - To execute a command specified by the class parameter. Must be a class that implements the Command interface.

gradle - To execute one or many Gradle tasks specified by the tasks parameter.

For example to invoke a Gradle task, you can define the following YAML:

description: Creates a WAR file for deployment to a container (like Tomcat)minArguments: 0usage: |
warsteps:
- command: gradletasks:
- war

If you need more flexiblity than what the declarative YAML approach provides you can create Groovy script commands. Each Command script is extends from the GroovyScriptCommmand class and hence has all of the methods of that class available to it.

The following is an example of the create-script command written in Groovy:

For more information on creating CLI commands see the section on creating custom scripts in the Command Line section of the user guide.

6.6 Creating Profile Features

A Profile feature is a shareable set of templates and dependencies that may span multiple profiles. Typically you create a base profile that has multiple features and child profiles that inherit from the parent and hence can use the features available from the parent.

To create a feature use the create-feature command from the root directory of your profile:

$ grails create-feature myfeature

This will create a myfeature/feature.yml file that looks like the following:

Plug-ins

Services

Servlet API

Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.