Using Alfred 2 to run Maven scripts

Alfred is a quite popular productivity tool for OSX, however I’ve not cared much for it – until little over a month ago when Alfred 2 was released. The new version’s Power Pack includes a new feature called workflows which caught my attention. This allows you to define a keyword or keyboard shortcut that will run a set of actions of your choice.

I often build from source code using Maven, nearly all of these projects reside in my git folder. So usually it’s a matter of opening a terminal, navigating to the project and executing mvn clean package, then wait for the build to finish. Not a very time consuming process, but with Alfred’s workflow feature it can be improved upon. The Alfred manual is quite limited, so I decided to document the process here to serve as inspiration.

Above is the workflow with two bash scripts and two notifications.

The trick lies in the Script Filter box. It contains a bash script that finds all relevant pom-files and formats the list for use in Alfred. The script is shown below. Note that the expected output is in XML format:

echo "&lt?xml version='1.0'?>"
echo ""
for OUTPUT in $(ls -d -- ~/git/*{query}*/pom.xml)
do
    folder=$(dirname $OUTPUT) 
    project=${folder##*/}
    echo ""
    echo ""
    echo "mvn clean package $OUTPUT&lt/subtitle>"
    echo "icon.png&lt/icon>"
    echo ""
done
echo ""

When an item is selected the following script is executed:

cd {query}
mvn clean package > mvn.log
rc=$?
if [[ $rc != 0 ]] ; then
    echo "{query} FAILED!"
    open mvn.log
else
    echo "{query} successful."
fi

The rest is simply a matter of handling the notifications.

Alfred in action

Now when I press Alt+SPACE followed by mvn and a qualifier I’m rewarded with a list of all my root pom-files residing in ~/git. Selecting an entry will execute mvn clean package in the associated folder, a notification will pop up informing me that the script is running. Another notification will be shown when the script is done and if it failed; the error log will be opened.

mvn.png

If you like this workflow you’ll find the file here. It can be easily imported by double clicking on it. Note that the file has been zipped because my crappy service provider is using IIS which thinks files ending with “.alfredworkflow” does not exist.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.