Friday, March 25, 2011

Maven aliases for bash

When I have to type more than 20+ characters on a command line to build an application hundreds of times a day, day after day, I look for a way to make my life easier by reducing the strain on my fingers; and at the same time increase my productivity by reducing typing time and errors. 

My tip today will be about developers that use Apache Maven, but you can apply the same method for any command that you are tired of typing. I have shared the method below with a few of my (lazy) colleagues and because everyone enjoyed it so much, I am now sharing it with you:



  1. Open your ~/.bashrc or ~/.bash_profile
  2. Add the following function:

    mvnrun () {
      echo mvn $@
      eval mvn $@
      echo mvn $@
    }

    The function will echo the full maven command before and after running it. I found the echos helpful to see the full command and also to help me remember the full command and not be dependent on the aliases.



  3. Add an alias referencing the mvnrun function for every command that you are tired of typing.

    For instance, when I want to run mvn clean install, I can just type the alias mci. This is the configuration you need to add to the file:
    alias mci='mvnrun clean install'
  4. Save the file and open a new bash shell to make the alias available.
  5. Type the new alias and you should see maven start executing.

    For instance, this is my bash output when I type mci :
     
    $ mci
    mvn clean install

    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Building My Maven Application
    [INFO]    task-segment: [clean, install]
    [INFO] ------------------------------------------------------------------------
    ...
    [INFO] [clean:clean {execution: default-clean}]
    ...
    [INFO] [surefire:test {execution: default-test}]
    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESSFUL
    [INFO] ------------------------------------------------------------------------
    mvn clean install

Note that you can still append maven options after the alias and the mvnrun function will append them at the end of your command. For instance, if I type:
$ mci -P int-tests
it will translate to:
$ mvn clean install -P int-tests


Below are the most common aliases I use. There are some conventions that I use such as ee stands for eclipse:eclipse. ec=eclipse:clean, st=skip tests, c=clean, i=install, t=test, s=skip, tc=tomcat:, dr=dependency report...you get the idea. If you don't like my conventions, feel free to add your own.

alias mecee='mvnrun eclipse:clean eclipse:eclipse $@'
alias meceedsdj='mvnrun eclipse:clean eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true$@'
alias mciecee='mvnrun clean install eclipse:clean eclipse:eclipse $@'
alias mcistecee='mvnrun clean install -Dmaven.test.skip=true eclipse:clean eclipse:eclipse $@'
alias mci='mvnrun clean install'
alias mcist='mvnrun clean install -Dmaven.test.skip=true $@'
alias mt='mvnrun test $@'
alias mc='mvnrun clean $@'
alias mct='mvnrun clean test $@'
alias mist='mvnrun install -Dmaven.test.skip=true $@'
alias mtd='mvnrun tomcat:deploy $@'
alias msttd='mvnrun tomcat:deploy -Dmaven.test.skip=true $@'
alias mtr='mvnrun tomcat:redeploy $@'
alias msttr='mvnrun tomcat:redeploy -Dmaven.test.skip=true $@ '
alias mtu='mvnrun tomcat:undeploy $@'
alias mtcr='mvnrun tomcat:run $@' 
alias mjr='mvnrun jetty:run $@'
alias mdr='mvnrun project-info-reports:dependencies'
alias mda='mvnrun dependency:analyze'

No comments:

Post a Comment