ArunBharath
ArunBharath

Reputation: 153

How to Run Java class as a goal in maven after test

I have create a Java class to send a report through Email. Reports are getting generated by Maven Surfire Plugin. Now I have a problem is test reports are not getting saved until the complete run of project. So If I called that java class in @AfterSuite , it is failing.

Is there any way to run the java class after the maven goal?

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SoapOCPAutomation</groupId>
  <artifactId>SoapOCPAutomation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SoapOCPAutomation</name>
  <description>SoapOCPAutomation</description><repositories>
    <repository>
        <id>smartbear-sweden-plugin-repository</id>
        <url>http://www.soapui.org/repository/maven2/</url>
    </repository>
</repositories>
  <build>
    <sourceDirectory>src</sourceDirectory>
  <plugins>

      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18</version>
            <configuration>
             <forkCount>0</forkCount>
                <suiteXmlFiles>         
                    <suiteXmlFile>testng.xml</suiteXmlFile>

                </suiteXmlFiles>
            </configuration>
    </plugin>


    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.2</version>
      <executions>
        <execution>
          <id>send-report</id>
          <phase>package</phase>
          <goals>
            <goal>java</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <mainClass>com.acxsys.ocp.api.emt.ks.report.KSSendEmail</mainClass>
      </configuration>
    </plugin>

    </plugins>
  </build>
</project>

Upvotes: 1

Views: 1822

Answers (1)

Amila
Amila

Reputation: 5213

You can use maven-exec plugin with java goal:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.2</version>
      <executions>
        <execution>
          <id>send-report</id>
          <phase>package</phase>
          <goals>
            <goal>java</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <mainClass>your.reporting.Class</mainClass>
      </configuration>
    </plugin>
  </plugins>
</build>

When you do mvn package or mvn install, this will execute your reporting class in package phase, which comes after surefire plugin execution.

Upvotes: 4

Related Questions