Execute Maven-surefire-plugin twice with different arguments

2.7k views Asked by At

I am trying to run my unit tests through sure-fire plugin with two different arguments. One using jacoco to feed in test results into SonarQube and the other is to run in dynatrace. I tried putting it in two different execution tags but doesnt seem to work properly. Please help me as to what I am doing wrong? Below is the snippet from my pom.xml:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.12</version>
  <configuration>
     <argLine>${jacoco.ut.arg}</argLine>
     <argLine>-agentpath:"C:\Program Files\dynaTrace\Dynatrace 6.3\agent\lib64\dtagent.dll"=name=JavaAgent,server=localhost:9998,optionTestRunIdJava=${dtTestrunID}</argLine>
     <excludes>
       <exclude>**/at/**</exclude>
       <exclude>**/it/**</exclude>
     </excludes>
   </configuration>
</plugin>
1

There are 1 answers

0
carlspring On

You need to use <executions/>. Consider the following example:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12</version>

    <!-- You could also have the configuration tag inside the execution -->
    <configuration>
        <argLine>${jacoco.ut.arg}</argLine>
        <argLine>-agentpath:"C:\Program Files\dynaTrace\Dynatrace 6.3\agent\lib64\dtagent.dll"=name=JavaAgent,server=localhost:9998,optionTestRunIdJava=${dtTestrunID}</argLine>
        <excludes>
            <exclude>**/at/**</exclude>
            <exclude>**/it/**</exclude>
        </excludes>
    </configuration>

    <executions>
         <execution>
             <id>run-tests</id>
             <phase>test</phase>   <!-- or whatever phase you like -->
             ...
         </execution>
         <execution>
             <id>run-jacoco</id>
             <phase>test</phase>   <!-- or whatever phase you like -->
             <goals>...</goals>
             ...
         </execution>
    </executions>
</plugin>

Have a look at the Maven POM Reference:

executions: It is important to keep in mind that a plugin may have multiple goals. Each goal may have a separate configuration, possibly even binding a plugin's goal to a different phase altogether. executions configure the execution of a plugin's goals.