Maven Goals To Execute Commands Post Test Execution

Back to Blog
Maven

Maven Goals To Execute Commands Post Test Execution

Objective – 

Recently in one of the projects we got the requirement to create the test execution summary file after parsing the TestNG emailable report once the test execution is finished. 

 

Challenge – 

We were running the test with Maven goal which generates the emailable report in target directory of the project but it does it once build is completed due to which we were not able to write the parsing code in TestNG listener.

 

Solution –  

We have written the code for parsing the emailable report (Check our other blogs for it) in a class having main() method. 

We have used below maven plugin and provided our class which contains the required code.

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>exec-maven-plugin</artifactId>

<version>1.4.0</version>

<configuration>

<mainClass>Class file containing main method</mainClass>

</configuration>

</plugin>

 

Now we can execute tests using below command

mvn -X  clean install test exec:java -Dexec.mainClass=”class file containing with main method” -Dexec.classpathScope=test -e -Dsurefire.suiteXmlFiles=path of the testng.xml file

 

IMPORTANT: Above goal will not execute if there is any test failure as it will mark the build as failed so to execute above goal even on build failure add below configuration in the surefire plugin

<configuration>

<testFailureIgnore>true</testFailureIgnore>

</configuration>

Share this post

Back to Blog