`
spreadscala
  • 浏览: 88807 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

搭建方便的Lift开发环境

    博客分类:
  • Lift
阅读更多
本文将介绍在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插件)

接下来下载并安装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要下载工程所需的依赖。在命令运行过程中,向导会请求确认当前的工程配置,直接按[Enter]键即可。



项目创建成功后,可以在当前目录看到一个名为“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工程结构图:


接着运行helloworld工程。在左边的工程目录树中,右键点击“helloworld”工程节点,在弹出的菜单中选择“Run helloword [jetty:run]”,Intellij idea将编译工程并在jetty中运行helloworld工程。待启动完毕后,打开浏览器并访问:http://localhost:8080,可看到界面效果如下:



你可以注册一个自己的用户名并登陆,Enjoy it!

到这里,我们已经完美的创建并运行了一个简单的Lift工程,但是我们非常怀念使用java开发web工程时的热发布功能,我们只需修改代码,并且刷新浏览器,就可以马上看到改变,无需重启服务器或者应用。希望在开发Lift工程时拥有这般享受?那就请出大名鼎鼎的JRebel吧。

首先告诉大家一个好消息:JRebel对于所有scala开发者是免费的。(而对于java开发者则是收费的)“零周转”公司(ZeroTurnaround,JReble的拥有者)的团队是scala的粉丝,他们在一年前宣布JRebel面向所有scala开发者提供免费的JRebel授权,这种行为被Lift的创始人David Pollark称为“奇迹般的馈赠”。访问这个页面并填写申请的表单便可以获得一个具有一年授权的License。

安装JRebel。下载解压版的JRebel,并将刚才获取的license文件放入JRebel的解压目录,具体操作请参考收到的license授权邮件。

为了在intellij idea中使用JRebel,需要安装JRebel插件。通过intellij idea的插件管理器安装,并在“File --> Settings --> JRebel”中设置jrebel.jar所在的位置:



安装完成之后,可以看到在工具栏中新增了两个按钮:[run with jrebel]和[debug with jrebel]。

接下来我们就可以使用jrebel运行helloworld工程。首先在工程的pom.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的自动扫描功能:
      <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 [jetty:run]”。在启动过程中,我们将看到如下输出:

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",修改以下函数:
  def howdy(in: NodeSeq): NodeSeq =
    Helpers.bind("b", in, "time" -> date.map(d => Text(d.toString + " and enjoy yourself!")))


保存并且编译这个文件[Ctrl + Shift + F9],刷新页面,就可以马上看到更改,无需重新装载应用或者重启服务器。同时可以看到控制台的输出:

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自导的热部署时的一个输出:

[INFO] Starting scanner at interval of 5 seconds.
......
INFO - Service request (GET) / took 78 Milliseconds
[INFO] restarting org.mortbay.jetty.plugin.Jetty6PluginWebAppContext@94b318{/,E:\liftprojects\helloworld\src\main\webapp}
[INFO] Webapp source directory = E:\liftprojects\helloworld\src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = E:\liftprojects\helloworld\target\classes
[INFO] Context path = /
[INFO] Tmp directory =  determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = E:\liftprojects\helloworld\src\main\webapp\WEB-INF\web.xml
[INFO] 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.
[INFO] Restart completed at Fri Jun 25 22:37:35 CST 2010
可以看到类改变时,整个helloworld应用被重新加载。如果应用很大,重启的时间是很客观的,而且有可能导致内存泄露。而使用JRebel,则只需要重新加载改变过的类,无需加载应用,所以极力推荐使用JRebel.

参考资料:
  1. Lift wiki教程:http://www.assembla.com/wiki/show/liftweb/Using_Maven
  • 描述: lift工程目录
  • 大小: 39.8 KB
  • 大小: 16.1 KB
  • 描述: 运行结果
  • 大小: 51 KB
  • 描述: jrebel插件设置
  • 大小: 37.9 KB
0
0
分享到:
评论
1 楼 jamesqiu 2010-09-07  
IDEA编译Scala太慢,基本不可用;我觉得sbt或者eclipse都更快,可以试试。

相关推荐

Global site tag (gtag.js) - Google Analytics