Wednesday, 19 June 2013

Liferay with Maven : Service builder

Introduction:-
After deploying a simple portlet, next requirement is to have a support from Maven to use one of the strongest features of Liferay i.e to create DAO layer through the service Builder

Prerequisites:-
Create a simple portlet with maven  (http://michi-path.blogspot.in/2013/06/liferay-with-maven.html)

How To:-

1. Create a service.xml
Sample:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="me.pd.test">
    <author>priyanka</author>
    <namespace>fe</namespace>

    <entity name="Foo" local-service="true" remote-service="true">

        <!-- PK fields -->

        <column name="fooId" type="long" primary="true" />

        <!-- Audit fields -->

        <column name="companyId" type="long" />
        <column name="userId" type="long" />
        <column name="userName" type="String" />
        <column name="createDate" type="Date" />
        <column name="modifiedDate" type="Date" />

        <!-- Finder methods -->

        <finder name="Field2" return-type="Collection">
            <finder-column name="field2" />
        </finder>
    </entity>
</service-builder>


2. In the pom.xml follow the following structure
<?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>me.pd.test.portlet</groupId>
    <artifactId>MyDemo</artifactId>
    <packaging>war</packaging>
    <name>MyDemo Portlet</name>
    <version>1.1-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>com.liferay.maven.plugins</groupId>
                <artifactId>liferay-maven-plugin</artifactId>
                <version>6.1.1</version>

                <configuration>
                    <apiDir>/home/priyanka/workspace/MyDemo/src/main/java</apiDir>
                    <autoDeployDir>\home\priyanka\pfiles\bundles\deploy</autoDeployDir>
                    <appServerDeployDir>\home\priyanka\pfiles\bundles\tomcat-7.0.27\webapps</appServerDeployDir>
                    <appServerLibGlobalDir>\home\priyanka\pfiles\bundles\tomcat-7.0.27\lib\ext</appServerLibGlobalDir>
                    <appServerPortalDir>\home\priyanka\pfiles\bundles\tomcat-7.0.27\webapps\ROOT</appServerPortalDir>

                    <liferayVersion>6.1.1</liferayVersion>
                    <pluginType>portlet</pluginType>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>

                <groupId>org.codehaus.mojo</groupId>

                <artifactId>build-helper-maven-plugin</artifactId>

                <version>1.5</version>

                <executions>

                    <execution>

                        <id>add-source</id>

                        <phase>generate-sources</phase>

                        <goals>

                            <goal>add-source</goal>

                        </goals>

                        <configuration>

                            <sources>

                                <source>${service.api.dir}</source>

                            </sources>

                        </configuration>

                    </execution>

                </executions>


            </plugin>
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-resources-plugin</artifactId>

                <version>2.4</version>

            </plugin>

        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>service.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.jackrabbit</groupId>
            <artifactId>jackrabbit-core</artifactId>
            <version>2.1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>jcl-over-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.tika</groupId>
            <artifactId>tika-core</artifactId>
            <version>0.9</version>
        </dependency>
        <dependency>

            <groupId>com.liferay.portal</groupId>
            <artifactId>portal-service</artifactId>
            <version>6.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.liferay.portal</groupId>
            <artifactId>util-bridges</artifactId>
            <version>6.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.liferay.portal</groupId>
            <artifactId>util-taglib</artifactId>
            <version>6.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.liferay.portal</groupId>
            <artifactId>util-java</artifactId>
            <version>6.1.1</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>

    <properties>
        <service.api.dir>src/main/java-service-api</service.api.dir>
    </properties>

</project>


Note:- The Tags in bold formatting are the alterations done to the pom.xml 

3. From the command Prompt or Terminal Window(Linux)
Go to the respective project and type:-
mvn clean liferay:build-service

DAO layer is created :)
WOW, its amazing, it was easy...right???
(References:-
1. http://liferaylive.blogspot.in/p/blog-page_3.html
2. http://www.liferay.com/web/mika.koivisto/blog/-/blogs/getting-started-with-liferay-maven-sdk)

1 comment:

  1. If you face any issues. try going for this
    http://michi-path.blogspot.in/2013/06/liferay-maven-service-builder-version-1.html

    ReplyDelete