I am having the following plugin to run a .sh
script:
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<id>deploy-bundles</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${basedir}/deploy.sh</executable>
<successCodes>
<successCode>0</successCode> <-- Not working
</successCodes>
</configuration>
</execution>
</executions>
</plugin>
which copies some folders and files to certain locations. It works. However, just in case, I want to have a fail-on-error mechanism. I already have set -e
command in my .sh
script, but I want a maven solution too. I heard that there is a tag called successCodes
, I try to incorporate it. But no luck so far. Could someone point out the correct way of doing it?
Edit: My .sh
script looks like this:
cp ../template/config.properties $component/conf
cp ../../runtime/group1/group1.mw/conf/log4j.xml $component/conf
# if the component is planning, create an additional folder called plans
if [[ $component == *".planning"* ]]
then
mkdir -p $component/plans
# copy all the plans here
cp ../../mission.planning/plans/* $component/plans
fi
where it is expected to fail in case these folders/files are not there. So, as a test, I manually change the paths above and expect it to fail. It does fail the execution process and tells me the error (since I have set -e
command in the .sh
script), however maven reports is as "success".
That's not an issue with the Exec Maven Plugin, but an issue with the handling of exit codes in the Shell script.
The successCodes
parameter is useful in situation where an executable has an exit code different than 0 for "successful execution":
Exit codes to be resolved as successful execution for non-compliant applications (applications not returning 0 for success).
The default behaviour is to consider the exit code 0 as a successful execution, and all the others as a failure,and the plugin will fail the build in that case.
In your Shell script, you have multiple commands, each of which has its own exit code. Without any extra handling, the exit code of the script itself, as a whole, is the exit code of the last command. Therefore, even if one of the command failed (so its exit code is not zero) a successful command after that will reset the script exit code to 0. You can test that by invoking the script on the command line outside of Maven, and echo the $?
variable, which contains the exit code.
As such, you need to test for the exit code of each invoked command that might fail inside your Shell. (You can also use a bit of arithmetic to accumulate each exit codes.)