Tuesday, July 17, 2012

Create Liferay Hook using Maven

Hi Friends,

This blog will show you how you can create Liferay Hook using Maven. As nowadays more people are moving  to Maven instead of Ant. It has many good features like Repeatable builds / Dependency Management, Versioned artifacts, Convention and configuration over scripting, IDE plugins support.

Now before moving further there are some prerequisite that you must have.

Prerequisite
  • Maven (You can go through my another blog for setting up Maven in your local system)
  • Liferay 

    Create Hook

    • Move to the folder where you want to create your hook plugins (i.e. D:\Liferay\Liferay61\source\maven\hook)
    • Go to that path from Command Prompt and execute mvn archetype:generate
    • A list of archetype will come up. Find liferay-hook-archetype from the list and check the number for the same archetype.  
    Note :- Number is not fixed for each archetype. It might differ based on the how many archetype you have. So make sure you are using correct number for your archetype after verifying from the list.
    • In next step, you need to select Liferay Version. So select version accordingly (i.e. 6.1.0)
    • Next, define proper groupId (i.e. com.opensourceforlife.liferay)
    • Define proper artifactId (i.e. myportlet)
    • Specify version (i.e. 1.0-SNAPSHOT)
    • If you want to specify different package then groupId then specify it here otherwise it will take same value of groupId (i.e. com.opensourceforlife.liferay)
    • Confirm all the values and hit 'Y' for confirmation.

    • Your done !!! Your Hook is created.

    Package and Deploy
    • Just go to your created hook and open pom.xml. It would look like as below
    <?xml version="1.0"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.opensourceforlife.liferay</groupId>
        <artifactId>myportlet</artifactId>
        <packaging>war</packaging>
        <name>myportlet Hook</name>
        <version>1.0-SNAPSHOT</version>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.liferay.maven.plugins</groupId>
                    <artifactId>liferay-maven-plugin</artifactId>
                    <version>${liferay.version}</version>
                    <configuration>
                        <autoDeployDir>${liferay.auto.deploy.dir}</autoDeployDir>
                        <liferayVersion>${liferay.version}</liferayVersion>
                        <pluginType>hook</pluginType>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <source>1.5</source>
                        <target>1.5</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>com.liferay.portal</groupId>
                <artifactId>portal-service</artifactId>
                <version>${liferay.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.liferay.portal</groupId>
                <artifactId>util-java</artifactId>
                <version>${liferay.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.portlet</groupId>
                <artifactId>portlet-api</artifactId>
                <version>2.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.4</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.0</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </project> 
    
    •  Now we need to add some other properties in it to able to make it capable of deploy it in our Liferay instance. Add below properties after </dependencies> tag. 
    • <liferay.auto.deploy.dir> will hold the deploy folder path where war file will be deployed.
    • <liferay.version> will load the specific version of Liferay artifacts.
    ....
    </dependencies>
    <properties>
            <liferay.auto.deploy.dir>D:/Liferay/Liferay61/runtimes/liferay-portal-6.1.10/deploy</liferay.auto.deploy.dir>
            <liferay.version>6.1.0</liferay.version>
        </properties>
    </project>
    • Now to compile the hook just execute mvn clean package
    • And to build & deploy war file execute mvn clean liferay:deploy
    • Your war is copied to your deploy folder and ready to be picked by Hot deploy listener.
    Revert back for any suggestions or issues. Will get back to you ASAP.

    Regards,
    Tejas