Spaces, not Tabs

The holy war about spaces or tabs continues and you can see all sorts of arguments for each side raging around the internet.

I prefer spaces because it’s a consistent format that remains the same no matter what you are using.  With tabs things appear differently depending on how your editor or other program is configured.  Plus the worst thing is when spaces and tabs get intermixed.

Arguments for tabs such as they take up less room or it’s easier to move around are in my mind weak.  In this day of terabyte disk drives, file size differences between tabs and spaces are trivial and most of your disk space is taken up by images and binary files anyway.  And being able to move around and format quickly is not an issue either with modern editors.

The two editors I use most are XEmacs and Eclipse.  Below I describe how to use spaces for indents for each program.

In Emacs:

(setq-default indent-tabs-mode nil)  ; Use spaces for indents except
                                     ; in buffers that already have
                                     ; their own local values for the
                                     ; variable.

In Eclipse:

Eclipse Ganymede

I was about to write a post about how much I was starting to dislike Eclipse. Two years ago I was singing the praises of Eclipse but Eclipse Europa (version 3.3) in my opinion has been a disaster. It is incredibly slow, crashes often, runs out of memory, etc. I found myself at times using XEmacs instead because it was faster.

But today I installed Eclipse Ganymede (version 3.4) and so far life has been much better.  It is much more responsive, it is not crashing, things are running smoothly.   You can read about what is New and Noteworthy in Ganymede.  The most exciting new feature to me is the Retain case of match when replacing, just like XEmacs.

Also I noticed that in my JBoss project, hot swapping of code is again working.  I am not sure if this is because I upgraded to Ganymede or because I turned on “Build Automatically”.  I suspect it might be the latter.

Maven Integration for Eclipse

Previously I was using an old Maven integration for Eclipse, version 0.0.11, which I got from http://m2eclipse.codehaus.org/update.  The plugin was horribly slow and seemed to sometimes interfere with my builds.

Today I upgraded to the latest, version 0.9.4, from http://m2eclipse.sonatype.org/update/ and things are moving much more smoothly.  You can learn more at Maven Integration for Eclipse.

In Windows > Preferences > Maven I only turned on Download Artifact Sources and Download Artifiact JavaDoc.  Download repository index updates on startup is on by default but I turned it off after receiving this advice from a fellow developer, Bill Crook.

It’s unclear to me how the m2 plugin uses the indexes so I don’t think I can answer you. 🙂 Enabling it causes the plugin to rescan the entire repository if i remember correctly. I personally am not willing to have Eclipse do a full file scan of the local repository each time i start for unknown reasons. if you don’t mind the incurred overhead, go ahead and enable it!

The Maven plugin has a nice dependency feature which Bill Crook explains well.

There will come a time when you need to work on two projects simultaneously. Additionally, there will probably be a dependency from one project to another. The key to doing this effectively is understanding workspace resolution. Workspace resolution is a concept of the m2eclipse plugin. The way this works is that the plugin scans all projects in your workspace and analyzes the poms. Based on group and artifact ID’s the plugin will know if there are interdependencies between your projects. Once the plugin has successfully detected the dependency between projects, you can jump from one project source into another when jumping into methods (via control-click or F3).

There is one important caveat to this, versions. In addition to looking at group and artifact ID’s, the m2eclipse plugin will look at versions to determine if the project for a dependency is in the current workspace. Because of this you must make sure the versions match. Let’s take the example of projects A and B in your workspace. Assume A has a dependency on version 2.0.0.1 of B. Now, if the version of project B in your workspace is 3.0-SNAPSHOT, workspace resolution will NOT work. Can you figure out how to make this work? If you guessed, update the pom of project A to depend on 3.0-SNAPSHOT, you are correct. The moral of the story is be aware of versions when trying to get workspace resolution working.

You will know that workspace resolution is not functioning properly if you see duplicates of the same class when doing searches with control-shift-t. This behavior makes sense when you think about it as Eclipse sees two of these classes in your workspace. For example, continuing with projects A and B as above, let’s assume there is a class Foo in project B. Eclipse would find class Foo in project B source (as java source in your workspace) as well as in version 2.0.0.1 of B (as a class file in jar dependency).

Now if someone would improve the Perforce plugin…

Eclipse memory settings

Recently I have been having problems with Eclipse running out of memory.

Previously I had as my command line argument for Eclipse.

eclipse.exe -vmargs --Xmx512m

The double dashes were a problem, preventing Eclipse to properly load the correct memory settings from eclipse.ini.

I then removed the -vmargs –Xmx512m from the command line argument and instead modified eclipse.ini which is in the top level of the Eclipse installation. This blog article, Eclipse and memory settings, explains how you should put each argument on a separate line and that you can go to Help > About Eclipse Platform and then click on the Configuration Details button to check that Eclipse is running with the correct settings.

I changed this:

-Xms40m
-Xmx512m

to:

-Xms120m
-Xmx1024m

but I reverted back when I realized it was my original command line argument that was the problem.

I tried to use these settings as recommended in this forum, [news.eclipse.tools.jdt] Re: How to prevent out of memory errors?

-Xms120m
-Xmx1024m
-XX:PermSize=256M
-XX:MaxPermSize=512M

But for some reason that kept crashing the Eclipse startup.

Maven Assembly Convert Shell Scripts to use UNIX LF’s

In a previous post, Shell Scripts with Windows LF’s Fail in Latest Cygwin, I wrote about how to use Perforce to check out all files in UNIX format. However if you use Eclipse then this solution will not work because Eclipse always inserts Windows LF’s in any line you insert into a file leading to a mess with files that have both UNIX and Windows LF’s.

However if you happen to use Maven to generate / copy your shell scripts to their target directories you can take advantage of the lineEnding property in the Assembly Director. If you specify the lineEnding property to be “unix” then the outputted shell scripts will be in UNIX format. For example:

<file>
  <source>src/main/scripts/foo.sh</source>
  <outputDirectory>bin</outputDirectory>
  <lineEnding>unix</lineEnding>
</file>

Shell Scripts with Windows LF’s Fail in Latest Cygwin

I am not sure when this happened but now in Cygwin shell scripts with Windows LF’s (the infamous control M’s) fail, i.e. Cygwin fails to run them because it will complain about the ‘\r’ character.

The simple way to fix this is to change your scripts to use UNIX LF’s by calling dos2unix or “conv -U”.

If you are using Perforce on Windows and seeing this problem then you can change your client to use “share” for the LineEnd option. Since Perforce stores all its text files in UNIX format on the server then it will write them out locally on your Windows machine in UNIX format. See the Perforce Knowledge Base article, CR/LF Issues and Text Line-endings. Fortunately Windows batch scripts with UNIX LF’s still run properly.

Note that when you make this change from the “local” LineEnd option then text files you already checked out will have the Windows LF’s. You will need to do a p4 sync -f on the files you want updated to have UNIX LF’s. Also when do you a p4 diff on your opened text files it will look like the whole file has changed. Again this is because of line feed issues. Doing a dos2unix on those opened text files will solve the problem.

Comparison of DSP and DSPEL

ATG has two JSP tag libraries, DSP and DSPEL. Both have similar syntax but DSPEL allows you to use JSTL.

Here’s a simple comparison of how you would use DSP versue how you would use DSPEL to do a simple RQL query.

DSPEL

<dspel:droplet name="/atg/dynamo/droplet/RQLQueryForEach">
  <dspel:param name="repository" bean="/betweengo/Repository"/>
  <dspel:param name="itemDescriptor" value="Merchant"/>
  <dspel:param name="queryRQL"
    value="name EQUALS \"${requestScope['nm']}\""/>
  <dspel:oparam name="empty">
    No merchant with the name "<c:out value="${requestScope['nm']}"/>".
  </dspel:oparam>
  <dspel:oparam name="output">
    Name: <dspel:valueof param="element.name"/>
  </dspel:oparam>
</dspel:droplet>

DSP

<% String query = "name EQUALS \"" + request.getAttribute("nm") + "\"";%>

<dsp:droplet name="/atg/dynamo/droplet/RQLQueryForEach">
  <dsp:param name="repository" bean="/betweengo/Repository"/>
  <dsp:param name="itemDescriptor" value="Merchant"/>
  <dsp:param name="queryRQL" value="<%= query %>"/>
  <dsp:oparam name="empty">
    No merchant with the name "<%= request.getAttribute("nm") %>".
  </dsp:oparam>
  <dsp:oparam name="output">
    Name: <dsp:valueof param="element.name"/>
  </dsp:oparam>
</dsp:droplet>

Configuring Capistrano and Mongrel

For this article I used the chapter Setting Up A Development Environment in Agile Web Development with Rails, version 2.0, plus these two articles.

Capistrano working together with Mongrel allows you to deploy and restart Mongrel clusters quite nicely.

Configure Mongrel Cluster

First you need to configure a Mongrel cluster. Here is an example that creates three Mongrel instances starting at port 8000, listening on the local interface, 127.0.0.1.

$  mongrel_rails cluster::configure -N 3 -p 8000 -e production -a 127.0.0.1 \
   -c /usr/local/rails/production/current \
   -C /usr/local/rail/production/current/config/mongrel_cluster.yml

Here is how the created file looks like.

---
cwd: /usr/local/rails/production/current
log_file: log/mongrel.log
port: "8000"
environment: production
address: 127.0.0.1
pid_file: tmp/pids/mongrel.pid
servers: 3

Note that for testing purposes you should comment out the address line.

#address: 127.0.0.1

We specified listening only on the local interface for security purposes but for testing the cluster we need to access it directly via its remote IP address.

Setup Capistrano

Next you create a stub Capfile and config/deploy.rb.

$ capify .

Now you can get a list of all the tasks that are available and there quite a few.

$ cap -T

Next modify config/deploy.rb like in this example.

require 'mongrel_cluster/recipes'

set :application, "foobook"
set :repository, "http://example.com/svn/foo/trunk/foobook"

role :web, "192.168.3.17"
role :app, "192.168.3.17"
role :db,  "192.168.3.117", :primary => true

set :deploy_to, "/usr/local/rails/production"
set :mongrel_conf, "#{current_path}/config/mongrel_cluster.yml"
set :svn_user, "fkim"
set :svn_password, "fkim"
#set :user, "root"            # defaults to the currently logged in user
set :scm, :subversion         # defaults to :subversion
set :svn, "/usr/bin/svn"      # defaults to searching the PATH

Deploy using Capistrano

Now run setup which will create the directories remotely.

$ cap deploy:setup

Check dependencies.

$ cap -q deploy:check

Deploy for the first time.

$ cap deploy:cold

Everytime after you can deploy like this.

$ cap deploy

Starting and Stopping Mongrel using Capistrano

If you want to just start the mongrel cluster.

$ cap mongrel:cluster:start

If you want to just stop the mongrel cluster.

$ cap mongrel:cluster:stop

And that’s it.

Not that bad at all. Starting and stopping mongrel was the biggest issue for me. I realized that I did not need to create the spin script as suggested in the Using Capistrano with Rails article. If the mongrel configuration is correct then Capistrano will correctly start and stop it and you can use the above cap commands to test it.

Perforce and Cygwin

To get Perforce to work with Cygwin is not too difficult. One just has to make an alias for p4 like this (thanks to this excellent Perforce FAQ).

if [ -e /bin/cygpath ]; then
  alias p4='p4 -d `cygpath -w $PWD`'
fi

This alias works all the time except I think when you are in a directory you accessed via a link.

Note you don’t need to go such lengths as described in this post.