Maven bash completion

Today I finally set up the bash completion for Maven. Yes, I know, it's nothing new. But in case you don't know how to do it, you might be interested in reading this post. If you do already know, go waste your time somewhere else. ;)

complaints

I've googled a little bit and I've found some nice solution but... well, many of them are monstrously overgrown. Some people produced kind of a swiss-knifes, which contains every possible maven command and parameter. I find it unnecessary and cumbersome.

I use like 5 maven goals everyday (clean, test, install, package, eclipse) and the rest of goals and parameters - help:effective-pom, site, -DartifactId, -Dmaven.test.skip etc. - so rarely that it makes no sense to automate them in any way. With these swiss-knife solutions, if I type

   mvn c[tab][tab]

I get a list of possible completions, from which only one - clean - is really interesting to me. So why should I use a tool which gives me so many useless responses ? I dunno, and I don't want to.

my solution

Ok, so, here goes my maven completion script. Taken from the official maven site (http://maven.apache.org/guides/mini/guide-bash-m2-completion.html) and slightly changed (I threw away few random lines ;).

So, the maven2 file that I put in /etc/bash_completion.d folder looks like this:

#!/bin/bash

_m2_make_goals()
{
   plugin=$1
   mojos=$2
   for mojo in $mojos
   do
     export goals="$goals $plugin:$mojo"
   done
}

_m2_complete()
{
   local cur goals

   COMPREPLY=()
   cur=${COMP_WORDS[COMP_CWORD]}
   goals='clean test install package site'
   goals=$goals _m2_make_goals "eclipse" "clean eclipse"
   cur=`echo $cur | sed 's/\\\\//g'`
   COMPREPLY=($(compgen -W "${goals}" ${cur} | sed 's/\\\\//g') )
}

complete -F _m2_complete -o filenames mvn

I'm not sure how it works and why it works, but it works for me, so I don't complain. :)

In addition to this, I've added these entries to .bash_aliases file:

alias mci='mvn -o clean install'
alias mclean='mvn eclipse:clean eclipse:eclipse clean'
alias mfclean='mvn eclipse:clean eclipse:eclipse clean -DdownloadSource=true -DdownloadJavadocs=true'
alias msite='mvn site'

I'm not sure yet if all of them will be really useful, probably I'll introduce some changes after some time. But for today, I'm really happy with this solution.

links

 
 
 
This used to be my blog. I moved to http://tomek.kaczanowscy.pl long time ago.

 
 
 

Please comment using