Nathan Friedly
  • Home
  • About
  • Portfolio
  • Tech blog

How to make Apache Ant tasks run in a default directory

Apache takeoffI often open a new tab in my Terminal to run an Ant task, and often as not I forget to cd into the correct directory first and so I am greeted with this error:

$ ant compile
Buildfile: build.xml does not exist!
Build failed

On my system, there’s only one main project that uses ant, so I almost always intend for ant tasks to be run against that project’s build.xml. So, I created a function that makes ant tasks “just work” no matter what directory I am in.

The code

First you’ll need to edit your .profile file. To do so, run the following command:

nano ~/.profile

Then copy-paste in this code, changing the DEFAULT_ANT_DIR to the path to your project (whatever folder has the build.xml file in it):

# make ant commands run in the default directory if
# there is no build.xml in the current directory

export DEFAULT_ANT_DIR = /path/to/your/projects/folder/

function magic-ant() {
  if [ -e ./build.xml ]
    then
      ant $@                                                                    
    else
      pushd DEFAULT_ANT_DIR
      ant $@
      popd
  fi
}

# reset ant to avoid loops if you ever run `source ~/.profile` again
alias ant=`which ant`
alias ant="magic-ant"

Press [Control]-O then [Enter] to save and [Control]-X to exit nano.

Lastly, run this command to import your changes (or just close and reopen your terminal):

source ~/.profile

How it works

First, we define the path in a shell variable with the path to the default ant project. This isn’t absolutely required, but it keeps things tidy and simple.

AntNext, we create the magic-ant function which checks if there is a build.xml file in the current directory. If so, it uses that one, otherwise it calls pushd to temporarily change directories to our default one, runs the ant command, and then calls popd to get back to whatever directory you started in. $@ is an automatic variable that includes whatever parameters this function was called with.

After that we use alias to reset the ant command in order to avoid endless loops where our magic-ant function calls itself instead of the real ant if you source. The command which returns the path to the program with the given name. Putting it in backticks (`) makes bash execute the command and return the result.

Finally we alias ant to point to our new magic-ant function when run from the command line.

The only downside is that I haven’t yet figured out how to make Ant task tab completion work with this when you’re not in the project directory.

Photo Credits: Apache feather photo by Rich Bowen, Apache helicopter photo by The U.S. Army, plexiglass ant photo by Qfamily

Contact Nathan

  • nathan @ (this website)

Site Map

  • Home
  • About
  • Portfolio
  • Tech blog

Website by Nathan Friedly

Creative Commons License Content licensed under Creative Commons Attribution.

Source code available on Github under a MIT License

Built with DocPad, Bootstrap, and Node.js.