Monday, 17 April 2017

Document of liferay with gradle

Spring MVC Portlet in Liferay 7 with gradle
Liferay is an open platform in an ecosystem of open platforms. Just because Liferay has its own MVC framework, therefore, doesn’t mean you have to use it. It is perfectly valid to bring the tools and experience you have from other development projects over to Liferay. In fact, we expect you to. Liferay’s development platform is standards-based, making it an ideal choice for applications of almost any type.
If you’re already a wizard with Spring MVC, you can use it instead of Liferay’s MVCPortlet with no limitations whatsoever. Since Spring MVC replaces only your application’s web application layer, you can still use Service Builder for your service layer.
So what does it take to implement a Spring MVC application in Liferay? Start by considering how to package a Spring MVC application for Liferay Portal CE 7.0.

Steps to create a portlet:

1)          Create Liferay module project:

                    Go to fileàNewàLiferay module project.

In the Project Dialog we need provide project name, Display Name, Build Type,project template name.once we have done all click on Next As soon as you click on finish it will create Liferay MVC portlet.






SpringMvcGradlePortlet.java

 


View.jsp

<%@includefile="/META-INF/resources/init.jsp"%>

<h1>hello spring this is spring mvcgradle</h1>

 

Screenshot of view jsp

 

 

 

 

 

Once portlet is created you can see Portlet Project in Package Explored i.e. in left side of Eclipse IDE and Portlet related Gradle build file.
After Creating portlet need to add spring related jars.Right click on project build pathàconfigure build pathàlibraryàadd external jarsàgotoliferay-ce-portalàtomcatàwebappsàrootàweb-infàlib add the jars.



Now go to the project and create ; under srcàmain,underwebapp folder create Spring-context folder à create SpringMvcGradle-Portlet.xml,Portlet-Aplication-context.xml.

SpringMvcGradle-Portlet.xml


<?xmlversion="1.0"?>

<beans
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
> 
      <context:component-scanbase-package="com.spring.gradle.portlet" />
     
</beans>


 

 

 

Portlet-Aplication-context.xml.


<?xmlversion="1.0"?>

<beans
     xmlns="http://www.springframework.org/schema/beans"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
> 
     <context:annotation-config />

     <beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <propertyname="contentType"value="text/html;charset=UTF-8" />
           <propertyname="prefix"value="/WEB-INF/jsp/" />
           <propertyname="suffix"value=".jsp" />
          
          
           <!-- <property name="prefix" value="/META-INF/resources/" />
            <property name="suffix" value=".jsp" /> -->
          
           <propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView" />
     </bean>
</beans>




Under WEB-INF folder need to create liferay -display.xml,liferay-plugin-package.properties,liferay-portlet.xml,portlet.xml,web.xml files.


liferay -display.xml

<?xmlversion="1.0"?>
<!DOCTYPEdisplayPUBLIC"-//Liferay//DTD Display 7.0.0//EN""http://www.liferay.com/dtd/liferay-display_7_0_0.dtd">

<display>
     <categoryname="category.sample">
           <portletid="SpringMvcGradle"/>
     </category>
</display>

liferay-plugin-package.properties

 

author=Liferay,Inc.
change-log=
licenses=LGPL
liferay-versions=7.0.0+
long-description=
module-group-id=liferay
module-incremental-version=1
name=SpringMVC
page-url=http://www.liferay.com
short-description=
tags=



liferay-portlet.xml

 

<?xmlversion="1.0"?>
<!DOCTYPEliferay-portlet-appPUBLIC"-//Liferay//DTD Portlet Application 7.0.0//EN""http://www.liferay.com/dtd/liferay-portlet-app_7_0_0.dtd">

<liferay-portlet-app>
      <portlet>
             <portlet-name>SpringMvcGradle</portlet-name>
             <icon>/icon.png</icon>
             <requires-namespaced-parameters>false</requires-namespaced-parameters>
             <header-portlet-css>/css/main.css</header-portlet-css>
             <footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
             <css-class-wrapper>SpringMvcGradle-portlet</css-class-wrapper>
      </portlet>
      <role-mapper>
             <role-name>administrator</role-name>
             <role-link>Administrator</role-link>
      </role-mapper>
      <role-mapper>
             <role-name>guest</role-name>
             <role-link>Guest</role-link>
      </role-mapper>
      <role-mapper>
             <role-name>power-user</role-name>
             <role-link>Power User</role-link>
      </role-mapper>
      <role-mapper>
             <role-name>user</role-name>
             <role-link>User</role-link>
      </role-mapper>
</liferay-portlet-app>



portlet.xml

<?xmlversion="1.0"?>

<portlet-appxmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"version="2.0">
      <portlet>
             <portlet-name>SpringMvcGradle</portlet-name>
             <display-name>Spring MVC</display-name>
             <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
             <init-param>
                    <name>contextConfigLocation</name>
                    <value>/WEB-INF/spring-context/portlet/SpringMvcGradle-portlet.xml</value>
             </init-param>
             <expiration-cache>0</expiration-cache>
             <supports>
                    <mime-type>text/html</mime-type>
             </supports>
             <resource-bundle>content/Language</resource-bundle>
             <portlet-info>
                    <title>Spring MVC</title>
                    <short-title>Spring MVC</short-title>
                    <keywords>Spring MVC</keywords>
             </portlet-info>
             <security-role-ref>
                    <role-name>administrator</role-name>
             </security-role-ref>
             <security-role-ref>
                    <role-name>guest</role-name>
             </security-role-ref>
             <security-role-ref>
                    <role-name>power-user</role-name>
             </security-role-ref>
             <security-role-ref>
                    <role-name>user</role-name>
             </security-role-ref>
      </portlet>
</portlet-app>

web.xml

 

<?xmlversion="1.0"?>

<web-app
    version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
> 
    <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>/WEB-INF/spring-context/portlet-application-context.xml</param-value>
    </context-param>
    <servlet>
           <servlet-name>ViewRendererServlet</servlet-name>
           <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
           <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
           <servlet-name>ViewRendererServlet</servlet-name>
           <url-pattern>/WEB-INF/servlet/view</url-pattern>
    </servlet-mapping>
    <listener>
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

Gradle Build

Gradle is a popular open source build automation system. You can take full advantage of Gradle in Liferay IDE by utilizing Buildship, which is a collection of Eclipse plugin-ins that provide support for building software using Gradle with Liferay IDE. Buildship is bundled with Liferay IDE versions 3.0 and higher.

Now need to add the Spring dependencies and war creation plugin.

 

Build.gradle



buildscript {

           repositories {
           mavenLocal()
           maven {
                  url "https://cdn.lfrs.sl/repository.liferay.com/nexus/content/groups/public"
           }
     }

    
     dependencies {
           classpath group: "com.liferay", name: "com.liferay.gradle.plugins.css.builder", version: "latest.release"
           classpath group: "com.liferay", name: "com.liferay.css.builder", version: "latest.release"
     }

  }

apply plugin: 'war'

war {
     dependsOnbuildCSS

     exclude('**/*.scss')

     filesMatching("**/.sass-cache/") {
           it.path = it.path.replace(".sass-cache/", "")
     }

     includeEmptyDirs = false
}

dependencies {
     compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
     compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
     compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
     compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
     compileOnly group: "jstl", name: "jstl", version: "1.2"
     compileOnly group: "org.osgi", name: "osgi.cmpn", version: "6.0.0"
    
    
     compile group: 'aopalliance', name: 'aopalliance', version: '1.0'
     compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
     compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.2'

    compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.2'
    compile group: 'commons-io', name: 'commons-io', version: '1.2'
   
    compile group: 'org.springframework', name: 'spring-aop', version: '4.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-beans', version: '4.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-context', version: '4.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-core', version: '4.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-expression', version: '4.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc-portlet', version: '4.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.1.9.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version: '4.1.9.RELEASE'
}



Now right click on build.gradleàgradleàrefresh gradle project and deploy.
Refresh the workspace and go to the bundles--.osgiàmodules here jar file is created copy that jar under liferay-ce-portalàosgiàwar folder and it will auto deployed.

Location of jar and war file in project directory

 



keep in mind that if you change the Gradle build scripts inside your Gradle projects (e.g., build.gradle or settings.gradle), you must refresh the project so Liferay IDE can account for the change and display it properly in your views. To refresh a Gradle project, right-click on the project and select Gradle  Refresh Gradle Project.







Portlet Anatomy/Directory structure

 

 

 

Database interaction with Service Trackers


Someone wise once said, “With increased modularity comes increased dynamism.” Okay, nobody ever said that, but it’s true. Now that Liferay is promoting more modular plugins deployed into an OSGi runtime, you have to consider how your own code, living in its own module, can rely on services in other modules for functionality. You need to account for the possibility of service implementations being swapped out or removed entirely if your module is to survive and thrive in the environment of OSGi. It’s easy for Liferay Portal CE 7.0 developers who need to call services from their @Component classes. They just use another Declarative Services annotation, @Reference, to get a service reference.

IMPLEMENTING A SERVICE TRACKER



No comments:

Post a Comment