Friday, October 7, 2011

Emacs and J2EE on OSX notes

Here's how I've managed to setup Emacs for J2EE / Tomcat / Maven application development on OSX. 

Install Emacs

I use Emacs 23.3 from Emacs for OSX.


Malabar mode

Malabar mode offers easy code navigation, Maven, JUnit and Groovy integration. Download release 1.4 and extract in a directory to build it.

As the build will invoke emacs in batch mode, be sure that Emacs 23.3 is in your path (instead of using the one packaged with OSX).

What works on my machine:
unzip espenhw-malabar-mode-malabar-1.4.0-0-g35e1e5e.zip
cd espenhw-malabar-mode-1daaee4
export PATH=/Applications/Emacs.app/Contents/MacOS:$PATH
mvn package -DskipTests=true
Now in target directory there's a malabar-1.4.0-dist.zip. Unzip it where you put all your emacs librairies:

cd ~/.emacs.d/
unzip /path/to/malabar/target/malabar-1.4.0-dist.zip


Configure Emacs for Malabar

Open your .emacs and add:
(add-to-list 'load-path (expand-file-name "~/.emacs.d/malabar-1.4.0/lisp/")) 

(require 'cedet)
(require 'malabar-mode)
(setq malabar-groovy-lib-dir "/path/to/malabar/lib")
(add-to-list 'auto-mode-alist '("\\.java\\'" . malabar-mode))

(semantic-mode 1)
(require 'malabar-mode)
(setq malabar-groovy-lib-dir "~/.emacs.d/malabar-1.4.0/lib"
      malabar-load-source-from-sibling-projects t
      malabar-extra-source-locations
      '( "/absolute/path/to/project1/src" 
        "/absolute/path/to/project2/src" ) ) ;; you need to setup this 

(add-to-list 'auto-mode-alist '("\\.java\\'" . malabar-mode))

(add-hook 'malabar-mode-hook
     (lambda () 
       (add-hook 'after-save-hook 'malabar-compile-file-silently
                  nil t)))

Now open a .java file within Emacs.

Try malabar-jump-to-thing on one of your class and it should open the corresponding file.

Open a test file and try malabar-run-test. It should run maven test for this file. If all tests OK but finished with "BUILD FAILURE", then see next section


Java troubleshooting

Malabar should use java from jdk, not jde. I've tried setting JAVA_HOME and configure malabar without success. So quick dirty hack is:

cd /usr/bin
mv java java.jre
ln -s  /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java

Ressources

Entreprise Java Development With Emacs

Debugging Tomcat

Install JDIBug. In jdibug-expr.el, change the line (and to forget to byte-recompile or remove .elc):
(require 'semantic-java)
(require 'semantic/java)
Configure emacs:

(add-to-list 'load-path "~/.emacs.d/jdibug-0.5")
(setq jdibug-connect-hosts (quote ("localhost:8000"))
      jdibug-use-jde-source-paths nil
      jdibug-source-paths
      (list
			 "/path/to/project1/src" 
			 "/path/to/project2/src"  ))
(require 'jdibug)
Start Tomcat like with debugger enabled:
./bin/catalina.sh jpda start
Then try jdibug-connect to attach Emacs to tomcat.