21

I'm working on a large Maven project consisting of a number of different modules. The Maven build intermittently fails on certain modules but if all is well, a simple manually invoked --resume-from (sometimes on a couple of different modules) will allow it to continue to success.

Am deliberately omitting the details of exactly why this happens - suspect it may be due to Windows file locking on files in the target folder - but that's not the point of the question.

Question: Is it possible to get Maven to automatically retry (maybe just once, or maybe up to 3 times), resuming from the failed module?

Thoughts: The only way I can currently think of doing it is via a batch file, which reads the last line and somehow extracts the module name to resume from - but that doesn't look easy. Don't want to reinvent the wheel and was wondering if there's a nice Maven plugin that already does the job?

Steve Chambers
  • 37,270
  • 24
  • 156
  • 208

3 Answers3

6

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.

Edit:

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.

Vishnu M. D.
  • 522
  • 6
  • 7
2

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...
:retry
  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%
endlocal
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
1

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
)
endlocal

Regret that I haven't tested it.

Anitha.R
  • 344
  • 2
  • 15
  • This may need some tweaking but for me it looks likes the path of least resistance. (I may post my own answer in future if I can get it to work reliably and possibly include retries etc.) – Steve Chambers Feb 01 '19 at 11:33
  • **Update:** The code as written here didn't work for me but provided a good starting point. Have now posted an [answer](https://stackoverflow.com/questions/54305409#54549477) with a modified version. – Steve Chambers Feb 06 '19 at 08:44