测试后如何在Maven中将Java类作为目标运行

时间:2018-09-28 15:54:30

标签: java maven

我已经创建了一个Java类来通过电子邮件发送报告。 Maven Surfire插件正在生成报告。      现在我有一个问题,就是直到项目完全运行后,测试报告才会保存。因此,如果我在@AfterSuite中调用了该Java类,则会失败。

在达到Maven目标之后,有什么方法可以运行Java类吗?

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

1 个答案:

答案 0 :(得分:3)

您可以将maven-exec插件用于java目标:

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

当您执行mvn packagemvn install时,这将在肯定执行插件执行之后的package阶段执行报告类。