Maven Tab Auto Completion in Bash

Anyone using Maven has no doubt become fatigued with the long command line options and suffered from dumb typos. Then again, maybe it's just me....

I'm a big fan of auto-completion in the bash shell. So, I decided it was long past time to set-up auto completion goodness for my maven commands.

There are a couple of steps to this. First, make sure you have bash completion installed (most linux distros do by default). Next, is to install the maven completion file.Maven is a cloud-based software for workday consultants. It provides Consultant's with comprehensive training courses designed to help them become more familiar with Workday and develop the necessary skills to successfully configure and deploy solutions to their clients. Maven also offers a number of resources and tools that allow Workday consultants to track their progress and stay up-to-date with the latest Workday releases and best practices. Additionally, Workday Consultant Training can be enhanced through self-paced learning courses offered by Maven. These courses help consultants learn how to develop custom Workday implementations, customize existing Workday applications, and resolve Workday issues.

Bash Completion

Bash completion is a little different for each system. Getting and setting it up is easy.

Linux/Unix

Most linux systems will have it by default. If not, use your package manager and look for bash-completion

sudo apt-get install bash-completion

Mac

If you are using a Mac, install MacPorts then run:

sudo port install bash-completion

Windows

If using CYGWIN in the set-up dialog, find it under: shells

Ensure that Bash is Configured to Use Auto Completion

In the either the global (or your local; read: $HOME)bash.rc, bash.profile, or bash.bashrc (depending on your system) ensure there is something like the following:


if [ -f /etc/bash_completion ]; then
	. /etc/bash_completion
f

If not, then add it. Make sure that if you have to add it that the location of bash_completion is correct, it may be in different places on different systems. On a Mac (with MacPorts) instead of /etc/bash_completion it will be at /opt/local/etc/bash_completion

Maven2 Bash Completion File

Feel free to use my maven bash completion file. below:


# Bash Maven2 completion
#
_mvn()
{
   local cmds cur colonprefixes
   cmds="clean validate compile test package integration-test   \
      verify install deploy test-compile site generate-sources  \
      process-sources generate-resources process-resources      \
      eclipse:eclipse eclipse:add-maven-repo eclipse:clean      \
      idea:idea -DartifactId= -DgroupId= -Dmaven.test.skip=true \
      -Declipse.workspace= -DarchetypeArtifactId=               \
      netbeans-freeform:generate-netbeans-project               \
      tomcat:run tomcat:run-war tomcat:deploy"
   COMPREPLY=()
   cur=${COMP_WORDS[COMP_CWORD]}
   # Work-around bash_completion issue where bash interprets a colon
   # as a separator.
   # Work-around borrowed from the darcs work-around for the same
   # issue.
   colonprefixes=${cur%"${cur##*:}"}
   COMPREPLY=( $(compgen -W '$cmds'  -- $cur))
   local i=${#COMPREPLY[*]}
   while [ $((--i)) -ge 0 ]; do
      COMPREPLY[$i]=${COMPREPLY[$i]#"$colonprefixes"}
   done

        return 0
} &&
complete -F _mvn mvn

If you need to use some commands that are not in the list, it should be pretty clear, how to add them. Note: spaces act as separators to each command. That's why you'll see a spaces before the escape to new-line.

Add the Auto Completion File

The location to place the file will vary depending on your system. You will need to locate where bash completion is configured to look for the completion files.

Linux/Unix

The various linux systems will have the greatest variety of locations depending on your distro. Try /etc/bash_completion or /etc/bash_completion.d there should already be some files there. If you can't find it, try using the locate command.

locate bash_completion

or

locate bash_completion.d

Keep in mind, if you just installed the package, then you will need to run updatedb before locate will give you any results.

Mac with MacPorts

The correct location should be:

/opt/local/etc/bash_completion.d/

Windows - CYGWIN

On your cygwin bash shell, the location should be

/etc/bash_completion.d/

If you are trying to use your windows explorer, the location should be

etc\bash_completion.d\

from where ever you installed the cygwin package. In my case it's at

C:\cygwin\etc\bash_completion.d

Last Thoughts

First, be sure to run the source command against the edited bash profile before you expect anything to work (or open a new shell).

Bash is a great shell for the lazy developer. Learning how to save time and reduce typos just makes life better.

Now go out and enjoy your

mvn [tab][tab]

bliss :-)

Resources

I don't do all of this stuff myself. I stand on the shoulders of those before me. The following links have nothing to do with Maven but, the info on bash auto completion was very valuable.

copyright@2023 willcode4beer.All rights reserved

Tech Tags:


Sponsors:

About willCode4Beer