Monday, October 29, 2012

Java Development With Emacs

I would like to share my J2EE development environment. Currently it's Emacs 23 on Linux, Maven and few additional pieces of software.

JDE

The JDE is a major mode for editing java sources. I will not describe the whole package, just emphasize customizations and a way I use it. Since JDE has lack of Maven support I use Maven2 Jdee plugin to transforms pom.xml to prj.el. Here is oneliner which periodically performs such transformation on projects tree:

Unfortunately this script is quite complex because there are multi-module Maven POMs and we have to exclude their subprojects.

The JDE's settings are quite simple. I just set compiler output directory to the temporary folder to prevent the source directories from useless compiled files.

Documentation

The JDE has nice feature "Context-Sensitive Class Help". There are no problems when you have graphical UI (XServer, Windows, etc.), you just specify your favorite browser (I'm sure it's Conkeror) and use it. I wish I had graphical UI. Console is all that I have. So I use text mode browser w3. It's unable to display local files so there is small fix:

Building

One of the most boring routine in Java language is specifying imports. The JDE may do it for us automatically. All we need is just to append calls to "before-save-hook". To provide fast call back each file is compiled after saving, so you can efficiently fix cause of error without full project rebuilding.

I use "compile" command to run Maven goals. It's suitable for all targets such as building, testing, running. Torstein K. Johansen suggests to use "C-z" binding to call "compile". Setting "jde-compile" variable to "compile" seems reasonable as well. Also it's necessary to set additional regexps to parse compiler output.

I list frequently used compilations commands in special file java-compile.org which then load as compilation history. Header of each line is part of effective command so is possible to find a line in mini-buffer's history by pressing "M-r" + "HEADER":

Here is an example of predefined command lines java-compile.org:

Tags

I use GNU GLOBAL as tagging system. It provides more correct tags table for Java then ctags or etags.

Logs

Log4j mode nice and only mode for viewing log4j logs in Emacs.

Completing

There are no special settings for Auto Complete Mode except adding "jde-mode" to "ac-list" and adding "gtags" as source.

Debugging

I use jdb as debugger. Its default behavior is good and all what we need is to set source paths. I've made module jdb-sourcepath.el which appends source paths to rc-file (~/.jdbrc). Function jdb-sourcepath-from-rc helps us to read settings in jdb-mode:

The brilliant Jdb feature is redefining class on fly. Here is a shortcut for it: