Use of Execution Tag in Maven

Viewed 712

What is the purpose of Execution tag in Maven? I run testng.xml using POM file, With and Without using execution tag ,i get the same output using mvn clean.

                <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
                <executions>
                    <execution>
                        <id>testngtest</id>
                        <phase>test</phase>
                    </execution>
                </executions>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${testngxml.location}/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>

Then why we are using Execution tag?

3 Answers

It is because for "maven-surefire-plugin", test is the default phase. Hence nothing got changed for it.

Change the phase to compile or other phase.

The <execution> block is required to bind plugin execution to one of Maven's lifecycles. For surefire this is not necessary, since it is bound to the test phase by default.

Note that the <execution> block can also contain a <configuration> block, so you can have multiple executions with different configurations. You can read more about it in the Maven documentation on configuring plugins.

The execution tag is most commonly used for mojos that are intended to participate in some phases of the build lifecycle. i.e you hjave configured maven-surefire-plugin to run in the testing phase of the build life cycle which is where it runs by default anyway so this has no effect. Since surefire is bound to the test phase by default, I'm not sure changing the phase in executions would have any effect. You would need to try a different plugin and try changing the execution setting on that plugin. Maybe try the one in the documentation 'maven-myquery-plugin'

See the dopcumentation on the executions tag here

Related