Maven exhibits strange behaviour from cmd windows 7 - windows

I am using maven in a project here at work and I've come up to a rather strange (at least for me) problem. When I do an: mvn package everything seems ok but the output of mvn disappears as soon as maven completes. To test it more I just did a: mvn --help and I could not see the output. As soon as the command finishes the prompt is cleared. I am doing my work now using redirections: mvn package > out and then: type out in order to see the output. Any help would be greatly appreciated!
Thanx!
Solved: To whoever finds this useful. At the end of mvn.bat there was the line
cmd /C exit /B %ERROR_CODE%
which I had to comment out like this:
#REM cmd /C exit /B %ERROR_CODE%

Check if the MAVEN_TERMINATE_CMD is set to on. If it is unset it:
set MAVEN_TERMINATE_CMD=
Check that the mvn.bat script in maven's bin directory doesn't contain an exit command without the /b option. It should end with the following line:
cmd /C exit /B %ERROR_CODE%
If neither alternative solves your problem, set the MAVEN_BATCH_PAUSE variable to on:
set MAVEN_BATCH_PAUSE=on
before you run maven. This should cause mvn.bat to wait for a keystroke before exiting.

The following is an example of how to use mvn in a batch file under Windows 7 and Maven 3.2.1
:: ensure variables do not propagate outside this batch call
setlocal
:: save current directory and change to ".."
pushd ..
:: tell mvn.bat to exit after done, not to spawn cmd (why is this by default..)
set MAVEN_TERMINATE_CMD=on
:: spawn new cmd prompt to handle the mvn call, which blocks and exits
cmd /c mvn clean install
:: do something fancy here
:: cd to saved directory (pushd)
popd
:: pause the script and require pressing a key to continue
pause

Related

Can I call %COMSPEC% without terminating all Windows batch execution?

Came across this oddity of batch behaviour as part of the build process in our systems that I'm trying to automate in Jenkins pipelines.
To skip to the meat of the problem and why I'm encountering it, the batch file I'm calling is a build generated batch file that must be called prior to another executable within the same command window to set up paths and alike for our executable to build our system. When building as a user this is fine, you just open the cmd console, run the batch, then run the executable. However in Jenkins I have to pass all commands effectively as one batch file to make it call within the same window, unless I want to mess around with configuring all those paths by hand using withEnv or something.
Something along the lines of this in my Jenkins groovy script:
// Option 1 - && operator
bat "env_configuration.bat && env_dependent_exec.exe"
// Option 2 - multi line version
bat """env_configuration.bat
env_dependent_exec.exe
"""
// Option 3 - same using calls, yadda yadda
bat """call env_configuration.bat
call env_dependent_exec.exe
"""
// Option 4 - Place these calls in batch file and call that instead
bat "run_it_all.bat"
As part of the batch file however, for whatever reason, it has this command within it.
%COMSPEC%
exit /b 0
This will call "C:\WINDOWS\system32\cmd.exe" and output the Mircrosoft version and related blurb.e.g:
Microsoft Windows [Version 10.0.19045.2486]
(c) Microsoft Corporation. All rights reserved.
The catch is, calling this executable will immediately end all batch execution. The line "exit /b 0" isn't actually hit, something whoever created this process I assume never realised. After this batch file process completes, all subsequent commands/calls, (Option 1 above is the easiest to repro) are never hit as all batch processing just stops. This is visible directly in cmd itself, you don't need Jenkins to reproduce.
I will probably wind up just find and replacing this line to comment it out or something... but I refuse to believe I can't find some way of stopping %COMSPEC% from ending all execution of whatever batch file calls it. If I were to guess, I would guess that cmd.exe calls EXIT as it finishes and kills all....
For the sake of interest I have tried modifying the batch file in numerous ways to try and see if I can call %COMSPEC% and still get the rest of the batch script to run afterwards.
:: This fails the same way
call %COMSPEC%
exit /b 0
:: This succeeds to run, but spawns another cmd window that doens't print in the original calling batch file output, so doesn't achieve the original goal
start %COMSPEC%
exit /b 0
:: call %COMSPEC% via subroutine also immediately terminates, we never see "echo 2"
call :comspec_call
exit /b 0
:comspec_call
#echo %COMSPEC% echo 1
%COMSPEC%
#echo %COMSPEC% echo 2
I would guess cmd.exe calls the EXIT command flat on termination, hence the process death, but I've yet to find a means to prevent it doing so...
I've checked %COMSPEC% for flags that might just printout and terminate nicely, but any flags I've found that do provide this information, also terminate with EXIT (I think)
Does anyone has any idea how to call this line and continue execution as I assume the original dev intended?
Cheers in advance!
Batch processing doesn’t just stop — you have started another command interpreter, and the one that launched it is waiting for the new instance to terminate. As it doesn’t quit, everything appears to halt and you wind up killing both to get things back to normal.
Create a new batch script that does everything:
:: controller.bat
#echo off
call env_configuration.bat
call some_other.bat
env_dependent_exec.exe
...
Use of the call command causes the command shell to invoke a script using the current interpreter.
Now your Jenkins groovy script should be:
bat controller.bat
(Disclaimer: I don’t know Jenkins, so...)
Thanks to #Duthomhas and #Magoo
The problem here is that a call to %COMSPEC% launches a new command line process that must terminate before the rest of the script can continue. I didn't understand that cmd.exe was spawning a new script that the call script was waiting to terminate.
With a simplified recap:
:: env_configuration.bat
#echo env_configuration before COMSPEC
#%COMSPEC%
#echo env_configuration after COMSPEC
:: controller.bat
#echo controller before env_configuration.bat
#call env_configuration.bat
#echo controller after env_configuration.bat
controller.bat && #echo back in shell
When you run controller.bat the process will output the Microsoft blurb, but halt.
If you enter "exit" at this point it will kick out of the terminal launched with %COMSPEC% and then all following script steps continue.
It appears the original dev needed the user to be in a new subprocess to continue. The final solution to doing as was intended here is:
:: env_configuration.bat
#echo env_configuration before COMSPEC
#call %COMSPEC% /C <whatever_command_or_exec_next>
#echo env_configuration after COMSPEC
:: controller.bat
#echo controller before env_configuration.bat
#call env_configuration.bat
#echo controller after env_configuration.bat
controller.bat && #echo back in shell
Cheers!

How to exit 'git-cmd.exe' after running a command via it in a batch file?

I'm writing a batch file that runs a command via git-cmd.exe but it doesn't run the command(s) after it.
I've tried to use CALL, START /WAIT, and START /B /WAIT. All have the same behavior. Maybe there is a parameter should be sent to git-cmd.exe to execute the command and exit but I didn't find any guide explaining how to use git-cmd.exe.
This is a sample batch file:
#ECHO OFF
SET "PATH=C:\Ruby26-x64\bin;C:\Program Files\nodejs;%PATH%"
SET "CurrentDirectory=%CD%"
CD /D "%UserProfile%\AppData\Local\GitHub\PortableGit_*\"
SET "GitDirectory=%CD%"
CD /D "%CurrentDirectory%"
"%GitDirectory%/git-cmd.exe" CALL rake build
PAUSE
The command passed to git-cmd.exe is executed but the PAUSE command doesn't execute until I type EXIT command manually in the 'Command Prompt' window.
I've also tried a simple DIR command instead of rake build but the same issue still occurs:
"%GitDirectory%/git-cmd.exe" DIR
PAUSE
Thanks to the suggestions, the issue was resolved by passing EXIT command to git-cmd.exe as follows:
#ECHO OFF
SET "PATH=C:\Ruby26-x64\bin;C:\Program Files\nodejs;%PATH%"
SET "CurrentDirectory=%CD%"
CD /D "%UserProfile%\AppData\Local\GitHub\PortableGit_*\"
SET "GitDirectory=%CD%"
CD /D "%CurrentDirectory%"
"%GitDirectory%/git-cmd.exe" "CALL rake build & EXIT"
PAUSE
There was a useful conversation between me and someone else (I think his name was mony) but it was deleted, don't know why?!
He sent me this link with the difference between & and && between commands:
https://stackoverflow.com/a/25344009/9586127
The & before the EXIT command in order to execute both commands independent on result of the first one. If && is used, the EXIT command will be executed only if the first command succeeded (exit code 0).
In addition, he told me a way to replace the lines 3:6 by one line to be:
FOR /D %%I IN ("%LocalAppData%\GitHub\PortableGit_*") DO SET "GitDirectory=%%I"

batch windows command written in prompt but no execution

To avoid to repeat a task too often, I am setting up a batch file (in WINDOWS 10). It opens several CMD PROMPT to a specific Directory and launch a command.
For one case, I want the CMD PROMPT to open, to go to the specific directory and to set the COMMAND in the PROMPT without launching it. Then I'd just have to click on ENTER to launch that command whenever I want later on.
Here is my code:
setlocal ENABLEEXTENSIONS
set CordovaProjPath="C:\MyPath\"
start cmd /k "cd /d %CordovaProjPath% && cordova build android"
With this code it launches the command "cordova build android".
If I go with start cmd /k "cd /d %JCACordovaProjPath% instead of start cmd /k "cd /d %JCACordovaProjPath% && cordova build android" it gives me the PROMPT with: "C:\MyPath>", I'd like to write: "cordova build android" behind it without launching the command.
Any idea?
To provide repeatable execution (as mentioned in comments) you can put the relevant commands in a loop with a "quit" option:
#Echo Off
setlocal
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
:loop
Cd /D %CordovaProjPath%
Echo %CommandToRun%
set QUIT=
set /p QUIT=Press ENTER to run command or 'Q' to quit:
if /i "%QUIT%" == "Q" goto :eof
%CommandToRun%
goto :loop
Unlike the original, this runs the target command in the same command-window as the repeating loop. Depending on what the command in question does, this may be more attractive (less windows popping-up). However, some commands may cause the main window to close; if this is the case, you can revert to running the command in its own window in one of two different ways. In each case, replace the line:
...
%CommandToRun%
...
Run in own window and remain open
...
start "%CommandToRun%" /wait cmd /k %CommandToRun%
...
Using /k will leave the command-prompt window open after the target command has run -- this may be appropriate if you need to see the output of the command and it does not have its own pause.
Run in own window then close
...
start "%CommandToRun%" /wait cmd /c %CommandToRun%
...
Using /c will mean the command-prompt will close after the target command has run. This may be appropriate if you do not need to see the output of the command, or if it has its own pause.
Would something like this do you:
#Echo Off
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
Start "%CommandToRun%" Cmd /K "Cd /D %CordovaProjPath%&Echo %CommandToRun%&Pause>Nul&%CommandToRun%"
Below is an alternative which may allow for your alternative double-quoting method:
#Echo Off
Set CordovaProjPath="C:\MyPath"
Set CommandToRun="cordova build android"
Start %CommandToRun% Cmd /K "(Cd /D %CordovaProjPath%)&(Echo %CommandToRun%)&(Pause>Nul)&(%CommandToRun%)"

batch file: detect when 2nd command prompt closes?

I'm using a batch file to run a bunch of selenium automation through maven.
When the automation starts it opens a new command prompt to run the maven command:
start cmd /c mvn site surefire-report:report -Dtest=uk.co.......
When the maven command prompt closes I then ask the person that's running it to press 1 in the main command prompt so it can copy the test results to a shared location.
Sometimes people are forgetting to press 1 and just closing everything so it fails to copy, I was wondering if there is a way I can detect when the maven command prompt closes and then automatically copy the results?
start "" /wait cmd /c mvn site surefire-report:report -Dtest=uk.co.......
Or if the new cmd window is not needed, directly call cmd /c .... or, if mvn is a batch file, use call mvn ....
In any of the cases, the caller will stop its execution until the called process ends.
You may use a file to indicate that maven command is active: create the file before run maven command and delete it when maven ends. In the main program, enter a wait cycle until the indicator file disappear:
rem Create the indicator file:
echo X > mavenActive.txt
rem Start maven and delete the indicator file at end:
start cmd /c mvn site surefire-report:report -Dtest=uk.co....... ^& del mavenActive.txt
rem Wait for the indicator file disappear:
:wait
rem Insert a delay so not use too much cpu:
timeout /t 20 /nobreak
if exist mavenActive.txt goto wait
rem Copy maven results here:
copy ...
If there are several simultaneous maven processes, you may use a different number for each indicator file and then wait for all of them with if exist mavenActive*.txt goto wait

How to prevent a bat file exiting early?

I'm running a maven plugin (this is just a new process) as part of a bat file.The plugin command causes the bat file to exit
so the subsequent commands do not run. Is there a command or some other way to prevent the
bat file quitting too soon ?
Here is the bat file :
ECHO Updating Version
mvn versions:set -DnewVersion=1.2
ECHO this echo does not occur
Perhaps I could use the 'call' command as referenced in How do you stop a Windows Batch file from exiting early? but I would like to run all of the code within one bat file.
If mvn is a batch file, then you need to precede it with a call, or else it will terminate on that line.
I would use:
set /p v=Press enter to exit
At the end of your program. This will wait for the user to interact before exiting.
Use the PAUSE command.
Pause /?
Example:
#Echo off
Echo hello world
Pause >NUL
exit

Resources