Creating a Hook
Regardless of whether you use Liferay IDE or your terminal to create hooks, hooks projects are stored in the Plugins SDK’s
Using Liferay IDE:
Go to File → New → Liferay Project.
Assign a project name and display name. To demonstrate, we’ll use example-hook and Example for the project name and display name, respectively. Notice that upon entering example-hook as the project name, the wizard conveniently inserts Example in grayed-out text as the plugin’s default display name. The wizard derives the default display name from the project name, starts it in upper-case, and leaves off the plugin type suffix Hook because the plugin type is automatically appended to the display name in Liferay Portal. The IDE saves the you from repetitively appending the plugin type to the display name; in fact, the IDE ignores any plugin type suffix if you happen to append it to the display name.
Enter the following values for the project name and display name:
- Project name: example-hook
- Display name: Example
Select the build type, Plugins SDK, and Liferay runtime.
If you select the Maven build type, you’ll be prompted to enter an artifact version, group ID, and active profile for your project. See Developing Plugins Using Maven for more information. Otherwise, select the Ant build type, a Plugins SDK and a Liferay runtime.
For this demonstration, make the following selections:
- Build type: Ant
- Plugins SDK:
[a configured Plugins SDK]
- Liferay runtime:
[a configured Liferay runtime]
For more information, see sections Setting Up the Liferay Plugins SDK and Liferay Portal Runtime and Server Setup from
Developing Apps with Liferay IDE
Select the Hook Plugin Type.
Figure 11.1 shows the values you specified for the hook plugin.
Figure 11.1: Creating a hook plugin is easy with Liferay IDE. Just click File → New → Liferay Project, enter a project name and display name, select a build type, Plugins SDK, and Liferay Runtime, select Hook, and then click Finish.
The Plugins SDK automatically names the hook by appending “-hook” to the project name. With Liferay IDE, you can create a hook in a completely new plugin project or create a hook in an existing plugin project. Use File → New → Liferay Project to create a new plugin project and File → New → Liferay Hook to create a hook in an existing plugin project.
Using the terminal: Navigate to your Plugins SDK directory in a terminal and enter the appropriate command for your operating system:
In Linux and Mac OS X, enter
./create.sh example "Example"
In Windows, enter
create.bat example "Example"
A BUILD SUCCESSFUL message from Ant tells you there’s a new folder named
example-hook inside the Plugins SDK’s
hooks folder. The Plugins SDK automatically named the hook by appending “-hook” to the project name.
Now that you’ve created a hook, let’s go ahead and deploy it.
Deploying the Hook
Using Liferay IDE: Click and drag your hook project onto your server. Upon deployment, your server displays messages indicating that your hook was read, registered and is now available for use.
Reading plugin package for example-hook
Registering hook for example-hook
Hook for example-hook is available for use
Voila! Your hook deployed.
Note: If the Liferay server prints the following message to your console, the Marketplace Portlet and Portal Compatibility Hook must not already be deployed on your server.
Plugin example-hook requires marketplace-portlet, portal-compat-hook
For Liferay 6.2.0 CE GA1, you can fork and clone Liferay’s liferay-plugins project from GitHub, checkout the respective branch and/or tag, and deploy each plugin. You can install the Plugins SDK in Liferay IDE and import each plugin and deploy them. Here is information on each of the plugins:
- Marketplace Portlet (
marketplace-portlet) - is available at
- Portal Compatibility Hook (
portal-compat-hook) - is available at
If you ever need to redeploy your plugin while in Liferay IDE, right-click your plugin’s icon located underneath your server and select Redeploy.
Using the terminal: Open a terminal window in your
hooks/example-hook directory and enter
A BUILD SUCCESSFUL message indicates your hook is now being deployed. If you switch to the terminal window running Liferay, in a few seconds you should see the message “Hook for example-hook is available for use”.
Note: When we created portlets and themes, they were fully functional upon deployment. Hooks aren’t like that because they’re Liferay customizations. The default customization is the original implementation!
Anatomy of the Hook
To make your hook useful, you need to customize something in Liferay. You begin by mirroring the structure of Liferay’s code that you plan to customize. A hook plugin is built to contain this:
In Liferay IDE’s Package Explorer, here’s what the hook structure looks like:
Figure 11.2: In Liferay IDE, the folder structure of a newly created hook plugin looks like this.
The particular files you’ll work on depend on the Liferay features you’re overriding with your hook. We’ll start by making one of the most common hook plugin customizations: a customization of Liferay’s web resources.