Thursday, May 28, 2015

Batch Script: Deploy to local Tomcat.

 :: deploy.bat  
 ::  
 :: Runs maven build commands on project folder,  
 :: copies generated binary to application server,  
 :: rename the binary to simpler name and  
 :: re-starts the application server.  
 ::  
 :: @author Ameya Aloni  
 ::  
 @ECHO OFF  
 @REM Declare the variables  
 SET project.home=<C:\path\to\project\directory>  
 SET app.server.home=<C:\path\to\application\server\home>  
 SET app.server.port=8282  
 SET app.context=<applicationcontext>  
 SET working.directory=%CD%  
 @REM Echoes information:  
 ECHO %project.home%  
 ECHO %app.server.home%  
 ECHO %app.server.port%  
 ECHO %app.context%  
 ECHO %working.directory%  
 ECHO.  
 CLS  
 CD %project.home%  
 ECHO Generating the war file...  
 CALL mvn clean package -Dmaven.test.skip=true  
 ECHO.  
 @REM Read war file name  
 FOR %%F IN (%project.home%\target\*.war) DO (  
   SET project.war.file.name=%%F  
   ECHO Found war file: %project.war.file.name%  
   GOTO rename  
 )  
 IF NOT EXIST %project.home%\target\*.war (  
   ECHO *** Aborting deployment as build has failed. ***  
   GOTO eof  
 )  
 :rename  
 SET renamed.project.war.file.name=%project.home%\target\%app.context%.war  
 ECHO Renaming war file: %project.war.file.name% to %renamed.project.war.file.name%  
 MOVE %project.war.file.name% %renamed.project.war.file.name%  
 ECHO.  
 @REM Cleanup existing app deployment.  
 IF NOT EXIST %app.server.home%\webapps\%app.context%.war (  
   GOTO deleteFolder  
 )  
 ECHO Removing war file: %app.server.home%\webapps\%app.context%.war  
 DEL %app.server.home%\webapps\%app.context%.war  
 :deleteFolder  
 IF NOT EXIST %app.server.home%\webapps\%app.context% (  
   GOTO copyWarFile  
 )  
 ECHO Removing war folder: %app.server.home%\webapps\%app.context%  
 RD /S /Q %app.server.home%\webapps\%app.context%  
 ECHO.  
 :copyWarFile  
 ECHO Copying war file: %renamed.project.war.file.name% to %app.server.home%\webapps  
 COPY %renamed.project.war.file.name% %app.server.home%\webapps  
 ECHO.  
 CD %app.server.home%\bin  
 ECHO Restarting application server...  
 NETSTAT -na | find "LISTENING" | find /C /I ":%app.server.port%" > NUL  
 IF ERRORLEVEL 1 (  
   ECHO Application server is stopped. Starting new instance...  
   ECHO.  
 ) ELSE (  
   ECHO Application server is running. Stopping existing instance...  
   CALL %app.server.home%\bin\shutdown.bat  
   ECHO.  
 )  
 CALL %app.server.home%\bin\startup.bat  
 :eof  
 @REM Back to working directory is:  
 CD %working.directory%  

No comments: