Toggle Java Debugger for Maven

Sometimes when creating a Maven plugin (Mojo) or when running java tools with maven using the exec plugin it’s helpful to debug the actual JVM that’s been started by maven. This is a snippet which you can throw into your ~/.bashrc to allow you to quickly toggle in and out of debug mode for Maven. Tweak the java debugger settings as you wish, as configured this will cause maven to wait on port 5005 for a remote debugger to attach.


Example Usage

$ mavenDebugOn
$ mvn exec:java -Dexec.mainClass="com.jontodd.MyClass"
Listening for transport dt_socket at address: 5005
$ mavenDebugOff

Unix sort command with columns

I’ve written about how to count unique occurrences of a string in a file in unix but today I needed to push the unix sort command a little further.

Consider the case where you have a CSV file with a column of text and a column of numbers like the following:

s, 3
h, 6
a, 4
c, 3
z, 2
b, 4

How would you sort it numerically by column two and then by column one such that if there are multiple numerical values in column two with the same value then column one will be sorted alphabetically? Well, of course the sort command has everything you need:

[~]$ sort -k2n -k1 -t',' /file
z, 2
c, 3
s, 3
a, 4
b, 4
h, 6

Breaking this down:

  • -kX – refers to column number so k2 is the second column
  • n – sorts numerically instead of alphabetically
  • -t – delimits by comma instead of whitespace

Finally, the ordering of the columns in the command dictates which column to order by first.

Sending notifications to Notification Center from Terminal

For the last few years I’ve been using Growl and Growl Notifier on the command-line to get asnc notifications when long running builds and tests have finished. Now that OSX Mountain Lion has it’s built-in Notification Center, the next inevitable question is how to get this same functionality in Notification Center.

The answer is a utility called terminal-notifier which can be downloaded from github. Just unzip the binary and drag it to your Applications folder and you’ll have access to the terminal-notifier command in terminal.

Here’s an example of how I use this for running tests in the background. When the tests finish you’ll get a notification which you can click on if you want the results.

mvn test; terminal-notifier -message "Your tests are done. \
  $(num_failures) failures." \ 
  -open http://localhost:8080/my/test/results/index.hml

Count unique grep results

I’m working through a big refactor in our code base and I wanted to quickly find a count of all the unique matches to a string search expression. Here’s an example of how to do this on a unix command-line with the help of grep, cut, sort, and uniq.

grep -r "import javax.xml.bind.annotation" . | \
cut -f2 -d \: | \
sort | uniq -c | sort -nr

Let’s break that down. The first part says: recursively search the current directory and any subdirectories for lines containing “import javax.xml.bind.annotation”. This part alone will result in something like: javax.xml.bind.annotation.XmlElement javax.xml.bind.annotation.XmlTransient javax.xml.bind.annotation.XmlElement

Now we want to get rid of everything before the “:” so we use cut and specify we want to delimit by “:” and select the second part for each line. Now we have something like this:


Now we want to get the count of each unique match so we can first do a sort which will group all the like elements together. Then pass it to uniq to take the sorted results and squash duplicates to a single line with a count. Then finally to sort the aggregated results we pass it to sort once more this time with -n which tells sort to do a numeric (not lex) sort and -r which makes the order descending. So our final output looks like:

2  javax.xml.bind.annotation.XmlElement
1  javax.xml.bind.annotation.XmlTransient