Setting up Intellij to Debug Into Grails / Spring / Acegi 3rd Party Source
While working on a project that made use of the Grails Acegi (Spring Security) plug-in I ran into some snags setting up my IDE so that I could debug into all the third-party source (including that of Grails, Spring and all of Spring’s dependent projects.) After spending more time tinkering than I will admit here, I realized that I had two problems. First, the Spring sources that I was attaching didn’t include all of the sources that I assumed were there. I thought that spring-framework-3.0.2.RELEASE-dependencies.zip included the Spring source with all its dependencies. Not so. To get both the Spring sources and all of the dependent projects, you need to download and unzip two archives. I’ll describe the details in this post.
My second problem resulted from the fact that I thought Grails Acegi (Spring Security) depended on the latest Spring Security sources (from release 3.x.) Wrong again. Surprisingly, the Grails Acegi plugin is bundled with Spring Security release 2.0.4 sources. They obviously work OK when launched in a container which depends on Spring 3.x for the non-security components.
Which Spring Version Should You Grab ?
If you follow the instructions below, you will be able to set up IDEA (my IDE of choice) to debug deep into Grails, Spring, and points beyond. Figuring out how to do this with Eclipse is left as an exercise for the reader. Whatever your IDE, you first need to figure out which Spring distribution you need. This can be done by simply going to $GRAILS_HOME/lib and looking at the Spring jars. At least this works for the current Grails release (1.3.1, at the time of this writing.)
Type the command below (assuming your environment is *nix/Cygwin):
- ls $GRAILS_HOME/lib
You will then see a bunch of Spring .jar’s tagged as 3.0.2.RELEASE. So that means for Grails 1.3.1 we need to grab the Spring 3.0.2.RELEASE source. If we go to http://www.springsource.org/download we’ll see spring-framework-3.0.2.RELEASE.zip and spring-framework-3.0.2.RELEASE-dependencies.zip. Download both of those archives. Then find all the .jars in the directory hierarchies that result from exploding the two archives you downloaded. There are .jars at many levels of subdirectories, so you will need to follow a receipe like this to get them all:
- mkdir /tmp/jarz
- cd [top-level-directory-where-RELEASE-dependencies.zip-was-extracted]
- find . -name “*sources*jar” -exec cp {} /tmp/jarz \; -print
- cd [top-level-directory-where-3.0.2.RELEASE.zip-was-extracted]
- find . -name “*sources*jar” -exec cp {} /tmp/jarz \; -print
- for jar in `/bin/ls *sources*.jar` ;do jar -xvf $jar ; done
Now load up a Grails project of your choosing into IDEA. Next, select ‘Go To’ from the main menu, then ‘C’ for ‘Go To Class’. Type the name of my favorite Spring class, JdbcTemplate, in the ‘Enter class name’ dialog. Odds are the source won’t be available. What you’ll see instead is a skeleton page showing only the method, interface and member variable definitions that are discernible from byte code. You should also see a button that says ‘Attach Source’. Click that and specify the directory /tmp/jarz, or wherever you stuck your exploded jars. The key point here is that the .jar’s must be exploded so that you can attach all of them in one fell swoop. That is the purpose of the last line of the script/recipe given above where we do the ‘jar xvf’, which does the exploding.
Now its time to set things up so you can debug into the source code for the Grails framework itself. Again,try ‘Go To Class’ specifying some class in the core Grails distribution. GrailsDispatcherServlet is a good one. Again, odds are you will get a skeleton page for the source, lacking method bodies, but containing the ‘Attach Source’ button. Click that button again, and this time specify $GRAILS_HOME/src/java. This is the directory that houses both the Java and Groovy sources for the Grails framework.
Finally, you can attach the sources corresponding to the libraries pulled in by the Grails Acegi plug-in by going to http://www.springsource.org/download and going to the section where it says ‘Get the latest Spring Security releases here’. You’ll select 2.0.5, then on the subsequent page, click the ‘More’ tab and select 2.0.4. Follow the same procedure we gave for the core Spring framework classes to extract and load the .jars for Spring Security.
(Side note: I wrote in a previous blog post that it is possible to streamline this process if you wrap your Grails application with a Maven pom. But sadly, I’m finding that the Maven Grails plugin does not always keep up with the latest version of Grails. So, for my current project I’m settings things up without Maven.)
























on June 20th, 2010
[...] To track the problem down I spent several hours crawling through the source with my debugger (this post will explains how I attached all the necessary Grails and Spring sources so I could see [...]