NewUser
NewUser

Reputation: 3819

How to stop batch execution when maven build fails

I have a batch file to make my life easy.

I want to build a maven project then copy the distribution from the target folder to another folder, then unzip them and followed by few copy replace instructions. This works perfectly fine when there is no issue with the code.

If there is any issue with the build and build fails then I want to stop the batch execution.

I am not able to figure it out.

::Kill all the java processs
taskkill /f /im "java.exe"

::Take the sprint number form the user
set /p sprintNumber=Enter the sprint number:%=%

:: Navigate to the code path
cd D:\Sprint-%sprintNumber%
call D:

:: Build the code

::@echo ###############BUILDING####################
::call mvn clean install

::cd ..

::Remove the distribution
IF EXIST "D:\Distribution\Sprint-%sprintNumber%\" (
    rmdir "D:\Distribution\Sprint-%sprintNumber%\" /s /q
)
::Few more instructions

I want to stop my execution if maven install fails. Please provide any suggestion to acheive this task.

Upvotes: 2

Views: 2081

Answers (2)

Andry
Andry

Reputation: 2727

You can stop an outer script execution by a malformed if expression:

Implementation: https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/assert.bat

assert.bat:

@echo off
@if 0 %*

build.bat:

@echo off

if not 1 == 1 call assert.bat "test 1"
if not 2 == 3 call assert.bat "test 2"
if not 3 == 3 call assert.bat "test 3"
>bulid.bat
"test 2" was unexpected at this time.

Note: This won't change the error level.

Upvotes: 0

foxidrive
foxidrive

Reputation: 41234

On the line after the build command add something like

if errorlevel 1 echo An error occurred & pause & goto :EOF

Upvotes: 8

Related Questions