Fast path switching between projects

Really fast and easy to try different versions of things that are wired into the command line path. For example, switching grails versions is as simple as....

Object Partners

I just finished some mods to the script I use when switching from one project to another.  The script makes it really fast and easy to try different versions of things that are wired into the command line path.  For example, switching grails versions is as simple as

use grails 1.1.1
  grails run-app

Switching back again…

use grails 1.2.0
  grails run-app

Three things come together to make this work.

First, I keep a directory called devtools full of different versions of tools.  Like this…

~/devtools/
    ant/
      ant-1.7.0/
      ant-1.7.1/
      current -> ant-1.7.1
    jdk/
      jdk-1.3.1/
      jdk-1.4.2/
      jdk-1.5.0/
      jdk-1.6.0/
      current -> jdk-1.5.0
    grails/
      grails-1.1.1/
      grails-1.2.0/
      current -> grails-1.1.1
  ...

and so on. I keep this in my home directory so it is easy for me to replicate across machines. You might want to keep it in some system specific location; like /usr/local.

Second, in my .profile I set up a bunch of environment variables to point to the current symlinks (for example: export GRAILS_HOME=~/devtools/grails/current). Also in .profile, my path is configured to include GRAILS_HOME/bin.

And third, the use script (see below) sets the current symbolic links.

With these three things in place I can quickly switch between projects with aliases that call the use script. For example…

alias iaap="use jdk 1.5.0; use grails 1.1.1; cd ~/opi/clients/mdh/iaap"
  alias buyer="use jdk 1.6.0; use grails 1.2.0; cd ~/opi/clients/buyer"
  ...

There have got to be a million ways to do this kind of thing, but with this script I can try out different versions of things really fast. And it is especially useful when I have to go back and pick up an old project for doing a demo, debugging or whatever.

[Note: This works great for me on macosx/linux/unix though I am not sure how well it’ll fly with cygwin; don’t don’t know how well symbolic links work on windows (ln -s.)]

The use script:

#!/bin/sh
#
\# sets a symlink by convention...
#
\# For example,
\#   in a directory names ~/devtools/grails with
\#     ~/devtools/grails/grails-1.1.2 and
\#     ~/devtools/grails/grails-1.2,
\#   executing `use 1.2`
\#   will create a symlink ~/devtools/grails/current -> ~/devtools/grails/grails-1.2
#
\# Set your path to include DEFAULT_TOOL_DIR/grails/current/bin.  Got it?
#
USAGE="
USAGE: `basename $0`: must specify version (and optional component name.)n
USAGE: For example: `basename $0` [component] version
"  

echo  

DEFAULT_TOOL_DIR=~/devtools  

if [ $# -eq 2 ]; then
    COMPONENT=$1
    VERSION=$2
    echo "default_tool_dir = $DEFAULT_TOOL_DIR"
    cd $DEFAULT_TOOL_DIR/$COMPONENT
elif [ $# -eq 1 ]; then
    COMPONENT=`basename $PWD`
    VERSION=$1
else
    echo $USAGE
    exit -1
fi  

USE=$COMPONENT-$VERSION  

if [ -d $USE ]; then
    if [ -h current ]; then
        CURRENT=`ls -l current | awk '{ print $11 }'`
        if [ "$CURRENT" = "$USE" ]; then
            echo "keeping $CURRENT"
        else
            echo "switching from $CURRENT to $USE"
            rm current
            ln -s $USE current
            ls -l current | awk '{ printf "%s %s %s", $9, $10, $11 }'
        echo
        fi
    else
        echo "using $USE"
        ln -s $USE current
        ls -l current | awk '{ printf "%s %s %s", $9, $10, $11 }'
        echo
    fi
else
    echo "ERROR: missing directory for $@"
    if [ -h current ]; then
        CURRENT=`ls -l current | awk '{ print $11 }'`
        echo "WARNING: keeping $CURRENT"
    else
        echo "WARNING: current not set."
    fi
    echo $USAGE
    exit -1
fi

Share this Post

Related Blog Posts

JVM

Simplifying JPA Testing with Springs PersistenceUnitPostProcessor

December 11th, 2009

At my current client, we are writing an application that is deployed as an ear into Websphere. One of the goals of the project is to write out-of-container tests so that we dont have to build and deploy over and over again. Two of the tools we are using in this application is JPA for the ORM and Spring. Initially, we had two persistance.xml files, one for production and one for testing. Some of the differences were that the production version used a transaction type of JTA and the test version used resource local. There were a few other property differences, but otherwise they needed to be identical. At first this was ok, but as our list of classes and properties grew it became a maintenance hassle.

Object Partners
JVM

Write Readable Tests with Mockito

December 4th, 2009

A good mocking framework is a must have in any Java developers toolset. Mocking frameworks greatly reduce the amount of code needed to effectively mock dependent interfaces and/or classes. Over the past several years EasyMock has been my mocking…

David Reines
JVM

Mac OS X Development Hurdles

November 23rd, 2009

Moving to Mac OS X as a development environment and some issues or differences discovered.

Object Partners

About the author