Liferay Service builder is one of the important tool in Liferay portal development. As most of the developers are aware of the uses of service builder using plugin-sdk. but using service builder with maven is also straight forward. only you need to understand the project structure for service project. All the file generation of the service and service.xml will remain same. only the steps for creating the plugin project will be different from plugin-sdk method.
These are the steps for creating the service project using maven.
Step 1. create a new maven project using new maven project wizard.
Step 2. At the time of archetype selection select the liferay-servicebuilder-archetype and your appropriate version. (In this example i am using 6.2.0-RC5).
Click on Next button.
(Again if you are not finding the liferay-servicebuilder-archetype archetype in your list then re-index your archetype or follow this article to download archetype in m2e plugin.)
Step 3. Provide the group id, artifact id and package details for your project. and click on finish button.
It will create your maven project with liferay service capability. if you see the screen shot below you will find three maven project is created. where the main project is test-service-portlet and this project has two child project modules test-service-portlet-portlet and test-service-portlet-portlet-service. there are three pom.xml file is created.
Now we have to provide the liferay version number in pom.xml file. if we give the version information in parent project pom.xml file then it will be shared to both the project. lets add the below properties in parent pom.xml.
<properties>
<liferay.version>6.2.0-RC5</liferay.version>
<liferay.auto.deploy.dir>D:/Personal work/sandbox/liferay-portal-6.2.0-ce-ga1/deploy</liferay.auto.deploy.dir>
<liferay.maven.plugin.version>6.2.0-RC5</liferay.maven.plugin.version>
<liferay.app.server.lib.global.dir>D:/Personal work/sandbox/liferay-portal-6.2.0-ce-ga1/tomcat-7.0.42/lib/ext</liferay.app.server.lib.global.dir>
</properties>
then your test-service-portlet parent pom.xml file looks like 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.test</groupId>
<artifactId>test-service-portlet</artifactId>
<packaging>pom</packaging>
<name>test-service-portlet</name>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.liferay.maven.plugins</groupId>
<artifactId>liferay-maven-plugin</artifactId>
<version>${liferay.maven.plugin.version}</version>
<configuration>
<autoDeployDir>${liferay.auto.deploy.dir}</autoDeployDir>
<appServerDeployDir>${liferay.app.server.deploy.dir}</appServerDeployDir>
<appServerLibGlobalDir>${liferay.app.server.lib.global.dir}</appServerLibGlobalDir>
<appServerPortalDir>${liferay.app.server.portal.dir}</appServerPortalDir>
<liferayVersion>${liferay.version}</liferayVersion>
<pluginName>test-service-portlet-portlet</pluginName>
<pluginType>portlet</pluginType>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<modules>
<module>test-service-portlet-portlet</module>
<module>test-service-portlet-portlet-service</module>
</modules>
<properties>
<liferay.version>6.2.0-RC5</liferay.version>
<liferay.auto.deploy.dir>D:/Personal work/sandbox/liferay-portal-6.2.0-ce-ga1/deploy</liferay.auto.deploy.dir>
<liferay.maven.plugin.version>6.2.0-RC5</liferay.maven.plugin.version>
<liferay.app.server.lib.global.dir>D:/Personal work/sandbox/liferay-portal-6.2.0-ce-ga1/tomcat-7.0.42/lib/ext</liferay.app.server.lib.global.dir>
</properties>
</project>
and test-service-portlet-portlet pom.xml looks like 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">
<parent>
<groupId>com.test</groupId>
<artifactId>test-service-portlet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-service-portlet-portlet</artifactId>
<packaging>war</packaging>
<name>test-service-portlet Portlet</name>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.liferay.maven.plugins</groupId>
<artifactId>liferay-maven-plugin</artifactId>
<version>${liferay.maven.plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>build-css</goal>
</goals>
</execution>
</executions>
<configuration>
<apiBaseDir>${basedir}/../test-service-portlet-portlet-service</apiBaseDir>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>test-service-portlet-portlet-service</artifactId>
<version>${pom.version}</version>
</dependency>
<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-bridges</artifactId>
<version>${liferay.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>util-taglib</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>
and service project test-service-portlet-portlet-service pom.xml looks like 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">
<parent>
<groupId>com.test</groupId>
<artifactId>test-service-portlet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-service-portlet-portlet-service</artifactId>
<packaging>jar</packaging>
<name>test-service-portlet Portlet Service</name>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.liferay.maven.plugins</groupId>
<artifactId>liferay-maven-plugin</artifactId>
<version>${liferay.maven.plugin.version}</version>
<configuration>
<webappBaseDir>${basedir}/../test-service-portlet-portlet</webappBaseDir>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>portal-service</artifactId>
<version>${liferay.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Now pom.xml is ready to build the service.
Step 4. Now we need to configure the service.xml which can be found under test-service-portlet-portlet module.
you will find the sample Foo service Entity is already available. update the namespace in service.xml because the default namespace is not valid see the service xml below. you can also create your own entity in this service.xml.
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<service-builder package-path="com.test">
<namespace>chandan</namespace>
<entity name="Foo" uuid="true" local-service="true" remote-service="true">
<!-- PK fields -->
<column name="fooId" type="long" primary="true" />
<!-- Group instance -->
<column name="groupId" type="long" />
<!-- 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" />
<!-- Other fields -->
<column name="field1" type="String" />
<column name="field2" type="boolean" />
<column name="field3" type="int" />
<column name="field4" type="Date" />
<column name="field5" type="String" />
<!-- Order -->
<order by="asc">
<order-column name="field1" />
</order>
<!-- Finder methods -->
<finder name="Field2" return-type="Collection">
<finder-column name="field2" />
</finder>
<!-- References -->
<reference package-path="com.liferay.portlet.asset" entity="AssetEntry" />
<reference package-path="com.liferay.portlet.asset" entity="AssetTag" />
</entity>
</service-builder>
Step 5. Building the service, for building the service we need to run maven goal liferay:build-service, for this Right click on the parent project (test-service-portlet) and select Run As–> Maven build. or you can build the service from the module also which has service.xml file. provide the goal and click on run button.
Step 6. Refresh all the projects. your all the interfaces will be generated in test-service-portlet-portlet-service module. and all the implementation classes and configuration files will be generated in test-service-portlet-portlet module. like below screen shot.
Step 7. Now the time for building the war file. Right click on the parent project and select Run As –> Maven install. if we do the maven install from parent project then both the child modules will be build. The test-service-portlet-portlet-service module will build the jar file which will be added to the war file as a dependency. the war file will be created in the test-service-portlet-portlet module. you can see the war file and jar file in the respective target folder.
Step 8. deploy the war file to the server or use maven goal to deploy the war file file directly to the server.
Thanks
The post Liferay service builder project with Maven appeared first on CodingLoading.