前言

笔者把多年UBuntu上用Emacs和Maven开发Java应用(包括TestNG和Web application)的技巧总结出来,希望对有志于Linux开发的朋友有帮助。我并不反对用IDE开发,但是很多时候,Emacs能够带来更快的速度(同时对机器硬件要求也很小),对Java工程的更透彻了解,更容易和开源领域的工程师交流。我还不怎么喜欢用鼠标,Emacs让我能够解放我的右手。

安装

1.确保你已经安装了JDK
2.确保你安装了Maven
3.安装Emacs:sudo apt-get install emacs
4.下载jdee-bin-2.4.0.1.zip和依赖文件elib-1.0.zip,地址在这里:http://sourceforge.net/projects/jdee/files/5.将两个文件解压到/opt/emacs_plugins/目录下面
6.还需要下载ecb-2.40和ecb.el,也放在emacs_plugins目录下
7.下载javadoc-help的javadoc-help.el文件放在/opt/emacs_plugins/java目录下面 http://javadochelp.sourceforge.net/

配置.emacs

指定jdk

'(jde-jdk-registry (quote (("1.6.0_29" . "/usr/jdk1.6.0_29"))))



配置JDEE

'(jde-jdk-registry (quote (("1.6.0_29" . "/usr/jdk1.6.0_29"))))

;;JDEE configuration
(add-to-list 'load-path (expand-file-name "/opt/emacs_plugins/jdee-2.4.0.1/lisp"))
(load-file "/opt/emacs_plugins/cedet-1.0pre7/common/cedet.el")
(add-to-list 'load-path (expand-file-name "/opt/emacs_plugins/elib-1.0"))
(add-to-list 'load-path (expand-file-name "/opt/emacs_plugins/ecb-2.40"))
;;(add-to-list 'load-path (expand-file-name "/opt/emacs_plugins/jde-jalopy"))

(load-file (expand-file-name "/opt/emacs_plugins/ecb-2.40/ecb.el"))

(require 'font-lock)
(require 'ecb)
(require 'ecb-autoloads)
(require 'jde)

(custom-set-variables
 '(browse-url-browser-function (quote browse-url-generic))
 '(browse-url-generic-program "chromium-browser")
 '(column-number-mode t)

 '(jde-complete-function (quote jde-complete-menu))
 '(jde-debugger (quote ("jdb")))
 '(jde-jdk-registry (quote (("1.6.0_29" . "/usr/jdk1.6.0_29"))))
 '(jde-resolve-relative-paths-p t)
 '(jde-run-option-debug (quote ("Server" "Socket" "javadebug" nil "5005" t))))



查看API文档


可惜的是javadoc-helper不能检索jdk文档。自己在浏览器里面保留一个书签代替吧。

;; Java document support
(add-to-list 'load-path (expand-file-name "/opt/emacs_plugins/java"))
(require 'javadoc-help)
(javadoc-set-predefined-urls '("/opt/java_docs/common-cache-1.0.0-javadoc"
                               "/opt/java_docs/commons-codec-1.4-javadoc"
                               "/opt/java_docs/commons-logging-1.1.1-javadoc"
                               "/opt/java_docs/ehcache-server-1.0.0-javadoc"
                               "/opt/java_docs/gson-1.7.1-javadoc"
                               "/opt/java_docs/jcommander-1.12-javadoc"
                               "/opt/java_docs/jersey-apache-client-1.9.1-javadoc"
                               "/opt/java_docs/jersey-client-1.9.1-javadoc"
                               "/opt/java_docs/jersey-core-1.9.1-javadoc"
                               "/opt/java_docs/joda-time-1.6.2-javadoc"
                               "/opt/java_docs/junit-4.8.2-javadoc"
                               "/opt/java_docs/logback-classic-1.0.0-javadoc"
                               "/opt/java_docs/logback-core-1.0.0-javadoc"
                               "/opt/java_docs/lombok-0.10.4-javadoc"
                               "/opt/java_docs/mongo-java-driver-2.7.2-javadoc"
                               "/opt/java_docs/mybatis-3.0.5-javadoc"
                               "/opt/java_docs/slf4j-api-1.6.4-javadoc"
                               "/opt/java_docs/snakeyaml-1.6-javadoc"
                               "/opt/java_docs/testng-6.1.1-javadoc"))

(global-set-key [(f1)]          'javadoc-lookup)  ; F1 to lookup
(global-set-key [(shift f1)]    'javadoc-help)    ; Shift-F1 to bring up menu

注意,上面的文档目录都是自己从互联网上下载的,如果使用maven构建项目,可以很方便的用一个脚本文件将文件jar包下载下来,并且复制到/opt/java_docs/目录下。

mvn clean
mvn dependency:resolve -Dclassifier=javadoc
rm -rf /opt/java_docs
mkdir /opt/java_docs
cp $(find ~/.m2/repository/ -name *javadoc.jar) /opt/java_docs/


下面这个脚本可以帮助我把所有jar文件解压到同名目录下。

#!bin/sh
path=/opt/java_docs
for f in $(ls $path/*.jar);
do
    name=`basename $f`
    name2=`echo $name | sed 's/\.jar//g'`
    echo $name2
    mkdir $path/$name2
    cd $path/$name2
    jar xvf $path/$name
done

rm -f *.jar

这个脚本可以生成需要配置的目录路径,用在.emacs中。

path=/opt/java_docs
for f in $(ls $path/);
do
    name=`basename $f`

    echo \"/opt/java_docs/$name\"
done


配置工程文件

为每一个Maven工程目录里面创建一个prj.el文件。下面的是一个例子。/opt/jdk_src的jdk源代码文件是从jdk目录的src.zip解压而来。
jde-global-classpath里面列出了本项目使用的jar包。注意是绝对路径。
'(jde-db-option-connect-socket (quote (nil "5005"))) 指的是远程调试的连接端口,后面调试一节再细讲。
'jde-sourcepath 里面指定了源代码路径
手动配置这些东西的确有点麻烦,将来可以学点lisp编程,在Emacs里面自动产生这个文件。先凑合着用吧。


(jde-project-file-version "1.0")
(jde-set-variables
 '(jde-db-source-directoiries "/opt/jdk_src")
 '(jde-global-classpath (quote ("./target/classes"
                                "./target/test-classes"
                                "/home/chenshu/.m2/repository/joda-time/joda-time/1.6.2/joda-time-1.6.2.jar"
                                "/home/chenshu/.m2/repository/com/sun/jersey/jersey-client/1.9.1/jersey-client-1.9.1.jar"
                                "/home/chenshu/.m2/repository/org/testng/testng/6.0.1/testng-6.0.1.jar")))
 '(jde-db-option-connect-socket (quote (nil "5005")))
 '(jde-sourcepath (quote ("./src/main/java/com/freebird/business"
                          "./src/test/java/com/freebird/test"
                           ))))

Windows7 下的配置


最后附一个windows7的配置,当时整了好久,早算cedet不报错,勉强能用。

1. (setq debug-on-error t) 
2. (global-set-key [f5] (lambda() (interactive) (revert-buffer t t))) 
3. (setq c-basic-offset 4) 
4. (show-paren-mode) 
5. 
6. ;;JDEE configuration 
7. 
8. (add-to-list 'load-path (expand-file-name "c:/emacs_plugins/jdee-2.4.0.1/lisp")) 
9. (add-to-list 'load-path (expand-file-name "c:/emacs_plugins/cedet-1.0/common")) 
10. (load-file "c:/emacs_plugins/cedet-1.0/common/cedet.el") 
11. (add-to-list 'load-path (expand-file-name "c:/emacs_plugins/cedet-1.0/semantic")) 
12. (add-to-list 'load-path (expand-file-name "c:/emacs_plugins/cedet-1.0/eieio")) 
13. (add-to-list 'load-path (expand-file-name "c:/emacs_plugins/cedet-1.0/speedbar")) 
14. (add-to-list 'load-path (expand-file-name "c:/emacs_plugins/elib-1.0")) 
15. (add-to-list 'load-path (expand-file-name "c:/emacs_plugins/ecb-2.40")) 
16. (load-file (expand-file-name "c:/emacs_plugins/ecb-2.40/ecb.el")) 
17. (require 'font-lock) 
18. (require 'ecb) 
19. (require 'ecb-autoloads) 
20. (require 'jde) 
21. 
22. (custom-set-variables 
23. '(browse-url-browser-function (quote browse-url-generic)) 
24. '(browse-url-generic-program "chromium-browser") 
25. '(column-number-mode t) 
26. '(jde-gen-buffer-boilerplate (quote (nil " 
27. //Copyright (c) 2011-2012 Esri 
28. // Esri (Beijing) Software Research and Development Center 
29. // 
30. // All rights reserved under the copyright laws of the United States. 
31. "))) 
32. '(jde-complete-function (quote jde-complete-menu)) 
33. '(jde-debugger (quote ("jdb"))) 
34. '(jde-db-option-connect-socket (quote (nil "9009"))) 
35. '(jde-jdk-registry (quote (("1.6.0_29" . "C:/Program Files/Java/jdk1.6.0_29")))) 
36. '(jde-resolve-relative-paths-p t) 
37. '(jde-run-option-debug (quote ("Server" "Socket" "javadebug" nil "5005" t)))) 
38. 
39. (setq tab-width 4) 
40. (setq c-basic-offset 2)

(setq debug-on-error t)
(global-set-key [f5] (lambda() (interactive) (revert-buffer t t)))
(setq c-basic-offset 4)
(show-paren-mode)

;;JDEE configuration

(add-to-list 'load-path (expand-file-name "c:/emacs_plugins/jdee-2.4.0.1/lisp"))
(add-to-list 'load-path (expand-file-name "c:/emacs_plugins/cedet-1.0/common"))
(load-file "c:/emacs_plugins/cedet-1.0/common/cedet.el")
(add-to-list 'load-path (expand-file-name "c:/emacs_plugins/cedet-1.0/semantic"))
(add-to-list 'load-path (expand-file-name "c:/emacs_plugins/cedet-1.0/eieio"))
(add-to-list 'load-path (expand-file-name "c:/emacs_plugins/cedet-1.0/speedbar"))
(add-to-list 'load-path (expand-file-name "c:/emacs_plugins/elib-1.0"))
(add-to-list 'load-path (expand-file-name "c:/emacs_plugins/ecb-2.40"))
(load-file (expand-file-name "c:/emacs_plugins/ecb-2.40/ecb.el"))
(require 'font-lock)
(require 'ecb)
(require 'ecb-autoloads)
(require 'jde)

(custom-set-variables
 '(browse-url-browser-function (quote browse-url-generic))
 '(browse-url-generic-program "chromium-browser")
 '(column-number-mode t)
 '(jde-gen-buffer-boilerplate (quote (nil "
//Copyright (c) 2011-2012 Esri
// Esri (Beijing) Software Research and Development Center
//
// All rights reserved under the copyright laws of the United States.
")))
 '(jde-complete-function (quote jde-complete-menu))
 '(jde-debugger (quote ("jdb")))
 '(jde-db-option-connect-socket (quote (nil "9009")))
 '(jde-jdk-registry (quote (("1.6.0_29" . "C:/Program Files/Java/jdk1.6.0_29"))))
 '(jde-resolve-relative-paths-p t)
 '(jde-run-option-debug (quote ("Server" "Socket" "javadebug" nil "5005" t))))

(setq tab-width 4)
(setq c-basic-offset 2)


由于公司规定,里面增加了开发用的文件头的说明。





常用方法

JDEE的用法可以参考官方网站,我这里只列出自己最常用的。由于我不用Ant了,所以基本上编译都直接采用mvn命令,这些就不劳驾JDEE.java doc生成,也通过maven plugin来完成,不需要JDEE帮忙。

创建类

输入命令:jde-gen-class-buffer
然后按照向导提示完成创建类文件的过程。相对比较简单,但是能用了。

智能提示

在需要提示的地方按下组合键:Ctrl c v .
智能提示有几种方式(弹出窗口用于桌面版本,其他两种可以用于服务器纯字符界面),具体参见官方文档。

源代码跳转

在需要跳转的地方按下组合键:Ctrl c v y
前提是你正确配置了源代码路径。

注释生成

在方法或者类的那行按下:Ctrl c v j

查看API文档

在要查看的类上直接按F1,会弹出一个窗口让你选择究竟是哪个文档,选择后,Emacs打开Chromium浏览器(前面配置好了)。

重构代码

简单修改一个类名,可以用JDE的菜单。不过大规模的重构最好还是用现代IDE来做,所以我通常都会装一个NetBeans7.0.1,来帮我重构代码。

如何调试

调试非常重要,我们可以通过命令行方式调试,不过JDEE提供了比较方便的功能,可以随着单步命令的进行让指针指向当前源代码位置。下面列出三种常用调试。

查找源代码

用命令jde-find,很牛的,试试看。
当然也可以切换到eshell,然后直接用grep -n -R '关键字' ./

普通Java application调试

确保.emacs文件中有:
(custom-set-variables
'(jde-debugger (quote ("jdb")))

获得更多信息:http://jdee.sourceforge.net/jdedoc/html/jdb-ug/jdb-ug-frame.html
Maven工程默认情况下:maven-compiler-plugin的debug属性被设置为true,这样编译出来的文件已经可以调试了。

通过jde-run-application-class来指定调试启动时的主类名称。
比如:
(jde-set-variables
'(jde-run-application-class "com.exactor.bulkupload.App")
...

如果调试启动时要传递参数,可以通过jde-db-option-application-args来指定。

设置断点:C-c C-a C-b

运行程序,打开App.java文件,然后按键:
C-c C-v C-d

然后用jdb命令吧,run 或者 cont等。

TestNG调试

启动testng实例:
mvn -Dmaven.surefire.debug test

确保prj.el文件中包含了:
'(jde-db-option-connect-socket (quote (nil "5005")))

输入命令:jde-jdb-attach-via-socket
或者 选择菜单(我不喜欢用鼠标)jdb->external process->attach via socket

设置断点吧
然后输入run命令

Web程序调试

1)假定你已经成功把web程序部署到glassfish 3.1 server上。并且jde-globalclasspath设置正确。
2)通过maven命令以调试方式启动glasfish server
mvn org.glassfish.maven.plugin:maven-glassfish-plugin:start-domain
3)在浏览器中打开网站
4)选择 Jdb->External Process->Attach To 菜单.
jde-db-option-connect-socket 允许你设置默认的远程连接的地址和端口号.
5)设置断点C-c C-a C-b.
6)在网页上执行一些操作,使得程序停在你的断点上。
断点命中: "thread=http-thread-pool-8080-(2)", com.webt.pagebean.LoginPage.clickLoginButton(), line=94 bci=0