When you use Blade CLI to create a project, your project’s folder structure,
build script (e.g.,
build.gradle), Java classes, and other resources (such as
JSPs) are created based on the chosen template. In this tutorial, you’ll learn
how to use Blade CLI to create modules based on pre-existing templates and
Using Blade CLI gives you the flexibility to choose how you want to create your application. You can do so in your own standalone environment, or within a Liferay Workspace. You can also create a project using either the Gradle or Maven build tool. Creating Liferay modules in a workspace using Blade CLI is very similar to creating them in a standalone environment.
When creating projects in a workspace, you should navigate to the appropriate
folder corresponding to that type of project (e.g., the
/modules folder for a
module project). You can also provide further directory nesting into that
folder, if preferred. For example, the Gradle workspace, by default, sets the
directory where your modules should be stored by setting the following property
in the workspace’s
Change this property if you’d like to store your modules in a different directory.
First, you’ll learn how to create a module using a template.
To create a new Liferay project, you can run the Blade
create command, which
offers many available templates. There are, however, many other options you can
specify to help mold your project just the way you want it. To learn how to use
create command and the many options it provides, enter
blade help create into a terminal. A list of the
create options are listed below:
-b, --build <string>: The build type of the project. Available options are
-c, --classname <string>: If a class is generated in the project, provide the name of the class to be generated. If not provided, the class name defaults to the project name.
C, --contributorType <string>: Identifies your module as a theme contributor. This is also used to add the
Web-ContextPathbundle headers to the BND file.
-d, --dir <file>: The directory to create the new project.
-h, --hostbundlebsn <string>: If a new JSP hook fragment needs to be created, provide the name of the host bundle symbolic name.
-H, --hostbundleversion <string>: If a new JSP hook fragment needs to be created, provide the name of the host bundle version.
-l, --listtemplates: Prints a list of available project templates.
-p, --packagename <string>: The package name to use when creating the project.
-s, --service <string>: If a new Declarative Services (DS) component needs to be created, provide the name of the service to be implemented. Note that in this context, the term service refers to an OSGi service, not to a Liferay API.
-t, --template <string>: The project template to use when creating the project. Run
blade create -lfor a listing of available Blade CLI templates.
-v, --liferay-version: The Liferay DXP version to target when creating a project (e.g.,
To create a module project, use the following syntax:
blade create [OPTIONS] <NAME>
For example, if you wanted to create an MVC portlet project with Gradle, you could execute the following:
blade create -t mvc-portlet -p com.liferay.docs.guestbook -c GuestbookPortlet my-guestbook-project
This command creates an MVC portlet project based on the template
It uses the package name
com.liferay.docs.guestbook and creates the portlet
GuestbookPortlet. The project name is
my-guestbook-project. Since the
directory was not specified, it is created in the folder you executed the
command. When generating a project using Blade CLI, there is no downloading,
which means internet access is not required.
Blade CLI can also create the same project with Maven by specifying the
-b maven parameter. Using Blade CLI’s Maven option isn’t the only way leverage
Liferay’s Maven project templates; you can also generate them using Maven
archetypes. See Liferay’s
articles to see how.
When using Blade CLI, you’ll have to manually edit your project’s component class. Blade CLI gives you the ability to specify the class’s name, but all other contents of the class can only be edited after the class is created. See the Creating Modules with Liferay Developer Studio tutorial for further details and important dependency information on component classes.
Now that you know the basics on creating Liferay projects using
you can visit the
reference section to view specific
create templates and how they work.
Next, you’ll explore Liferay’s provided project samples and how to generate them using Blade CLI.
Liferay provides many sample projects that are useful for those interested in learning best practices on structuring their projects to accomplish specific tasks in Liferay DXP. These samples can be found in the liferay-blade-samples Github repository. You can also learn more about these samples by visiting the Liferay Sample Projects article.
You can generate these samples using Blade CLI for convenience, instead of cloning the repository and manually copy/pasting them to your environment. To do this, use the following syntax:
blade samples <NAME>
For example, if you wanted to generate the portlet-ds sample, you could execute
blade samples ds-portlet
For a full listing of all the available Blade samples, run
Awesome! Now you know the basics on creating Liferay projects with Blade CLI.