默认情况下,Liferay是用的Ant来完成编译,打包部署的,你可以轻易的在SDK中找到相应的Ant脚本,当然了, 我们也可以用Maven来完成这个目标:
首先,我们必须添加liferay相关的maven原型(archetype),在Windows->Preference->Maven->Archetype中:
这样我们的Maven创建向导中就有了 Liferay相关的原型:
我们现在用Maven向导来创建
我们选择liferay-portlet-archetype:
创建完了之后的portlet应该有如下的目录结构:
我们编辑pom.xml ,添加2个属性,一个是liferay.version,一个是liferay.auto.deploy.dir,
最终的pom.xml是这样的:
- <?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.charles.liferay</groupId>
- <artifactId>charles-portlet</artifactId>
- <packaging>war</packaging>
- <name>charles-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.version}</version>
- <configuration>
- <autoDeployDir>${liferay.auto.deploy.dir}</autoDeployDir>
- <liferayVersion>${liferay.version}</liferayVersion>
- <pluginType>portlet</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-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>
- <properties>
- <liferay.version>6.1.0</liferay.version>
- <liferay.auto.deploy.dir>D:\Liferay\RI\liferay-portal-6.1.0-ce-ga1\deploy</liferay.auto.deploy.dir>
- </properties>
- </project>
编辑我们的liferay应用,比如我们需要显示一些内容,所以更改view.jsp:
- <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
- <portlet:defineObjects />
- This is the <b>charles-portlet</b>.
- This is the portlet created by charles using maven.
我们创建一个Maven配置来编译,打包,部署之,如下图:
最终控制台正式编译,打包,部署:
- ...
- [DEBUG] Configuring mojo com.liferay.maven.plugins:liferay-maven-plugin:6.1.0:deploy from plugin realm ClassRealm[plugin>com.liferay.maven.plugins:liferay-maven-plugin:6.1.0, parent: sun.misc.Launcher$AppClassLoader@11b86e7]
- [DEBUG] Configuring mojo 'com.liferay.maven.plugins:liferay-maven-plugin:6.1.0:deploy' with basic configurator -->
- [DEBUG] (f) autoDeployDir = D:\Liferay\RI\liferay-portal-6.1.0-ce-ga1\deploy
- [DEBUG] (f) warFile = D:\Charles\Eclipse_Workspace\charles-portlet\target\charles-portlet-0.0.1-SNAPSHOT.war
- [DEBUG] (f) warFileName = charles-portlet-0.0.1-SNAPSHOT.war
- [DEBUG] -- end configuration --
- [INFO] Deploying charles-portlet-0.0.1-SNAPSHOT.war to D:\Liferay\RI\liferay-portal-6.1.0-ce-ga1\deploy
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESS
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 1:18.604s
- [INFO] Finished at: Fri Jun 01 12:50:47 CST 2012
- [INFO] Final Memory: 5M/12M
- [INFO] ------------------------------------------------------------------------
看出被打包的portlet叫 charles-portlet-0.0.1-SNAPSHOT.war
并且部署到了LIFERAY_HOME下deploy目录中
我们启动Liferay,和预期一样
本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/884674,如需转载请自行联系原作者