Can we execute exec-maven-plugin before maven-surefire-plugin?

2.3k views Asked by At

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).

2

There are 2 answers

2
Tunaki On BEST ANSWER

First of all, if you have an execution of the exec-maven-plugin bound to the test phase, it is normal that this execution is performed after the one of the maven-surefire-plugin. The reason is that you're likely dealing with a project of packaging jar, which has a default binding of the Surefire Plugin to the test 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 of default-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, the testng.xml, so it would be appropriate to use the generate-test-resources phase, whose purpose is to create resources that are needed for the tests. Therefore, you just need to specify

<phase>generate-test-resources</phase>

to the execution of the exec-maven-plugin generating the testng.xml.

Then, you can use the generated testng.xml with the suiteXmlFiles element, see Using Suite XML Files

0
Sabbir Subhan On

This is how I have implemented:

  1. I have added test script/java main class, I want to execute before Cucumber Test Suite, in following folder: enter image description here

  2. 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