Thursday, January 08, 2015

Do you use mvn on a bash prompt?

Update Sunday 26 April 2015, 01:36:51 PM: renamed my github repos where these files are stored.

Here is my bash version of the DOS script I wrote about in Do you use mvn on a DOS prompt?.

Do you use mvn on a bash prompt? Sick of having to scroll through a tiny bash prompt looking for an error amongst thousands of lines of output? Sick of not being able to search through the log? Want to save logs for future reference?

Well, look no further! I have written raven.sh, a wrapper around mvn that does one thing: saves the output to a file (which still gets displayed on the console) and opens the file in your favourite text editor.

The script itself includes instructions for setting up dependencies (not much set up) and how to use the script.

You can also see this script in GitHub: https://github.com/robertmarkbram/RobUtils/blob/master/tools/maven/raven.sh which is where I shall keep it updated. Pastebin too.

#!/bin/bash

# ------------------------------------------------------------------------------
# -- What is Raven?
# A Bash wrapper around mvn to save output to file and open it in an editor
# By Robert Mark Bram
# https://github.com/robertmarkbram/RobUtils
# http://robertmarkbramprogrammer.blogspot.com.au/2015/01/do-you-use-mvn-on-bash-prompt.html
# ------------------------------------------------------------------------------
# Set-up Dependencies first.
# - This script
#    - Save this script as raven.sh in some folder you like to use for utilities etc.
#    - Add the path (folder/directory name) where this script lives to your PATH (*see note 1*).
#    - Otherwise you will have to use the absolute path to this script: e.g. "/C/myApps/Batch/raven" instead of just "raven".
# - Maven
#    - Download it from: http://maven.apache.org/
#    - Set M2_HOME variable (*see note 1*) or LOCAL_M2_HOME in this script.
# - Java
#    - Download and install it from: http://www.oracle.com/technetwork/java/javase/downloads/index.html
#    - Set JAVA_HOME variable (*see note 1*) or LOCAL_JAVA_HOME in this script.
# - tee (to send output to file and console)
#    - Should already be part of Cygwin/nix install.
# - Your favourite text editor.
#    - This script defaults to notepad. If you like it, do nothing.
#    - If you want to use something else, set EDITOR in this script
#    - Because we are *nix, you can set it to vim or less as well.
# - Temp dir
#    - This script defaults to ${TEMP}/maven.
#    - If you want to use something else, set TMPDIR in this script
#
# How to use this script.
# - Instead of using "mvn", just use "raven", e.g.
#       raven clean deploy
#       /C/myApps/Batch/raven clean deploy
#
# Note 1 - changing PATH or other environment variables on Win7 and above: two options.
# - OPTION 1: Start button > Search for "Environment Variables for your account" > modify PATH (or other variable) in top section, "user variables for USERNAME"
#    - No re-boot required, just restart the DOS prompt.
#    - PATH is set only for your user. Other logged users will not see it.
# - OPTION 2: Start button > Search for "Edit the System Environment Variables" > Environment Variables > modify PATH (or other variable) in bottom section, "System Variables"
#    - Re-boot required.
#    - PATH is set for all logged in users.


# ------------------------------------------------------------------------------
# -- Journal of Changes
# ------------------------------------------------------------------------------
# Thursday 08 January 2015, 04:57:36 PM
# - Adapted so that shell version works the same way as batch version.
# - Modified the way this script looks for maven and java such that it always uses local versions first.


# ------------------------------------------------------------------------------
# -- Variables for this script.
#     Edit these variables - dependencies of this script.
# ------------------------------------------------------------------------------
# Could be vim or less too.
# EDITOR=/C/Program\ Files\ \(x86\)/IDM\ Computer\ Solutions/UltraEdit/Uedit32.exe
EDITOR=notepad
LOCAL_JAVA_HOME=/C/Program\ Files/Java/jdk1.7.0_67
LOCAL_M2_HOME=/C/apps/apache-maven-3.2.3
TMPDIR=${TEMP}/maven
MAVEN_OPTS="-Xms512m -Xmx1024m -XX:MaxPermSize=256m"
# ############################################################################
# DO NOT EDIT BELOW HERE
# ############################################################################

# ------------------------------------------------------------------------------
# -- Common functions for this script.
# ------------------------------------------------------------------------------

# ===  FUNCTION  ===============================================================
#   DESCRIPTION:  Output message if verbose is on
#    PARAMETERS:  1 - message to be printed
#                 2 - options
#                     - "off" - don't use -e in echo
#       RETURNS:  -
# ==============================================================================
function message() {
   if [ "$#" -eq 2 -a "$2" == "off" ] ; then
      echo "$1" 2>&1 | tee -a "${outputFile}"
   else
      echo -e "$1" 2>&1 | tee -a "${outputFile}"
   fi
}

# ===  FUNCTION  ===============================================================
#   DESCRIPTION:  Check that all dependencies exist.
#    PARAMETERS:  -
#       RETURNS:  -
# ==============================================================================
function checkDependencies() {
   # Temp dir for logs.
   if [ ! -e "$TMPDIR" ] ; then
      mkdir "$TMPDIR"
   fi

   # Check for tee.
   type tee >/dev/null 2>&1 || {
      echo -e "\n**************"
      echo Please update your *nix to install the \"tee\" command.
      echo -e "**************\n"
   }

   # Check editor.
   if [ "${EDITOR}" != "notepad" -a "${EDITOR}" != "notepad.exe" -a "${EDITOR}" != "less" -a "${EDITOR}" != "vim" ] ; then
      if [ ! -e "${EDITOR}" ] ; then
         message "\n**************"
         message "Please update EDITOR [${EDITOR}]"
         message "variable to point to an editor you wish to use. Using notepad."
         message "**************\n"
         set EDITOR=notepad
      fi
   fi

   # Check Maven.
   # First check if local version is defined and exists.
   # Allows user to specify a different maven than what exists in M2_HOME or PATH.
   if [ -e "${LOCAL_M2_HOME}/bin/mvn" ] ; then
      # All good, use local.
      M2_HOME="${LOCAL_M2_HOME}"
      PATH="${M2_HOME}/bin;${PATH}"

   # Next check if M2_HOME set.
   elif [ -e "${M2_HOME}/bin/mvn" ] ; then
      PATH="${M2_HOME}/bin;${PATH}"

   # LOCAL_M2_HOME and M2_HOME don't work.
   # OK, no mvn then.
   else
      # OK, no mvn then.
      message "\n**************"
      message "-- Please download Maven from http://maven.apache.org/ and make"
      message "--   the mvn command available via one of the following methods"
      message "--   (this script detects maven in this order):"
      message "-- 1. Set LOCAL_M2_HOME in this script."
      message "-- 2. Set M2_HOME environment variable (system or user level)."
      message "-- --"
      message "-- We must be able to set M2_HOME from one of these."
      message "**************\n"
      reportResults
      exit 3
   fi

   # Check Java.
   # First check if local version is defined and exists.
   # Allows user to specify a different maven than what exists in JAVA_HOME or PATH.
   if [ -e "${LOCAL_JAVA_HOME}/bin/java" ] ; then
      # All good, use local.
      JAVA_HOME="${LOCAL_JAVA_HOME}"
      PATH="${JAVA_HOME}/bin;${PATH}"

   # Next check if JAVA_HOME set.
   elif [ -e "${JAVA_HOME}/bin/java" ] ; then
      PATH="${JAVA_HOME}/bin;${PATH}"

   # LOCAL_JAVA_HOME and JAVA_HOME don't work.
   # One of these MUST be set because maven requires JAVA_HOME to be set.
   else
      message "\n**************"
      message "-- Please download and install Java from"
      message "--   http://www.oracle.com/technetwork/java/javase/downloads/index.html"
      message "--   and make the java command available via one of the following methods"
      message "--   (this script detects java in this order):"
      message "-- 1. Set LOCAL_JAVA_HOME in this script."
      message "-- 2. Set JAVA_HOME environment variable (system or user level)."
      message "-- --"
      message "-- We must be able to set JAVA_HOME from one of these or maven will fail."
      message "**************\n"
      reportResults
      exit 3
   fi

}


# ===  FUNCTION  ===============================================================
#   DESCRIPTION:  Open log in editor.
#    PARAMETERS:  -
#       RETURNS:  -
# ==============================================================================
function reportResults() {
   case "${EDITOR}" in
      less )
         message "Output sent to $outputFile"
         less -I "${outputFile}" ;;
      vim )
         message "\nOutput sent to $outputFile"
         vim "${outputFile}" ;;
      * )
         # Some windows app.
         outputFileWin=`cygpath -w -a "${outputFile}"`
         message "\n"
         message "Output sent to ${outputFileWin}" off
         unix2dos "${outputFile}"
         "${EDITOR}" "${outputFileWin}" &;;
   esac

}

# ===  FUNCTION  ===============================================================
#   DESCRIPTION:  Output environment details to aid debugging.
#    PARAMETERS:  -
#       RETURNS:  -
# ==============================================================================
function showEnvironmentDetails() {
   message "\n-----------"
   message "Current Directory [`pwd`]"
   message "This script [`pwd -P`/$0]"
   message "M2_HOME [$M2_HOME]"
   message "JAVA_HOME [$JAVA_HOME]"
   message "EDITOR [$EDITOR]"
   message "MTEE [`type tee`]"
   message "TMPDIR [$TMPDIR]"
   message "PATH [$PATH]"
   message "-----------\n"
}






# ------------------------------------------------------------------------------
# -- Script Logic
# ------------------------------------------------------------------------------
# No args means just open default notes file.


# Create timestamp.
timestamp=$(date +"%Y%m%d_%H%M%S")
outputFile=$TMPDIR/maven_$timestamp.txt


checkDependencies

showEnvironmentDetails

message "Caw caw said the Raven!\n"


message "==========================================================================="
message "Command:"
message "mvn $@"
message "===========================================================================\n"

mvn "$@"  2>&1 | tee -a "${outputFile}"


reportResults

Wednesday, January 07, 2015

Do you use mvn on a DOS prompt?

Update Thursday 08 January 2015, 05:17:54 PM - Modified the way this script looks for maven and java such that it always uses local versions first.

Do you use mvn on a DOS prompt? Sick of having to scroll through a tiny DOS prompt looking for an error amongst thousands of lines of output? Sick of not being able to search through the log? Want to save logs for future reference?

Well, look no further! I have written raven.bat, a wrapper around mvn that does one thing: saves the output to a file (which still gets displayed on the console) and opens the file in your favourite text editor.

The script itself includes instructions for setting up dependencies (not much set up) and how to use the script.

You can also see this script in GitHub: https://github.com/robertmarkbram/RobUtils/blob/master/tools/maven/raven.bat which is where I shall keep it updated. Pastebin too.

@echo off
SETLOCAL

:: ------------------------------------------------------------------------------
:: -- What is Raven?
:: A DOS Batch wrapper around mvn to save output to file and open it in an editor
:: By Robert Mark Bram
:: https://github.com/robertmarkbram/RobUtils
:: http://robertmarkbramprogrammer.blogspot.com.au/2015/01/do-you-use-mvn-on-dos-prompt.html
:: ------------------------------------------------------------------------------
:: Set-up Dependencies first.
:: - This script
::    - Save this script as raven.bat in some folder you like to use for utilities etc.
::    - Add the path (folder/directory name) where this script lives to your PATH (*see note 1*).
::    - Otherwise you will have to use the absolute path to this script: e.g. "C:\myApps\Batch\raven" instead of just "raven".
:: - Maven
::    - Download it from: http://maven.apache.org/
::    - Set M2_HOME variable (*see note 1*) or LOCAL_M2_HOME in this script.
:: - Java
::    - Download and install it from: http://www.oracle.com/technetwork/java/javase/downloads/index.html
::    - Set JAVA_HOME variable (*see note 1*) or LOCAL_JAVA_HOME in this script.
:: - mtee.exe (to send output to file and console)
::    - Download mtee.exe from http://www.commandline.co.uk/mtee/
::    - Add the path (folder/directory name) where mtee.exe lives to your PATH (*see note 1*) or set MTEE variable in this script.
:: - Your favourite text editor.
::    - This script defaults to notepad. If you like it, do nothing.
::    - If you want to use something else, set EDITOR in this script
:: - Temp dir
::    - This script defaults to %TEMP%\maven.
::    - If you want to use something else, set TMPDIR in this script

:: How to use this script.
:: - Instead of using "mvn", just use "raven", e.g.
::       raven clean deploy
::       C:\myApps\Batch\raven clean deploy

:: Note 1 - changing PATH or other environment variables on Win7 and above: two options.
:: - OPTION 1: Start button > Search for "Environment Variables for your account" > modify PATH (or other variable) in top section, "user variables for USERNAME"
::    - No re-boot required, just restart the DOS prompt.
::    - PATH is set only for your user. Other logged users will not see it.
:: - OPTION 2: Start button > Search for "Edit the System Environment Variables" > Environment Variables > modify PATH (or other variable) in bottom section, "System Variables"
::    - Re-boot required.
::    - PATH is set for all logged in users.

:: History
:: Wednesday 07 January 2015, 07:07:33 PM
:: - Updated to check java, maven, mtee temp dir and editor variables.
:: Thursday 08 January 2015, 04:57:36 PM
:: - Modified the way this script looks for maven and java such that it always uses local versions first.


:: ############################################################################
:: Edit these variables - dependencies of this script.
:: ############################################################################
:: set EDITOR=C:\Program Files (x86)\IDM Computer Solutions\UltraEdit\Uedit32.exe
set EDITOR=notepad
set LOCAL_JAVA_HOME=C:\Program Files\Java\jdk1.7.0_67
set LOCAL_M2_HOME=C:\apps\apache-maven-3.2.3
set MTEE=C:\apps\mtee.exe
set TMPDIR=%TEMP%\maven
set MAVEN_OPTS=-Xms512m -Xmx1024m -XX:MaxPermSize=256m
:: ############################################################################
:: DO NOT EDIT BELOW HERE
:: ############################################################################

:: Create timestamp.
SET hh=%time:~0,2%
if "%time:~0,1%"==" " SET hh=0%hh:~1,1%
SET YYYYMMDD_HHMMSS=%date:~10,4%%date:~7,2%%date:~4,2%_%hh%%time:~3,2%%time:~6,2%
set LOG_FILE=%TMPDIR%\maven_%YYYYMMDD_HHMMSS%.txt

:: Check temp dir.
if not exist %TMPDIR% mkdir %TMPDIR%

:: Check mtee.exe
:: Check in path first.
set MTEE_FOUND=
for %%e in (%PATHEXT%) do (
  for %%X in (mtee%%e) do (
    if not defined MTEE_FOUND (
      set MTEE_FOUND=%%~$PATH:X
    )
  )
)
if "%MTEE_FOUND%" == "" (
   :: Not in path. Check script variable.
   if not exist %MTEE% (
      echo.
      echo **************
      echo Please download mtee.exe from http://www.commandline.co.uk/mtee/
      echo and update MTEE variable in this script.
      echo **************
      echo.
      goto :exit
   )
) else (
   set MTEE=%MTEE_FOUND%
)

:: Check editor.
if "%EDITOR%" == "notepad.exe" goto :editor_check_end
if "%EDITOR%" == "notepad" goto :editor_check_end
if not exist "%EDITOR%" (
   echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo Please update EDITOR ["%EDITOR%"] 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo variable to point to an editor you wish to use. Using notepad. 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
   set EDITOR=notepad
)
:editor_check_end

:: Check Maven.
:: First check if local version is defined and exists.
:: Allows user to specify a different maven than what exists in M2_HOME or PATH.
if exist "%LOCAL_M2_HOME%\bin\mvn.bat" (
   set M2_HOME=%LOCAL_M2_HOME%
   set "PATH=%M2_HOME%\bin;%PATH%"

) else if exist "%M2_HOME%\bin\mvn" (
   :: Next check if M2_HOME set. It is.
   set "PATH=%M2_HOME%\bin;%PATH%"

) else (
   :: LOCAL_M2_HOME and M2_HOME don't work.
   :: OK, no mvn then.
   echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo -- Please download Maven from http://maven.apache.org/ and make 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo --   the mvn command available via one of the following methods 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo --   ^(this script detects maven in this order^): 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo -- 1. Set LOCAL_M2_HOME in this script. 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo -- 2. Set M2_HOME environment variable ^(system or user level^). 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo -- -- 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo -- We must be able to set M2_HOME from one of these. 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
   goto :report_results
)

:: Check Java.
:: First check if local version is defined and exists.
:: Allows user to specify a different java than what exists in JAVA_HOME.
if exist "%LOCAL_JAVA_HOME%\bin\java.exe" (
   set JAVA_HOME=%LOCAL_JAVA_HOME%
   set "PATH=%JAVA_HOME%\bin;%PATH%"
) else if exist "%JAVA_HOME%\bin\java.exe" (
   :: Next check if JAVA_HOME set. It is.
   set "PATH=%JAVA_HOME%\bin;%PATH%"
) else (
   :: LOCAL_JAVA_HOME and JAVA_HOME don't work.
   :: One of these MUST be set because maven requires JAVA_HOME to be set.
   :: OK, no java then.
   echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo -- Please download and install Java from 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo --   http://www.oracle.com/technetwork/java/javase/downloads/index.html 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo --   and make the java command available via one of the following methods 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo --   ^(this script detects java in this order^): 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo -- 1. Set LOCAL_JAVA_HOME in this script. 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo -- 2. Set JAVA_HOME environment variable ^(system or user level^). 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo -- -- 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo -- We must be able to set JAVA_HOME from one of these or maven will fail. 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
   echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
   goto :report_results
)

echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo ----------- 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo Current Directory [%cd%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo This script [%0] 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo M2_HOME [%M2_HOME%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo JAVA_HOME [%JAVA_HOME%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo EDITOR [%EDITOR%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo MTEE [%MTEE%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo TMPDIR [%TMPDIR%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo PATH [%PATH%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo ----------- 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo Caw caw said the Raven! 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%


echo =========================================================================== 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo Command: 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo mvn %* 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo =========================================================================== 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%

mvn %* 2<&1 | "%MTEE%" /+ %LOG_FILE%

:report_results
echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
echo Output sent to %LOG_FILE% 2<&1 | "%MTEE%" /+ %LOG_FILE%
start "" "%EDITOR%" %LOG_FILE%

:exit
echo.
echo Done.