Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninja!
Do you know what application profiles are in Grails? Have you ever heard how we create a profile in Grails? If not, then don't worry; we will help you.
In this article, we’ll discuss application profiles in grails. We will also discuss how we can create them and how to publish them. But before that, let’s look at grails in brief. Grails make creating web applications in Java easier. It helps in reducing the complexity of making a web application in Java. Grails achieve this by using already existing technologies such as Hibernate and Spring. Let us understand what applications profiles are in Grails.
Application Profiles in Grails
Application Profiles in Grails can help you to use the profile's commands, templates, and plugins. Thus specifying a correct profile is very important. The default profile when we create an app in Grails. To create an application in Grails, we use the command
grails create-app app_name
To change the profile, we specify the profile explicitly. For example,
$ grails create-app app_name --profile=web-plugin
The above command will create an app named 'app_name,' and the profile for the app will be web-plugin.
We have a command to list all the application profiles in Grails. The command is
$ grails list-profiles
For information about application profiles in Grails, the command is
$ grails profile-info web-plugin
To use your profile with some defaults, we can specify the profile in the settings .groovy file such as
After the above configuration, we have our profile ready for publishing in the local repository.
Now, to install the profile, we need to type the following command:
$ gradle install
To use the profile we need to type the following command:
$ grails create-app myapp --profile Coding Ninjas Studio
Now let us understand what profile inheritance is.
Profile Inheritance
Profile inheritance means that you can extend other parent profiles. Also, inheritance allows CLI commands and features from the parent profile to be inherited.
To inherit a profile, we need to modify the build.gradle of the profile. In the build.gradle file, we need to add the profile dependencies.
The above line will extend the base profile. Running an inherited profile with the create-app command will copy the parent's skeleton first. Then application.yml, build.gradle, and dependencies will going to be merged. Let us now understand what profiles are.
Understanding Profiles
The profile contains a file profile.yml file. It also includes three other directories, 'commands,' 'templates,' and 'skeleton.' profile.yml describes the profile. It also controls how the build for the profile is configured.
Creating an app using the create-app command copies the parent's skeleton to a new project. After this, the build.gradle is generated.
We came across the profile.yml file a lot. Let us now understand it a bit better.
Let us see what all is there in the file:
✍🏼repositories: It contains all the maven repos that the 'generated build' has to hold. Example-
✍🏼skeleton.parent.target: It is the directory where the parent’s skeleton is copied.
✍🏼skeleton.binaryExtensions: It defines the extensions which we want as binary files.
✍🏼skeleton.executable: This defines the file patterns we want as executable in the application.
✍🏼instructions: This is the information that the user sees after the application is complete.
Now, let us understand what profile commands are.
Profile Commands
We can have our commands specific to our profile. We define the commands in the YAML file. The command can have one or multiple steps. Every single step we define is itself a command.
Let us see what type of steps we can define:
🥷🏼render - This command helps in rendering a template to a specific location.
🥷🏼mkdir - It makes a directory according to the location parameter.
🥷🏼execute - It executes a command that is specified by the class parameter.
🥷🏼gradle - It helps accomplish gradle tasks specified by task parameter.
Profile Features
Profile feature contains templates and dependencies. We can create a profile with mutiple features, and other child profiles can inherit the features.
We use the following command to create a feature:
$ grails create-feature feature_name
Publishing Profiles
Now, let us understand how we can publish the profiles. We can do this in two ways:
Publishing Profiles to Grails Central Repository
We can publish a profile using the plugin grails-profile-publish. First, we need to upload the source to GitHub. After uploading the source to GitHub, we need to register an account on Bintray.
So basically, Bintray is a download hub with support for RPM and Debian packages and REST automation. Android libraries are frequently distributed via Maven or/and jCenter repositories.
After registering the account, we need to configure some keys in the build.gradle file.
After this, we can execute the $ gradle publishProfile command to publish the profile.
Publishing Profiles to an Internal Repository
Publishing profiles to an internal repository is more accessible than publishing them in the central repository. Because in this you don't need to sign in/ sign up on Bintray. We need to specify the repository in the build.gradle and execute the $ gradle publish command.
Here’s what we need to specify in the build.gradle file:
What is the default profile when we create an app in grails?
The default application profile in Grails is the web.
Name some application profiles in grails.
The following application profiles in Grails: base, plugin, rest-api, web, web-plugin.
Does profile inheritance allow CLI commands and features from the parent to be inherited?
Yes, profile inheritance allows CLI commands and features from the parent profile to be inherited.
Where can we publish an application profile in Grails?
We can publish an application profile in Grails either in the Grails Central Repository or an Internal Repository.
Conclusion
In this article, we saw what are Application Profiles in Grails. We saw how we create a profile, what profile inheritance is, and how we can publish a profile. We also covered how we make profile commands and features. If you want to explore Grails then you can check out our other blogs: