1. 程式人生 > >Emacs開發Java之安裝

Emacs開發Java之安裝

能夠 lock mkdir 端口 zip解壓 9.1 使用 config tom

Emacs開發Java

前言

筆者把多年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


配置JDEE

;;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_35" . "/usr/jdk1.6"))))
 ‘(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不報錯,勉強能用。
(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)

由於公司規定,裏面增加了開發用的文件頭的說明。

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow

Emacs開發Java之安裝