Advertisement
robertmarkbram

raven.bat - a DOS batch wrapper for mvn

Jan 8th, 2015
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.76 KB | None | 0 0
  1. @echo off
  2. SETLOCAL
  3.  
  4. :: ------------------------------------------------------------------------------
  5. :: -- What is Raven?
  6. :: A DOS Batch wrapper around mvn to save output to file and open it in an editor
  7. :: By Robert Mark Bram
  8. :: https://github.com/robertmarkbram/RobUtils
  9. :: http://robertmarkbramprogrammer.blogspot.com.au/2015/01/do-you-use-mvn-on-dos-prompt.html
  10. :: ------------------------------------------------------------------------------
  11. :: Set-up Dependencies first.
  12. :: - This script
  13. ::    - Save this script as raven.bat in some folder you like to use for utilities etc.
  14. ::    - Add the path (folder/directory name) where this script lives to your PATH (*see note 1*).
  15. ::    - Otherwise you will have to use the absolute path to this script: e.g. "C:\myApps\Batch\raven" instead of just "raven".
  16. :: - Maven
  17. ::    - Download it from: http://maven.apache.org/
  18. ::    - Set M2_HOME variable (*see note 1*) or LOCAL_M2_HOME in this script.
  19. :: - Java
  20. ::    - Download and install it from: http://www.oracle.com/technetwork/java/javase/downloads/index.html
  21. ::    - Set JAVA_HOME variable (*see note 1*) or LOCAL_JAVA_HOME in this script.
  22. :: - mtee.exe (to send output to file and console)
  23. ::    - Download mtee.exe from http://www.commandline.co.uk/mtee/
  24. ::    - Add the path (folder/directory name) where mtee.exe lives to your PATH (*see note 1*) or set MTEE variable in this script.
  25. :: - Your favourite text editor.
  26. ::    - This script defaults to notepad. If you like it, do nothing.
  27. ::    - If you want to use something else, set EDITOR in this script
  28. :: - Temp dir
  29. ::    - This script defaults to %TEMP%\maven.
  30. ::    - If you want to use something else, set TMPDIR in this script
  31.  
  32. :: How to use this script.
  33. :: - Instead of using "mvn", just use "raven", e.g.
  34. ::       raven clean deploy
  35. ::       C:\myApps\Batch\raven clean deploy
  36.  
  37. :: Note 1 - changing PATH or other environment variables on Win7 and above: two options.
  38. :: - OPTION 1: Start button > Search for "Environment Variables for your account" > modify PATH (or other variable) in top section, "user variables for USERNAME"
  39. ::    - No re-boot required, just restart the DOS prompt.
  40. ::    - PATH is set only for your user. Other logged users will not see it.
  41. :: - OPTION 2: Start button > Search for "Edit the System Environment Variables" > Environment Variables > modify PATH (or other variable) in bottom section, "System Variables"
  42. ::    - Re-boot required.
  43. ::    - PATH is set for all logged in users.
  44.  
  45. :: History
  46. :: Wednesday 07 January 2015, 07:07:33 PM
  47. :: - Updated to check java, maven, mtee temp dir and editor variables.
  48. :: Thursday 08 January 2015, 04:57:36 PM
  49. :: - Modified the way this script looks for maven and java such that it always uses local versions first.
  50.  
  51.  
  52. :: ############################################################################
  53. :: Edit these variables - dependencies of this script.
  54. :: ############################################################################
  55. :: set EDITOR=C:\Program Files (x86)\IDM Computer Solutions\UltraEdit\Uedit32.exe
  56. set EDITOR=notepad
  57. set LOCAL_JAVA_HOME=C:\Program Files\Java\jdk1.7.0_67
  58. set LOCAL_M2_HOME=C:\apps\apache-maven-3.2.3
  59. set MTEE=C:\apps\mtee.exe
  60. set TMPDIR=%TEMP%\maven
  61. set MAVEN_OPTS=-Xms512m -Xmx1024m -XX:MaxPermSize=256m
  62. :: ############################################################################
  63. :: DO NOT EDIT BELOW HERE
  64. :: ############################################################################
  65.  
  66. :: Create timestamp.
  67. SET hh=%time:~0,2%
  68. if "%time:~0,1%"==" " SET hh=0%hh:~1,1%
  69. SET YYYYMMDD_HHMMSS=%date:~10,4%%date:~7,2%%date:~4,2%_%hh%%time:~3,2%%time:~6,2%
  70. set LOG_FILE=%TMPDIR%\maven_%YYYYMMDD_HHMMSS%.txt
  71.  
  72. :: Check temp dir.
  73. if not exist %TMPDIR% mkdir %TMPDIR%
  74.  
  75. :: Check mtee.exe
  76. :: Check in path first.
  77. set MTEE_FOUND=
  78. for %%e in (%PATHEXT%) do (
  79.   for %%X in (mtee%%e) do (
  80.     if not defined MTEE_FOUND (
  81.       set MTEE_FOUND=%%~$PATH:X
  82.     )
  83.   )
  84. )
  85. if "%MTEE_FOUND%" == "" (
  86.   :: Not in path. Check script variable.
  87.    if not exist %MTEE% (
  88.       echo.
  89.       echo **************
  90.       echo Please download mtee.exe from http://www.commandline.co.uk/mtee/
  91.       echo and update MTEE variable in this script.
  92.       echo **************
  93.       echo.
  94.       goto :exit
  95.    )
  96. ) else (
  97.    set MTEE=%MTEE_FOUND%
  98. )
  99.  
  100. :: Check editor.
  101. if "%EDITOR%" == "notepad.exe" goto :editor_check_end
  102. if "%EDITOR%" == "notepad" goto :editor_check_end
  103. if not exist "%EDITOR%" (
  104.    echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  105.    echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
  106.    echo Please update EDITOR ["%EDITOR%"] 2<&1 | "%MTEE%" /+ %LOG_FILE%
  107.    echo variable to point to an editor you wish to use. Using notepad. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  108.    echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
  109.    set EDITOR=notepad
  110. )
  111. :editor_check_end
  112.  
  113. :: Check Maven.
  114. :: First check if local version is defined and exists.
  115. :: Allows user to specify a different maven than what exists in M2_HOME or PATH.
  116. if exist "%LOCAL_M2_HOME%\bin\mvn.bat" (
  117.    set M2_HOME=%LOCAL_M2_HOME%
  118.    set "PATH=%M2_HOME%\bin;%PATH%"
  119.  
  120. ) else if exist "%M2_HOME%\bin\mvn" (
  121.   :: Next check if M2_HOME set. It is.
  122.    set "PATH=%M2_HOME%\bin;%PATH%"
  123.  
  124. ) else (
  125.   :: LOCAL_M2_HOME and M2_HOME don't work.
  126.   :: OK, no mvn then.
  127.    echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  128.    echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
  129.    echo -- Please download Maven from http://maven.apache.org/ and make 2<&1 | "%MTEE%" /+ %LOG_FILE%
  130.    echo --   the mvn command available via one of the following methods 2<&1 | "%MTEE%" /+ %LOG_FILE%
  131.    echo --   ^(this script detects maven in this order^): 2<&1 | "%MTEE%" /+ %LOG_FILE%
  132.    echo -- 1. Set LOCAL_M2_HOME in this script. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  133.    echo -- 2. Set M2_HOME environment variable ^(system or user level^). 2<&1 | "%MTEE%" /+ %LOG_FILE%
  134.    echo -- -- 2<&1 | "%MTEE%" /+ %LOG_FILE%
  135.    echo -- We must be able to set M2_HOME from one of these. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  136.    echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
  137.    echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  138.    goto :report_results
  139. )
  140.  
  141. :: Check Java.
  142. :: First check if local version is defined and exists.
  143. :: Allows user to specify a different java than what exists in JAVA_HOME.
  144. if exist "%LOCAL_JAVA_HOME%\bin\java.exe" (
  145.    set JAVA_HOME=%LOCAL_JAVA_HOME%
  146.    set "PATH=%JAVA_HOME%\bin;%PATH%"
  147. ) else if exist "%JAVA_HOME%\bin\java.exe" (
  148.   :: Next check if JAVA_HOME set. It is.
  149.    set "PATH=%JAVA_HOME%\bin;%PATH%"
  150. ) else (
  151.   :: LOCAL_JAVA_HOME and JAVA_HOME don't work.
  152.   :: One of these MUST be set because maven requires JAVA_HOME to be set.
  153.   :: OK, no java then.
  154.    echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  155.    echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
  156.    echo -- Please download and install Java from 2<&1 | "%MTEE%" /+ %LOG_FILE%
  157.    echo --   http://www.oracle.com/technetwork/java/javase/downloads/index.html 2<&1 | "%MTEE%" /+ %LOG_FILE%
  158.    echo --   and make the java command available via one of the following methods 2<&1 | "%MTEE%" /+ %LOG_FILE%
  159.    echo --   ^(this script detects java in this order^): 2<&1 | "%MTEE%" /+ %LOG_FILE%
  160.    echo -- 1. Set LOCAL_JAVA_HOME in this script. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  161.    echo -- 2. Set JAVA_HOME environment variable ^(system or user level^). 2<&1 | "%MTEE%" /+ %LOG_FILE%
  162.    echo -- -- 2<&1 | "%MTEE%" /+ %LOG_FILE%
  163.    echo -- We must be able to set JAVA_HOME from one of these or maven will fail. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  164.    echo ************** 2<&1 | "%MTEE%" /+ %LOG_FILE%
  165.    echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  166.    goto :report_results
  167. )
  168.  
  169. echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  170. echo ----------- 2<&1 | "%MTEE%" /+ %LOG_FILE%
  171. echo Current Directory [%cd%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
  172. echo This script [%0] 2<&1 | "%MTEE%" /+ %LOG_FILE%
  173. echo M2_HOME [%M2_HOME%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
  174. echo JAVA_HOME [%JAVA_HOME%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
  175. echo EDITOR [%EDITOR%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
  176. echo MTEE [%MTEE%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
  177. echo TMPDIR [%TMPDIR%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
  178. echo PATH [%PATH%] 2<&1 | "%MTEE%" /+ %LOG_FILE%
  179. echo ----------- 2<&1 | "%MTEE%" /+ %LOG_FILE%
  180. echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  181. echo Caw caw said the Raven! 2<&1 | "%MTEE%" /+ %LOG_FILE%
  182. echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  183.  
  184.  
  185. echo =========================================================================== 2<&1 | "%MTEE%" /+ %LOG_FILE%
  186. echo Command: 2<&1 | "%MTEE%" /+ %LOG_FILE%
  187. echo mvn %* 2<&1 | "%MTEE%" /+ %LOG_FILE%
  188. echo =========================================================================== 2<&1 | "%MTEE%" /+ %LOG_FILE%
  189. echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  190.  
  191. mvn %* 2<&1 | "%MTEE%" /+ %LOG_FILE%
  192.  
  193. :report_results
  194. echo. 2<&1 | "%MTEE%" /+ %LOG_FILE%
  195. echo Output sent to %LOG_FILE% 2<&1 | "%MTEE%" /+ %LOG_FILE%
  196. start "" "%EDITOR%" %LOG_FILE%
  197.  
  198. :exit
  199. echo.
  200. echo Done.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement