Search code examples
windowsbatch-filegrails

Run Grails command from batch file until it succeed


Description:

  • Grails project with several plugin dependencies (many of them can not be resolved instantly due to connectivity issues, some other are being resolved properly very quickly)
  • I run grails from the command line on the project's root in order to get the grails plugins installed successfully in my project but as some of them fails I have to run the same command again (and sometimes again)

Question:

How could I make a batch file for Windows (10) in order to execute the grails command over and over again until its result is successful

>>> I tried with this: How to run command until it succeeds?, but I had not luck with it. It executes the command the first time only and then stops even when there were some errors resolving the dependencies as shown in the image below.

enter image description here

Some help would be really appreciated.


Solution

  • Found a workaround:

    Logic:

    • Make sure there is not a war on the destination folder intended for this on your grails project structure.
    • Execute the "grails war" command, but not just doing a grails war on the batch but call grails war. It turns out this was a key aspect here. Since when the command is not preceded by the call keyword, grails executes and then stops the batch, it means the following lines are never executed (at least when the grails command fails)
    • Check whether the war file exists or not
    • If it fails, then retry
    • If successful, then done!

    Code:

    @echo off
    
    :loop
        echo Executing command...
    
        call grails war
    
        if exist "<full_path_to_war_file>" (
            echo Success!
        ) else (
            echo Failed!
            echo Retrying!
            goto loop
        )
    
    echo Done!
    

    Important: This variant uses the functionality of grails for creating a war. Grails, before creating the war, tries to resolve those pending dependencies, so if this fails, the war is never made, and this way, the failure of the dependencies resolution can be detected.

    I'd like to say I've never had done a .bat before until now (so, this might be improved a lot), and thanks a lot to those who posted here.