How to make Maven automatically retry (resume from failed module)?

Below is a fully fledged batch file, using Anitha.R's answer as a starting point.

Usage instructions:

  1. Ensure the Maven executable is in the Windows path.
  2. Ideally also ensure that a version of tee for Windows is in the Windows path. (E.g. I'm use the one provided as part of Git for Windows, having added Git's usr\bin folder to my path).
  3. Copy the batch file code into a new file.
  4. Change the max_retries value as desired.
  5. Save as "mvnretry.bat" in a folder in the Windows path.
  6. Run in the same way as Maven, e.g. mvnretry clean install -Pmyprofile -DskipTests.

Batch file code:

@echo off
setlocal enabledelayedexpansion
set max_retries=3
set retry_count=0
set output_file=%date:/=%%time::=%
set output_file=%output_file: =0%
set output_file=%temp%\mvn%output_file:.=%.out
set mvn_command=call mvn %*
set tee_found=true
where /q tee
if not errorlevel 1 goto retry
  set tee_found=false
  echo tee.exe not found in system path^^! Build will continue but output will be delayed...
  echo %mvn_command%
  if %tee_found%==true (
    %mvn_command% | tee %output_file%
  ) else (
    %mvn_command% > %output_file%
    type %output_file%
  echo Parsing output...
  set "resume_from="
  for /f "tokens=2 delims=:" %%i in ('type %output_file% ^| find "mvn <goals> -rf"') do (
    set resume_from=%%i
  if !retry_count! LSS %max_retries% if not [%resume_from%] == [] (
    echo Resuming from %resume_from%...
    set /a retry_count=retry_count+1
    set /a retries_remaining=max_retries-retry_count
    echo Retrying... [retries used: !retry_count!, retries remaining: !retries_remaining!]
    set mvn_command=call mvn -rf :%resume_from% %*
    goto retry
del /q %output_file%

I believe achieving this using a batch script looks easy. Hope the below script works for you.

@echo off
setlocal enabledelayedexpansion
for /f "tokens=2 delims=:" %%i in ('call mvn clean install ^| find "mvn <goals> -rf"') do (
    call mvn clean install -e -rf : %%i

Regret that I haven't tested it.

I have one idea to solve your problem and maybe you don't need a batch file to do the job. You can create a Maven Core Extension and create an EventSpy library to execute --resume-from when the build fails. I already tested this example found in this answer Run a maven plugin when build fails

Based on this answer you could use Maven Invoker Maven Invoker

yet... I found an extension to safe parallel builds Maven Core Extensions Example for Safe Parallel Builds

I known you need the project info ... so looking inside of ExecutionEvent and MavenProject class we have all info about the current building project.

I hope it will work for you too.


I'm looking for some extension that implements this behavior "automatically retry" when some module fails. If I do not find anything we should create an extension to that.