Is it possible to run exec-maven-plugin before maven-surefire-plugin, what I observe during my run is maven-surefire-plugin is executing first even though the sequence in tag is second. My scenario is to execute JAVA CLASS (using exec-maven-plugin ) which generates my testng.xml and can run that using (maven-surefire-plugin).
Can we execute exec-maven-plugin before maven-surefire-plugin?
2.4k views Asked by kalyan chavali At
2
There are 2 answers
0
On
This is how I have implemented:
I have added test script/java main class, I want to execute before Cucumber Test Suite, in following folder: enter image description here
Added following in POM.xml in ...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java/BeforeSuite</source> <!-- source folder where Before Suite scripts are saved -->
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>before-test-suite-scripts</id>
<phase>generate-test-resources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>BeforeSuite.HelloBeforeSuiteScript</mainClass> <!-- <packagename>.<className> -->
</configuration>
</execution>
</executions>
</plugin>
When running mvn clean verify
, before test suite script will run prior the test suite execution.
enter image description here
First of all, if you have an execution of the
exec-maven-plugin
bound to thetest
phase, it is normal that this execution is performed after the one of themaven-surefire-plugin
. The reason is that you're likely dealing with a project of packagingjar
, which has a default binding of the Surefire Plugin to thetest
phase. This default execution is always the first one invoked, regardless of where the plugin is declared in the POM. In the logs, you will spot this execution with an id ofdefault-test
.There is a way to perform actions before the tests are run by leveraging the phases invoked before the phase
test
. In your case, your goal is to generate a test resource, thetestng.xml
, so it would be appropriate to use thegenerate-test-resources
phase, whose purpose is to create resources that are needed for the tests. Therefore, you just need to specifyto the execution of the
exec-maven-plugin
generating thetestng.xml
.Then, you can use the generated
testng.xml
with thesuiteXmlFiles
element, see Using Suite XML Files