使用surefire插件和postman插件,我能够生成surefire报告并将电子邮件发送给收件人。但是surefire报告(html)没有随电子邮件一起发送。收件人收到一封没有附件的电子邮件。如果我再次运行该项目,则电子邮件已随附件一起发送。以下是我的pom.xml。我不知道我在想什么。请帮忙。
<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>com.testing.example</groupId>
<artifactId>SampleExample</artifactId>
<packaging>jar</packaging>
<name>SampleExample</name>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>ch.fortysix</groupId>
<artifactId>maven-postman-plugin</artifactId>
<version>0.1.6</version>
<executions>
<execution>
<id>send_an_mail</id>
<phase>test</phase>
<goals>
<goal>send-mail</goal>
</goals>
<inherited>false</inherited>
<configuration>
<from>xxxxxxxxxx</from>
<subject>this is a test auto email sent from Eclipse using Maven</subject>
<htmlMessage>
<![CDATA[
<p>Hi, Please find attached.</p>
]]>
</htmlMessage>
<failonerror>true</failonerror>
<mailhost>smtp.gmail.com</mailhost>
<mailport>465</mailport>
<mailssl>true</mailssl>
<mailAltConfig>true</mailAltConfig>
<mailuser>xxxxxxx</mailuser>
<mailpassword>xxxxxxx</mailpassword>
<receivers>
<receiver>xxxxxxxxx</receiver>
</receivers>
<fileSets>
<fileSet>
<directory>${basedir}/target/site</directory>
<includes>
<include>**/surefire-report.html</include>
</includes>
</fileSet>
</fileSets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
通常,我发现在尝试引入Eclipse之前,从命令行运行Maven构建会更有用。
你是否设置了远程存储库(例如Nexus,Artifactory)?如果没有,那么如果你要继续定期使用Maven,将其设置在适当的位置将是一个很好的选择。远程存储库存在后,你将需要配置项目的distributionManagement
元素,以便将工件发布到该存储库。
现在,回到你的原始问题。 surefire-report:report
是报表目标,默认情况下作为报表生命周期的一部分运行。配置完成后,它与构建生命周期完全没有关系。在你的POM中,postman
插件绑定到test
阶段,这是默认生命周期的一部分。
当你mvn surefire-report:report
按照文档运行命令时,Maven会运行直到test
阶段(包括该阶段)的构建生命周期。(文档中的关键词是“在执行生命周期阶段测试之前先执行它。”)
因此,运行时的操作顺序mvn surefire-report:report
为:
postman:send-mail
作为测试阶段的一部分运行surefire-report:report
创建报告请注意最后两个步骤是如何混乱的。因此,第一次运行该命令时,尚无测试报告,因此也没有附件。第二次运行它时,会附带上一版的报告。
你遇到的问题是,你是否计划不时在命令行中运行一次以便在有人要求时发送报告?如果是这样,那么你只需phase
从 Postman 插件中删除配置,然后使用Maven命令mvn surefire-report:report postman:send-mail
。这将以正确的顺序执行步骤。
如果你希望电子邮件每次都发生(即,与一起发生mvn clean install site
),则需要将postman:send-mail
目标绑定到生成报告后运行的阶段。我会尝试的site
阶段。如果这不起作用,请使用post-site
并将Maven命令更改为mvn clean install post-site
。
PS:如果你是Maven的新手,我强烈建议你学习不同的生命周期以及阶段和目标之间的区别。没有这些知识,你将无法真正有效地使用Maven。
非常感谢您的描述。那很有帮助。我希望每次运行项目时都将带有附件的电子邮件发送出去。我在pom.xml的post-man插件下包含了<phase> site </ phase>,并使用以下命令运行了测试:mvn surefire-report:report postman:send-mail。我已经生成了报告,但最终出现以下错误:无法在项目SampleExample上执行目标ch.fortysix:maven-postman-plugin:0.1.6:send-mail(default-cli),示例:参数'receivers',目标ch.fortysix:maven-postman-plugin:0.1.6:发送邮件丢失或无效。
如果我执行mvn全新安装站点,则我的要求可以正常工作(即生成报告,随附件发送电子邮件)。我可以看到干净的安装命令删除了目标文件夹并安装了相应的文件和文件夹。
抱歉带注释。在阅读了Maven的生命周期之后,我有了一个很好的理解。我只是将<phase>更改为站点(如前所述)并运行了“ MVN站点”。这很好。因此,我不需要指定目标(例如:surefire-report:report),因为它说的是“站点”生成项目的文档。此外,目标是分阶段执行的,有助于确定执行目标的顺序。
感谢您抽出宝贵的时间阅读我建议的文档。当您继续使用Maven时,它将为您提供良好的服务。