6 Application Profiles - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.1.4
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
grails create-app myapp --profile=rest-api$ grails list-profiles
$ grails profile-info rest-apiProfile Repositories
By default Grails will resolve profiles from the Grails central repository. However, you can define additional profile repositories in theUSER_HOME/grails/settings.groovy file:grails {
profiles {
repositories {
myRepo {
url = "http://foo.com/repo"
snapshotsEnabled = true
}
}
}
}Note that Grails uses Aether to resolve profiles, as a Gradle instance is not yet available when thecreate-appcommand is executed. This means that you can also define repositories and more advanced configuration (proxies, authentication etc.) in yourUSER_HOME/.m2/settings.xmlfile if you wish.
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
$ cd mycompany
$ grails
| Enter a command name to run. Use TAB for completion:
grails>create-command create-creator-command create-feature create-generator-command create-gradle-command create-templatecreate-command- creates a new command that will be available from the Grails CLI when the profile is usedcreate-creator-command- creates a command available to the CLI that renders a template (Example: create-controller)create-generator-command- creates a command available to the CLI that renders a template based on a domain class (Example: generate-controller)create-feature- creates a feature that can be used with this profilecreate-gradle-command- creates a CLI command that can invoke gradlecreate-template- creates a template that can be rendered by a command
profile.yml.Below is an example profile.yml file:features:
defaults:
- hibernate
- asset-pipeline
build:
plugins:
- org.grails.grails-web
excludes:
- org.grails.grails-core
dependencies:
compile:
- "org.mycompany:myplugin:1.0.1"gradle install:$ gradle install
create-app command:$ grails create-app myapp --profile mycompany
$ 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 thebuild.gradle of a profile and define the profile dependences. For example typically you want to extend the base profile:dependencies {
runtime project(':base')
}- When the create-app command is executed the parent profile's skeleton is copied first
- Dependencies and
build.gradleis merged from the parent(s) - The
application.ymlfile is merged from the parent(s) - CLI commands from the parent profile are inherited
- Features from the parent profile are inherited
dependencies {
runtime project(':plugin')
runtime project(':web')
}6.3 Publishing Profiles
Publishing Profiles to the Grails Central Repository
Any profile created with the create-profile command already comes configured with agrails-profile-publish plugin defined in build.gradle:apply plugin: "org.grails.grails-profile-publish"build.gradle file:grailsPublish {
user = 'YOUR USERNAME'
key = 'YOUR KEY'
githubSlug = 'your-repo/your-profile'
license = 'Apache-2.0'
}TheWith this in place you can rungithubSlugargument should point to the path to your Github repository. For example if your repository is located athttps://github.com/foo/barthen yourgithubSlugisfoo/bar
gradle publishProfile to publish your profile:$ gradle publishProfile
Publishing Profiles to an Internal Repository
The aforementionedgrails-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:publishing {
repositories {
maven {
credentials {
username "foo"
password "bar"
} url "http://foo.com/repo"
}
}
}gradle publish:$ gradle publish
6.4 Understanding Profiles
A profile is a simple directory that contains aprofile.yml file and directories containing the "commands", "skeleton" and "templates" defined by the profile. Example:web
* commands
* create-controller.yml
* run-app.groovy
…
* features
* asset-pipeline
* skeleton
* feature.yml
* skeleton
* grails-app
* controllers
…
* build.gradle
* templates
* artifacts
* Controller.groovy
* profile.ymlprofile.yml file is used to describe the profile and control how a the build is configured.Understanding the profile.yml descriptor
Theprofile.yml can contain the following child elements.1) repositories
A list of Maven repositories to include in the generated build. Example:repositories:
- "https://repo.grails.org/grails/core"2) build.repositories
A list of Maven repositories to include in the buildscript section of the generated build. Example:build:
repositories:
- "https://repo.grails.org/grails/core"3) build.plugins
A list of Gradle plugins to configure in the generated build. Example:build:
plugins:
- eclipse
- idea
- org.grails.grails-core4) build.excludes
A list of Gradle plugins to exclude from being inherited from the parent profile:build:
excludes:
- org.grails.grails-core5) dependencies
A map of scopes and dependencies to configure. Theexcludes scope can be used to exclude from the parent profile. Example:dependencies:
excludes:
- "org.grails:hibernate"
build:
- "org.grails:grails-gradle-plugin:$grailsVersion"
compile:
- "org.springframework.boot:spring-boot-starter-logging"
- "org.springframework.boot:spring-boot-autoconfigure"6) features.defaults
A default list of features to use if no explicit features are specified.features:
defaults:
- hibernate
- asset-pipelineWhat happens when a profile is used?
When thecreate-app command runs it takes the skeleton of the parent profiles and copies the skeletons into a new project structure.The build.gradle file is generated as is result of obtaining all of the dependency information defined in the profile.yml files and produces the required dependencies.The command will also merge any build.gradle files defined within a profile and its parent profiles.The grails-app/conf/application.yml file is also merged into a a single YAML file taking into account the profile and all of the parent profiles.
6.5 Creating Profile Commands
A profile can define new commands that apply only to that profile using YAML or Groovy scripts. Below is an example of the create-controller command defined in YAML:description:
- Creates a controller
- usage: 'create-controller [controller name]'
- completer: org.grails.cli.interactive.completers.DomainClassCompleter
- argument: "Controller Name"
description: "The name of the controller"
steps:
- command: render
template: templates/artifacts/Controller.groovy
destination: grails-app/controllers/artifact.package.path/artifact.nameController.groovy
- command: render
template: templates/testing/Controller.groovy
destination: src/test/groovy/artifact.package.path/artifact.nameControllerSpec.groovy
- command: mkdir
location: grails-app/views/artifact.propertyNamerender- To render a template to a given destination (as seen in the previous example)mkdir- To make a directory specified by thelocationparameterexecute- To execute a command specified by theclassparameter. Must be a class that implements the Command interface.gradle- To execute one or many Gradle tasks specified by thetasksparameter.
description: Creates a WAR file for deployment to a container (like Tomcat)
minArguments: 0
usage: |
war
steps:
- command: gradle
tasks:
- wardescription( "Creates a Grails script" ) { usage "grails create-script [SCRIPT NAME]" argument name:'Script Name', description:"The name of the script to create" flag name:'force', description:"Whether to overwrite existing files" }def scriptName = args[0] def model = model(scriptName) def overwrite = flag('force') ? true : falserender template: template('artifacts/Script.groovy'), destination: file("src/main/scripts/${model.lowerCaseName}.groovy"), model: model, overwrite: overwrite
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 thecreate-feature command from the root directory of your profile:$ grails create-feature myfeature
myfeature/feature.yml file that looks like the following:description: Description of the feature
# customize versions here
# dependencies:
# compile:
# - "org.grails.plugins:myplugin2:1.0"
#feature.yml file from the "asset-pipeline" feature:description: Adds Asset Pipeline to a Grails project
build:
plugins:
- asset-pipeline
dependencies:
build:
- 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
runtime:
- "org.grails.plugins:asset-pipeline"FEATURE_DIR
* feature.yml
* skeleton
* grails-app
* conf
* application.yml
* build.gradleapplication.yml and build.gradle get merged with their respective counterparts in the profile by used.With the feature.yml you can define additional dependencies. This allows users to create applications with optional features. For example:$ grails create-app myapp --profile myprofile --features myfeature,hibernate
