Run maven tests in a DOS batch file - maven

I am new to scripting. Basically I want to run maven tests and then execute other commands once they're finished (e.g. Write a message to a file, or email the results to myself, etc. For simplicity, let's say I just want to write DONE to "C:/results" file in this case). This is the script that I have:
mvn test
echo "DONE" > C:/results
The problem is, the second line (echo) never executes, because first one (mvn test) never seems to finish, even though I can see from the output that running the test finished.
How can I change the script to execute the rest of the commands once execution of first line (mvn test) finishes?

If you are on windows you have to be aware that mvn itself is .bat file which means you have to do the following: call mvn test
You need to do the call... cause the mvn.bat is running and ended at the end of mvn.bat. This is the way it is in Windows.

Related

How to determine what gradle command was running

I want to measure time of execution for different gradle command so I have to know what exactly command was run.
For example when I execute from terminal
./gradlew clean
If I execute
./gradlew assembleDebug
desired result is "assembleDebug"
I know I can get list of tasks with project.tasks, but it will give me only list of tasks which my command will execute, without command that started them all.
Is there any option to determine that "first" command?
I figured out how that should work
project.gradle.startParameter.taskNames
gives list of task names. For example if I run multiple commands like
./gradlew app:clean app:cleanBuildCache
it will contain both commands.
Is is possible to cast it for string directly
project.gradle.startParameter.taskNames.toString()
So I will have
[app:clean, app:cleanBuildCache]
With single command like
./gradlew app:clean
I receive
[app:clean]
and that was what I searched for

ControlM job calling unix script fails repeatatively

I have a controlM job which calls a shell script that takes 4 command line parameters. The command is below:
sh /appl/Script/Script1.sh ABC /appl/Landing SV_SID_NormalisedEvent_* Y
The 3rd parameter (SV_SID_NormalisedEvent_*) in the command line is a file wildcard/pattern for which the script looks for in the path provided in the second parameter (/appl/Landing).
This job was running fine to date when it aborted for one specific corrupt file: SV_SID_NormalisedEvent_20150810_151805.csv.gz. We have handled this failure manually by ignoring this file and forced ok job
Since then whenever this job is triggered during daily runs it always fires command as below and fails. Somehow the 3rd parameter is always passed as a specific file rather than the wildcard:
sh /appl/Script/Script1.sh ABC /appl/Landing SV_SID_NormalisedEvent_20150810_151805.csv SV_SID_NormalisedEvent_20150810_151805.csv.gz Y
The correct command output when the job was running fine is as below:
sh /appl/Script/Script1.sh ABC /appl/Landing 'SV_SID_NormalisedEvent*' Y
Any pointers to this issue? The above command output is from sysout file created during each run.
We have handled this failure manually by ignoring this file and forced
ok job
It sounds like perhaps the file has not been cleaned up and is being found every time. Is that possible?
The job will run with the command line that is defined in if this still contains the proper parameter SV_SID_NormalisedEvent_*.
I can't think of any other reason you are seeing this behaviour.

How to execute next batch windows command if previous execution fails?

I am trying to execute multiple windows batch commands in jenkins one after the other. The problem is that if any the of the project/build fails, it never gonna execute next windows batch commands.
My question is How to execute next windows batch command if previous execution fails?
Help me with this regard.
When you say "multiple windows batch commands", do you mean:
Multiple Execute Windows Batch Command build steps
Or multiple lines of commands within a single build step?
If configuring multiple build steps, you just need to make sure that the last command of the build step does not return anything other than 0. You can do this by adding either of the following as the very last statement in your build step:
either exit /b 0
or echo "All done"
As for multiple lines within the same build step, default implementation of Execute Windows Batch Command does not break if one line/statement fails (which is different from default Execute Shell implementation). As long as the very last statement returns 0, the build step will not fail, and any lines failing in between does not matter.
Once again, you can reference to the above list to make sure that the last line always returns 0
Re "if any the of the project/build fails"
Do you mean "if any of the batch commands fails"?
See Conditional BuildStep Plugin.
In your job config scroll down to:
Build
[ For each command that can fail Add build step at the bottom of this Build section ]
Conditional step (single)
Run? | Not
! | Execute Windows batch commands
Commands | ... your commands ...
[ Click Advanced... ]
On evaluation failure | Fail the build
Builder | Set the build result
Result | Success
Or add just one Conditional step (single) section and write all of your commands into:
Commands | ... your commands ...
Or maybe Conditional step (multiple) is the way to go for you. I haven't used this so far, so I'm not much of a help there.

Jenkins skips build shell command

I've made freestyle Jenkins Job. In the configurations I've added Build step execute shell with simple command echo "HELLO WORLD". When I build the job this shell command I think is never executed.I got no HELLO WORLD output there are also no shell errors and such. Can you tell me possible reasons why this is happening.
What kind of shell are you using?
I've had the same kind of problem with tcsh - a command was never executed (not even echoed with the -x option). As it turned out, it was because there was no "newline" character after that command, and with tcsh that seems to be a problem.

How/When does Execute Shell mark a build as failure in Jenkins?

The horror stories I found while searching for an answer for this one...
OK, I have a .sh script which pretty much does everything Jenkins supposed to do:
checks out sources from SVN
build the project
deploys the project
cleans after itself
So in Jenkins I only have to 'build' the project by running the script in an Execute Shell command.
The script is ran (the sources are downloaded, the project is build/deploy) but then it marks the build as a failure:
Build step 'Execute shell' marked build as failure
Even if the script was successfully ran! I tried closing the script with:
exit 0 (still marks it as failure)
exit 1 (marks it as failure, as expected)
no exit command at all (marks it as failure)
When, how and why does Execute Shell mark my build as a failure?
First things first, hover the mouse over the grey area below. Not part of the answer, but absolutely has to be said:
If you have a shell script that does "checkout, build, deploy" all by itself, then why are you using Jenkins? You are foregoing all the features of Jenkins that make it what it is. You might as well have a cron or an SVN post-commit hook call the script directly. Jenkins performing the SVN checkout itself is crucial. It allows the builds to be triggered only when there are changes (or on timer, or manual, if you prefer). It keeps track of changes between builds. It shows those changes, so you can see which build was for which set of changes. It emails committers when their changes caused successful or failed build (again, as configured as you prefer). It will email committers when their fixes fixed the failing build. And more and more. Jenkins archiving the artifacts also makes them available, per build, straight off Jenkins. While not as crucial as the SVN checkout, this is once again an integral part of what makes it Jenkins. Same with deploying. Unless you have a single environment, deployment usually happens to multiple environments. Jenkins can keep track of which environment a specific build (with specific set of SVN changes) is deployed it, through the use of Promotions. You are foregoing all of this. It sounds like you are told "you have to use Jenkins" but you don't really want to, and you are doing it just to get your bosses off your back, just to put a checkmark "yes, I've used Jenkins"
The short answer is: the exit code of last command of the Jenkin's Execute Shell build step is what determines the success/failure of the Build Step. 0 - success, anything else - failure.
Note, this is determining the success/failure of the build step, not the whole job run. The success/failure of the whole job run can further be affected by multiple build steps, and post-build actions and plugins.
You've mentioned Build step 'Execute shell' marked build as failure, so we will focus just on a single build step. If your Execute shell build step only has a single line that calls your shell script, then the exit code of your shell script will determine the success/failure of the build step. If you have more lines, after your shell script execution, then carefully review them, as they are the ones that could be causing failure.
Finally, have a read here Jenkins Build Script exits after Google Test execution. It is not directly related to your question, but note that part about Jenkins launching the Execute Shell build step, as a shell script with /bin/sh -xe
The -e means that the shell script will exit with failure, even if just 1 command fails, even if you do error checking for that command (because the script exits before it gets to your error checking). This is contrary to normal execution of shell scripts, which usually print the error message for the failed command (or redirect it to null and handle it by other means), and continue.
To circumvent this, add set +e to the top of your shell script.
Since you say your script does all it is supposed to do, chances are the failing command is somewhere at the end of the script. Maybe a final echo? Or copy of artifacts somewhere? Without seeing the full console output, we are just guessing.
Please post the job run's console output, and preferably the shell script itself too, and then we could tell you exactly which line is failing.
Simple and short answer to your question is
Please add following line into your "Execute shell" Build step.
#!/bin/sh
Now let me explain you the reason why we require this line for "Execute Shell" build job.
By default Jenkins take /bin/sh -xe and this means -x will print each and every command.And the other option -e, which causes shell to stop running a script immediately when any command exits with non-zero (when any command fails) exit code.
So by adding the #!/bin/sh will allow you to execute with no option.
In my opinion, turning off the -e option to your shell is a really bad idea. Eventually one of the commands in your script will fail due to transient conditions like out of disk space or network errors. Without -e Jenkins won't notice and will continue along happily. If you've got Jenkins set up to do deployment, that may result in bad code getting pushed and bringing down your site.
If you have a line in your script where failure is expected, like a grep or a find, then just add || true to the end of that line. That ensures that line will always return success.
If you need to use that exit code, you can either hoist the command into your if statement:
grep foo bar; if [ $? == 0 ]; then ... --> if grep foo bar; then ...
Or you can capture the return code in your || clause:
grep foo bar || ret=$?
I 've tried all mentioned options (even changing sh to bash without -xe params), the only one option worked for me is:
<command-which-returns-not-zero> || exit 0
Plain and simple:
If Jenkins sees the build step (which is a script too) exits with non-zero code, the build is marked with a red ball (= failed).
Why exactly that happens depends on your build script.
I wrote something similar from another point-of-view but maybe it will help to read it anyway:
Why does Jenkins think my build succeeded?
So by adding the #!/bin/sh will allow you to execute with no option.
It also helped me in fixing an issue where I was executing bash script from Jenkins master on my Linux slave. By just adding #!/bin/bash above my actual script in "Execute Shell" block it fixed my issue as otherwise it was executing windows git provided version of bash shell that was giving an error.
Try and always find where exactly its failing by adding the following line into your "Execute shell" Build step.
#!/bin/sh -xe
By adding the -x you will print each and every command that ran (including the lines from embedded scripts) and that will help in spotting the root cause.
Removing the -e option i.e. running #!/bin/sh will allow you to execute with no option, which is really a bad idea as Bryan explained well in one of the answers.
The problem is with no option Jenkins will ignore errors and continue execution of subsequent steps (if there are any) which will leave your process in an consistent state. If this is for a production build or deployment, that may have a bad impact.
Once you find the problem area, run the same failing command from the directory as jenkins-user manually, to get to the exact error/rootcause.
In Jenkins ver. 1.635, it is impossible to show a native environment variable like this:
$BUILD_NUMBER or ${BUILD_NUMBER}
In this case, you have to set it in an other variable.
set BUILDNO = $BUILD_NUMBER
$BUILDNO

Resources