Advertisement
robertmarkbram

raqven.sh - a Bash wrapper for mvn

Jan 8th, 2015
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.69 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # ------------------------------------------------------------------------------
  4. # -- What is Raven?
  5. # A Bash wrapper around mvn to save output to file and open it in an editor
  6. # By Robert Mark Bram
  7. # https://github.com/robertmarkbram/RobUtils
  8. # http://robertmarkbramprogrammer.blogspot.com.au/2015/01/do-you-use-mvn-on-bash-prompt.html
  9. # ------------------------------------------------------------------------------
  10. # Set-up Dependencies first.
  11. # - This script
  12. #    - Save this script as raven.sh in some folder you like to use for utilities etc.
  13. #    - Add the path (folder/directory name) where this script lives to your PATH (*see note 1*).
  14. #    - Otherwise you will have to use the absolute path to this script: e.g. "/C/myApps/Batch/raven" instead of just "raven".
  15. # - Maven
  16. #    - Download it from: http://maven.apache.org/
  17. #    - Set M2_HOME variable (*see note 1*) or LOCAL_M2_HOME in this script.
  18. # - Java
  19. #    - Download and install it from: http://www.oracle.com/technetwork/java/javase/downloads/index.html
  20. #    - Set JAVA_HOME variable (*see note 1*) or LOCAL_JAVA_HOME in this script.
  21. # - tee (to send output to file and console)
  22. #    - Should already be part of Cygwin/nix install.
  23. # - Your favourite text editor.
  24. #    - This script defaults to notepad. If you like it, do nothing.
  25. #    - If you want to use something else, set EDITOR in this script
  26. #    - Because we are *nix, you can set it to vim or less as well.
  27. # - Temp dir
  28. #    - This script defaults to ${TEMP}/maven.
  29. #    - If you want to use something else, set TMPDIR in this script
  30. #
  31. # How to use this script.
  32. # - Instead of using "mvn", just use "raven", e.g.
  33. #       raven clean deploy
  34. #       /C/myApps/Batch/raven clean deploy
  35. #
  36. # Note 1 - changing PATH or other environment variables on Win7 and above: two options.
  37. # - OPTION 1: Start button > Search for "Environment Variables for your account" > modify PATH (or other variable) in top section, "user variables for USERNAME"
  38. #    - No re-boot required, just restart the DOS prompt.
  39. #    - PATH is set only for your user. Other logged users will not see it.
  40. # - OPTION 2: Start button > Search for "Edit the System Environment Variables" > Environment Variables > modify PATH (or other variable) in bottom section, "System Variables"
  41. #    - Re-boot required.
  42. #    - PATH is set for all logged in users.
  43.  
  44.  
  45. # ------------------------------------------------------------------------------
  46. # -- Journal of Changes
  47. # ------------------------------------------------------------------------------
  48. # Thursday 08 January 2015, 04:57:36 PM
  49. # - Adapted so that shell version works the same way as batch version.
  50. # - Modified the way this script looks for maven and java such that it always uses local versions first.
  51. # Thursday 08 January 2015, 06:55:09 PM
  52. # - Fix to reporting of commands. Use $* instead of $@ for reporting.
  53.  
  54. # ------------------------------------------------------------------------------
  55. # -- Variables for this script.
  56. #     Edit these variables - dependencies of this script.
  57. # ------------------------------------------------------------------------------
  58. # Could be vim or less too.
  59. EDITOR=/C/Program\ Files\ \(x86\)/IDM\ Computer\ Solutions/UltraEdit/Uedit32.exe
  60. # EDITOR=notepad
  61. LOCAL_JAVA_HOME=/C/Program\ Files/Java/jdk1.7.0_67
  62. LOCAL_M2_HOME=/C/apps/apache-maven-3.2.3
  63. # TMPDIR=${TEMP}/maven
  64. TMPDIR=${DirMavenLogs}
  65. MAVEN_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=256m"
  66. # ############################################################################
  67. # DO NOT EDIT BELOW HERE
  68. # ############################################################################
  69.  
  70. # ------------------------------------------------------------------------------
  71. # -- Common functions for this script.
  72. # ------------------------------------------------------------------------------
  73.  
  74. # ===  FUNCTION  ===============================================================
  75. #   DESCRIPTION:  Output message if verbose is on
  76. #    PARAMETERS:  1 - message to be printed
  77. #                 2 - options
  78. #                     - "off" - don't use -e in echo
  79. #       RETURNS:  -
  80. # ==============================================================================
  81. function message() {
  82.    if [ "$#" -eq 2 -a "$2" == "off" ] ; then
  83.       echo "$1" 2>&1 | tee -a "${outputFile}"
  84.    else
  85.       echo -e "$1" 2>&1 | tee -a "${outputFile}"
  86.    fi
  87. }
  88.  
  89. # ===  FUNCTION  ===============================================================
  90. #   DESCRIPTION:  Check that all dependencies exist.
  91. #    PARAMETERS:  -
  92. #       RETURNS:  -
  93. # ==============================================================================
  94. function checkDependencies() {
  95.    # Temp dir for logs.
  96.    if [ ! -e "$TMPDIR" ] ; then
  97.       mkdir "$TMPDIR"
  98.    fi
  99.  
  100.    # Check for tee.
  101.    type tee >/dev/null 2>&1 || {
  102.       echo -e "\n**************"
  103.       echo Please update your *nix to install the \"tee\" command.
  104.       echo -e "**************\n"
  105.    }
  106.  
  107.    # Check editor.
  108.    if [ "${EDITOR}" != "notepad" -a "${EDITOR}" != "notepad.exe" -a "${EDITOR}" != "less" -a "${EDITOR}" != "vim" ] ; then
  109.       if [ ! -e "${EDITOR}" ] ; then
  110.          message "\n**************"
  111.          message "Please update EDITOR [${EDITOR}]"
  112.          message "variable to point to an editor you wish to use. Using notepad."
  113.          message "**************\n"
  114.          set EDITOR=notepad
  115.       fi
  116.    fi
  117.  
  118.    # Check Maven.
  119.    # First check if local version is defined and exists.
  120.    # Allows user to specify a different maven than what exists in M2_HOME or PATH.
  121.    if [ -e "${LOCAL_M2_HOME}/bin/mvn" ] ; then
  122.       # All good, use local.
  123.       M2_HOME="${LOCAL_M2_HOME}"
  124.       PATH="${M2_HOME}/bin;${PATH}"
  125.  
  126.    # Next check if M2_HOME set.
  127.    elif [ -e "${M2_HOME}/bin/mvn" ] ; then
  128.       PATH="${M2_HOME}/bin;${PATH}"
  129.  
  130.    # LOCAL_M2_HOME and M2_HOME don't work.
  131.    # OK, no mvn then.
  132.    else
  133.       # OK, no mvn then.
  134.       message "\n**************"
  135.       message "-- Please download Maven from http://maven.apache.org/ and make"
  136.       message "--   the mvn command available via one of the following methods"
  137.       message "--   (this script detects maven in this order):"
  138.       message "-- 1. Set LOCAL_M2_HOME in this script."
  139.       message "-- 2. Set M2_HOME environment variable (system or user level)."
  140.       message "-- --"
  141.       message "-- We must be able to set M2_HOME from one of these."
  142.       message "**************\n"
  143.       reportResults
  144.       exit 3
  145.    fi
  146.  
  147.    # Check Java.
  148.    # First check if local version is defined and exists.
  149.    # Allows user to specify a different maven than what exists in JAVA_HOME or PATH.
  150.    if [ -e "${LOCAL_JAVA_HOME}/bin/java" ] ; then
  151.       # All good, use local.
  152.       JAVA_HOME="${LOCAL_JAVA_HOME}"
  153.       PATH="${JAVA_HOME}/bin;${PATH}"
  154.  
  155.    # Next check if JAVA_HOME set.
  156.    elif [ -e "${JAVA_HOME}/bin/java" ] ; then
  157.       PATH="${JAVA_HOME}/bin;${PATH}"
  158.  
  159.    # LOCAL_JAVA_HOME and JAVA_HOME don't work.
  160.    # One of these MUST be set because maven requires JAVA_HOME to be set.
  161.    else
  162.       message "\n**************"
  163.       message "-- Please download and install Java from"
  164.       message "--   http://www.oracle.com/technetwork/java/javase/downloads/index.html"
  165.       message "--   and make the java command available via one of the following methods"
  166.       message "--   (this script detects java in this order):"
  167.       message "-- 1. Set LOCAL_JAVA_HOME in this script."
  168.       message "-- 2. Set JAVA_HOME environment variable (system or user level)."
  169.       message "-- --"
  170.       message "-- We must be able to set JAVA_HOME from one of these or maven will fail."
  171.       message "**************\n"
  172.       reportResults
  173.       exit 3
  174.    fi
  175.  
  176. }
  177.  
  178.  
  179. # ===  FUNCTION  ===============================================================
  180. #   DESCRIPTION:  Open log in editor.
  181. #    PARAMETERS:  -
  182. #       RETURNS:  -
  183. # ==============================================================================
  184. function reportResults() {
  185.    case "${EDITOR}" in
  186.       less )
  187.          message "Output sent to $outputFile"
  188.          less -I "${outputFile}" ;;
  189.       vim )
  190.          message "\nOutput sent to $outputFile"
  191.          vim "${outputFile}" ;;
  192.       * )
  193.          # Some windows app.
  194.          outputFileWin=`cygpath -w -a "${outputFile}"`
  195.          message "\n"
  196.          message "Output sent to ${outputFileWin}" off
  197.          unix2dos "${outputFile}"
  198.          "${EDITOR}" "${outputFileWin}" &;;
  199.    esac
  200.  
  201. }
  202.  
  203. # ===  FUNCTION  ===============================================================
  204. #   DESCRIPTION:  Output environment details to aid debugging.
  205. #    PARAMETERS:  -
  206. #       RETURNS:  -
  207. # ==============================================================================
  208. function showEnvironmentDetails() {
  209.    message "\n-----------"
  210.    message "Current Directory [`pwd`]"
  211.    message "This script [`pwd -P`/$0]"
  212.    message "M2_HOME [$M2_HOME]"
  213.    message "JAVA_HOME [$JAVA_HOME]"
  214.    message "EDITOR [$EDITOR]"
  215.    message "MTEE [`type tee`]"
  216.    message "TMPDIR [$TMPDIR]"
  217.    message "PATH [$PATH]"
  218.    message "-----------\n"
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. # ------------------------------------------------------------------------------
  227. # -- Script Logic
  228. # ------------------------------------------------------------------------------
  229. # No args means just open default notes file.
  230.  
  231.  
  232. # Create timestamp.
  233. timestamp=$(date +"%Y%m%d_%H%M%S")
  234. outputFile=$TMPDIR/maven_$timestamp.txt
  235.  
  236.  
  237. checkDependencies
  238.  
  239. showEnvironmentDetails
  240.  
  241. message "Caw caw said the Raven!\n"
  242.  
  243.  
  244. message "==========================================================================="
  245. message "Command:"
  246. message "mvn $*"
  247. message "===========================================================================\n"
  248.  
  249. mvn "$@" 2>&1 | tee -a "${outputFile}"
  250.  
  251. reportResults
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement