How to run script in mvn as test - maven

I would like to have the following scenario in Maven/Jenkins:
Run test scripts (bash/shell)
when the script exited with a problem (an error), then the Maven build on Jenkins should be on UNSTABLE and not FAILURE status
Question: How can I do it?

You can run scripts in Maven using the Exec Maven Plugin and its exec goal.
If you want to run the script during the test phase, then you can bind an execution of the plugin to it as following:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>run-test-script</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable><!-- configure here your script .sh/.cmd --> </executable>
<arguments>
<argument><!-- configure here arguments, if any --></argument>
</arguments>
<workingDirectory><!-- configure here PWD, if required --></workingDirectory>
</configuration>
</plugin>
</plugins>
</build>
Note that you can also configure different successful exit codes via the successCodes configuration entry.
If the script fails, then the build will fail. However, you can change this behavior on the Jenkins build via the Jenkins Text Finder Plugin and configure it as Post-build Action:
You should set-up a regular expression which could be found as part of the Maven build output on Jenkins. As example, the regex .*Script Failed.* would match the string Script Failed printed by the script in such a case. So the build will actually fail, however we can change its status on Jenkins (but not on Maven)
You should check the option Unstable if found which will convert the status of the build from FAILED to UNSTABLE
As per documentation of the Unstable if found option:
Use this option to set build unstable instead of failing the build.
You can see an example of such a configuration in the image below:
As such, you would have a script executed in the test phase as you desired, the Maven build would fail if the script did so but the Jenkins build would change its status according to your configuration of the Text Finder plugin.
Also note: if you want Maven not to fail in case the script did, you can play with the successCodes as mentioned above and still make the Jenkins build change its status to UNSTABLE according to the same configuration of the Text Finder plugin. Hence different combinations are possible.

Related

Why is maven throwing an error in exec-maven-plugin for a specific dump

I am using the exec-maven-plugin and facing a very weird problem.
I have the following code
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<id>import database </id>
<phase>generate-test-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>imp</executable>
<commandlineArgs>user/user FULL=Y FILE=db_baseline.dmp</commandlineArgs>
<workingDirectory>${project.basedir}/src/test/resources/</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
When I run this, it runs to completion and shows me that the database is importing (even logging to database, I can see all the data) but the plugin is failing with the following error
[ERROR] Failed to execute goal
org.codehaus.mojo:exec-maven-plugin:1.5.0:exec (import database) on
project xxxxx: Command execution failed.: Process exited with an
error: 3 (Exit value: 3) -> [Help 1]
What's even more weird, if I use a different dump file then I don't see the error. Why is that? What could be different or wrong about this dump file that maven is throwing errors?
Do you get the same error if you run the program on the command line without Maven? Exit value: 3 means the script used is returning a code of '3' to Maven, specifically the exec plugin. By default, the exec:exec goal expects the "return of 0 means success, non-zero means failed" conventions typically honored by scripts in a Unix world.
If the imp executable doesn't honor this convention, use the <successCodes> configuration parameter to list which codes indicate success for this executable. The plugin will check the list to decide if it should fail the build.
As to why this file returns the code 3 but others don't - Maven is just passing on the value imp returns. You'd have to examine the file contents and the way the script runs to determine what the differences are. I doubt it's anything to do with Maven specifically.

What does "echo" mean when it comes to using Maven?

I'm currently learning how to use Maven and have encountered a term called "echo". I was just wondering what it actually means?
There are few important concepts related to Maven Lifecycles, which
are worth to mention:
1) When a phase is called via Maven command, for example mvn compile,
only phases up to and including that phase will execute.
2) Different maven goals will be bound to different phases of Maven
lifecycle depending upon the type of packaging (JAR / WAR / EAR).
In the following example, we will attach maven-antrun-plugin:run goal
to few of the phases of Build lifecycle. This will allow us to echo
text messages displaying the phases of the lifecycle.
echo is an ant task which allow to print messages to console (system.out)
This makes sense when using maven-antrun-plugin which allow to execute ant tasks in a maven build.
It can be used to print some maven properties during the build as there is no built-in way to output value to console in maven.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>${maven.antrun.plugin.version}</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Configuration properties :</echo>
<echo>service.endpoint=${service.endpoint}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
It means to use echo tag in ant script executed by maven-antrun-plugin:run. Nothing directly related to maven itself.

Use maven-exec-plugin to run command line

I want to use maven-exec-plugin to run command line (cmd) for converting a Markdown file to a PDF file using Pandoc.
To do that manually, I've executed these commands:
pandoc ReadMe.md -o ReadMe.html
pandoc ReadMe.html --latex-engine=xelatex -o ReadMe.pdf
I wasn't able to run that in one command, pandoc giving weird error! But this is another problem...
I've added this to my pom file using other sample found on the web but without success.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>pandoc</id>
<phase>generate-pdf</phase>
<configuration>
<executable>cmd</executable>
<workingDirectory></workingDirectory>
<arguments>
<argument>/C</argument>
<argument>pandoc</argument>
<argument>README.md</argument>
<argument>-o</argument>
<argument>README.html</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I'm not a maven guru and help is appreciate!
The defined phase, <phase>generate-pdf</phase>, is not a maven phase, hence Maven didn't bind the execution to its workflow.
You should bind it to a standard Maven phase, depending on your need. Try <phase>package</phase> for instance, it will be executed nearly at the end of your build.
The id element of a plugin execution is free text, you can type the id you want, it will appear as part of the build output in curved brackets after the plugin and goal name,
i.e. exec-maven-plugin:1.1:exec (pandoc)
The phase element instead must match a well known maven phase in order to attach the plugin/goal execution to it. If the phase is not well known, then Maven will simply ignore that plugin/goal execution (which is also an adopted approach, usually using the de-facto standard none as phase, to disable an inherited plugin execution, but that's a bit advanced for the scope of this question I would say).
For more details on maven phases, look at the official documentation, here.
For a full list of maven phases, here.

run calabash-android tests during Maven build

First off, I suspect this isn't going to be easy, because there isn't an entry for calabash-android in the Maven central repository and the calabash-android project doesn't provide one.
That being said, does anyone know how to manage calabash-android tests during a Maven build? Are there any plugins for this? I can't find any, but in the off chance my google-fu has failed me or there's a workaround... I'd love to hear about it.
[edit] I'm using the maven-android plugin for the apk build.
I am assuming you are familiar with the Android Maven plugin? If not, you should check it out. Here is a method that is not bulletproof, but should work:
1- Start by writing a shell script that launches the Calabash tests. This shell script, say calabash.sh, wouldn't be too complicated, just something along the lines of:
calabash-android run ../target/<app>.apk /path/to/calabash/tests
2- Launch this script once maven has finished its integration-test phase (if you are using Android Maven, that's when you run your unit tests). This is taken from this SO question:
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<id>Version Calculation</id>
<phase>validate</phase> //This occurs [after integration-test][3]
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${basedir}/scripts/calabash.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
This is not a bulletproof solution because I suspect that Maven cannot report any calabash test failures. Also, you might need to add some bash magic to make the script run until completion before Maven finishes building.

Maven generate-source ALWAYS Successful

I am trying to run a ant task in maven generate-sources phase.
However, after many non productive "successes" i've realised, that the build always succeeds regardless of anything i enter.
Here is the plugin config for the pom.xml of my module.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<configuration>
<tasks>
<fail message="Something wrong here."/>
</tasks>
</configuration>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
It still succeeds. If i place a bad ant file. Still succeeds.
Does anyone know what i could be doing wrong here?
The Compile/Clean/Install/Deploy phases all work fine. Just "generate-source" doesnt work at all.
My settings.xml file only contains Repo information
Thanks for any advice
Edit:
I've been able to narrow the error down a little.
<configuration>
<target>
<echo message="hello ant, from Maven!"/>
<echo>Maybe this will work?</echo>
</target>
</configuration>
If place that within the configuration of the plugin, Not in the nested configuration in the execution tag. and run "mvn antrun:run" I see the echos. However If i place it in the nested configuration in the execution element, it doesnt show... Is there some link missing between my mvn goal and the antrun instruction?
I don't get it. The same configuration runs outside of the executions/execution tags, but not within.
Solution *Solution* Solution
AHHH I found it!
In my pom.xml I had all my 'plugins''plugin' configured under the 'pluginManagement' .. This 'pluginManagement' configures the plugins not for this project, but for all children project. Effectively a parent default config file for all children implementing the plugin.. I simply removed the 'pluginManagement' tags and it works...
Thank heavens. I've been looking at this for a full day...
Try adding <failOnError>false</failOnError> to the execution.
Per the documentation, this parameter "specifies whether a failure in the ant build leads to a failure of the Maven build. If this value is 'true', the Maven build will proceed even if the ant build fails. If it is 'false', then the Maven build fails if the ant build fails."
This is counterintuitive to me. I would think that the default value of "true" would cause the Maven build to fail if the ant build failed, but that isn't what the docs appear to be saying.

Resources