---
title: "搭建方便的Lift开发环境"
author: "spreadscala"
date: "2010-06-25T23:59:30Z"
tags: []
categories: ["企业架构"]
slug: "lift"
draft: false
lastmod: "2010-06-25T23:59:30Z"
id: 699644
url: "https://spreadscala.iteye.com/blog/699644"
---

本文将介绍在windows中如何使用Intellij Idea 9.02 + Scala Maven plugin + jetty + JRebel打造一个便捷的Lift开发环境。

首先安装Intellij idea 的开源社区版本，目前的最新版本是9.02. 开源版本自带有功能强大的maven插件，这对于开发使用maven创建和管理的Lift工程特别方便。关于如何在Intellij上安装scala插件，请参考我的博文：[Scala学习笔记_1 搭建开发环境（Intellij IDEA + scala插件）](http://spreadscala.iteye.com/blog/550118)。

接下来[下载](http://maven.apache.org/download.html)并安装Maven，并把maven的bin目录加入“Path”环境变量中，以便可以在命令行中直接调用。

使用Maven创建一个Lift工程。打开cmd，并转到某个目录下，运行maven的archetype:generate命令。

**注：以下命令不换行**

```

mvn archetype:generate -U -DarchetypeGroupId=net.liftweb -DarchetypeArtifactId=lift-archetype-basic -DarchetypeVersion=1.1-SNAPSHOT -DremoteRepositories=http://scala-tools.org/repo-snapshots -DgroupId=demo.helloworld -DartifactId=helloworld -Dversion=1.1-SNAPSHOT

```


以上命令将创建一个基于lift 1.1-snapshot的lift工程（-DarchetypeVersion=1.1-SNAPSHOT），工程具有基本的登录，注册，菜单等功能（-DarchetypeArtifactId=lift-archetype-basic，如果archetypeArtifactId=lift-archetype-blank，则是一个空工程），工程的顶级目录名是helloworld（-DartifactId=helloworld）。

如果你是第一次运行这个命令，可能需要花费较长的时间，因为Maven要下载工程所需的依赖。在命令运行过程中，向导会请求确认当前的工程配置，直接按键即可。

![](http://dl.iteye.com/upload/attachment/269053/11490f3c-4380-3030-9628-049821d960e8.png)

项目创建成功后，可以在当前目录看到一个名为“helloworld”的文件夹，这就是生成的Lift工程，它也是一个Maven工程，我们可以在Intellij idea中导入它。

运行Intellij idea，选择“File --> New Project...”，在弹出的对话框中，选中“import project from external module”，点击“next”按钮，选择“Maven”模块，点击“next”，在“Root Directory”项中填入helloworld工程的路径，其它选项默认。点击“next”按钮，在新的对话框中会列出待导入的工程，点击“next”，然后点击“finish”完成设置。Intellij idea将导入工程并自动下载工程所需的依赖。
helloworld工程结构图：
![](http://dl.iteye.com/upload/attachment/269059/1a55adb9-97ac-3c11-b73a-4cc7d26feaa7.png)

接着运行helloworld工程。在左边的工程目录树中，右键点击“helloworld”工程节点，在弹出的菜单中选择“Run helloword ”，Intellij idea将编译工程并在jetty中运行helloworld工程。待启动完毕后，打开浏览器并访问：<http://localhost:8080>，可看到界面效果如下：

![](http://dl.iteye.com/upload/attachment/269062/734e6d8d-c942-3657-9f47-ef560de1edf9.png)

你可以注册一个自己的用户名并登陆，Enjoy it!

到这里，我们已经完美的创建并运行了一个简单的Lift工程，但是我们非常怀念使用java开发web工程时的热发布功能，我们只需修改代码，并且刷新浏览器，就可以马上看到改变，无需重启服务器或者应用。希望在开发Lift工程时拥有这般享受？那就请出大名鼎鼎的JRebel吧。

首先告诉大家一个好消息：JRebel对于所有scala开发者是免费的。（而对于java开发者则是收费的）“零周转”公司（ZeroTurnaround，JReble的拥有者）的团队是scala的粉丝，他们在一年前宣布JRebel面向所有scala开发者提供免费的JRebel授权，这种行为被Lift的创始人David Pollark称为“奇迹般的馈赠”。[访问这个页面](http://www.zeroturnaround.com/scala-license/)并填写申请的表单便可以获得一个具有一年授权的License。

安装JRebel。[下载解压版的JRebel](http://www.zeroturnaround.com/jrebel/current/)，并将刚才获取的license文件放入JRebel的解压目录，具体操作请参考收到的license授权邮件。

为了在intellij idea中使用JRebel，需要安装JRebel插件。通过intellij idea的插件管理器安装，并在“File --> Settings --> JRebel”中设置jrebel.jar所在的位置：

![](http://dl.iteye.com/upload/attachment/269065/82fc4b75-cc6d-30f7-9bd7-0ab7be2229e4.png)

安装完成之后，可以看到在工具栏中新增了两个按钮：和。

接下来我们就可以使用jrebel运行helloworld工程。首先在工程的pom.xml中加入一个插件：

```xml

<plugin>
  <groupId>org.zeroturnaround</groupId>
  <artifactId>javarebel-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>generate-rebel-xml</id>
      <phase>process-resources</phase>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
</plugin>

```

这个插件会自动往工程中添加rebel.xml，里面默认配置了jrebel监视的目录。关于rebel.xml的配置，请参考jrebel的文档。然后禁用jetty的自动扫描功能：

```xml

      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <configuration>
          <contextPath>/</contextPath>
          <scanIntervalSeconds>5</scanIntervalSeconds>  -- 改为 0,禁用自动装载
        </configuration>
      </plugin>

```


右键点击“helloworld”节点，在弹出的菜单中选择“Run with JRebel helloworld ”。在启动过程中，我们将看到如下输出：

JRebel: Directory 'E:\liftprojects\helloworld\src\main\resources' will be monitored for changes.
JRebel: Directory 'E:\liftprojects\helloworld\target\classes' will be monitored for changes.
JRebel: Directory 'E:\liftprojects\helloworld\src\main\webapp' will be monitored for changes.

接下来就让我们体验一下jrebel的神奇。打开"HelloWorld.scala",修改以下函数：

```java

  def howdy(in: NodeSeq): NodeSeq =
    Helpers.bind("b", in, "time" -> date.map(d => Text(d.toString + " and enjoy yourself!")))

```


保存并且编译这个文件，刷新页面，就可以马上看到更改，无需重新装载应用或者重启服务器。同时可以看到控制台的输出：

JRebel: Reloading class 'demo.helloworld.snippet.HelloWorld'.
JRebel: Reloading class 'demo.helloworld.snippet.HelloWorld$$anonfun$howdy$1'.

至此，我们搭建了一个完美的Lift开发环境，接下来要做的就是好好享受Lift的优雅带来的编码乐趣了。


**讨论和比较:**

事实上不需要JRebel也可以实现热部署，在pom.xml中，把scanIntervalSeconds设置为非0的数值，jetty就会每间隔设定时间扫描一次类以及pom.xml的变化，如果监测到有改变，则重新打包并部署。下面是使用jetty自导的热部署时的一个输出：

 Starting scanner at interval of 5 seconds.
......
INFO - Service request (GET) / took 78 Milliseconds
 restarting org.mortbay.jetty.plugin.Jetty6PluginWebAppContext@94b318{/,E:\liftprojects\helloworld\src\main\webapp}
 Webapp source directory = E:\liftprojects\helloworld\src\main\webapp
 Reload Mechanic: automatic
 Classes = E:\liftprojects\helloworld\target\classes
 Context path = /
 Tmp directory =  determined at runtime
 Web defaults = org/mortbay/jetty/webapp/webdefault.xml
 Web overrides =  none
 web.xml file = E:\liftprojects\helloworld\src\main\webapp\WEB-INF\web.xml
 Webapp directory = E:\liftprojects\helloworld\src\main\webapp
2010-06-25 22:37:32.531:INFO::No Transaction manager found - if your webapp requires one, please configure one.
 Restart completed at Fri Jun 25 22:37:35 CST 2010
可以看到类改变时，整个helloworld应用被重新加载。如果应用很大，重启的时间是很客观的，而且有可能导致内存泄露。而使用JRebel，则只需要重新加载改变过的类，无需加载应用，所以极力推荐使用JRebel.

**参考资料：**

Lift wiki教程：<http://www.assembla.com/wiki/show/liftweb/Using_Maven>