Using surefire plugin and postman plugin, I am able to generate a surefire report and send email to a recipient. But the surefire report (html) is not getting attached with the email. Recipient is getting an email without the attachment. If I run the project again, email has been delivered with the attachment. Following is my pom.xml. I don't know what I am missing. Please help.
<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>
Generally, I find that it is more helpful to get a Maven build working from the command line before attempting to introduce Eclipse.
Do you have a remote repository set up (e.g. Nexus, Artifactory)? If not, it would be good to have that in place if you are going to continue to use Maven regularly. Once the remote repo exists, then you will need to configure the project's distributionManagement
element in order to publish artifacts to that repository.
Now, back to your original question. surefire-report:report
is a report goal, and runs as part of the reporting lifecycle by default. As you have it configured, it is not related to the build lifecycle in any way. In your POM, the postman
plugin is bound to the test
phase, which is part of the default lifecycle.
When you run command mvn surefire-report:report
per the documentation Maven runs the build lifecycle up to and including the test
phase. (The key phrase in the documentation is "Invokes the execution of the lifecycle phase test prior to executing itself.").
So, the order of operations when you run mvn surefire-report:report
is:
postman:send-mail
runs as part of test phasesurefire-report:report
creates the reportsNote how the last two steps are out of order. So, the first time you run the command, there are no test reports yet, and thus no attachment. The second time you run it, there are reports from the previous build that get attached.
The question for you becomes, do you plan to run this at the command line once in a while in order to send reports when someone asks for them? If so, then you may simply remove the phase
configuration from the postman plugin and use Maven command mvn surefire-report:report postman:send-mail
. This will perform the steps in the correct order.
If you want the email to happen every time (i.e. with every mvn clean install site
), you need to bind the postman:send-mail
goal to a phase that runs after the reports are generated. I would try the site
phase. If that doesn't work, then use post-site
and change the Maven command to mvn clean install post-site
.
P.S. If you're new to Maven, I highly recommend learning about the different lifecycles and the difference between a phase and a goal. You can't really use Maven effectively without that knowledge.
Thank you very much for the description. That was helpful. I want the email to be sent with an attachment every time I run the project. I included the <phase>site</phase> under the post-man plugin in my pom.xml and did run the test using the command : mvn surefire-report:report postman:send-mail. I have got the report generated but ended up with the following error: Failed to execute goal ch.fortysix:maven-postman-plugin:0.1.6:send-mail (default-cli) on project SampleExample: The parameters 'receivers', 'from' for goal ch.fortysix:maven-postman-plugin:0.1.6:send-mail are missing or invalid.
If I do mvn clean install site, my requirements are working perfectly (i.e. Report generated, email delivered with the attachment). I can see clean install commands remove the target folder and installs respective files and folders.
SORRY FOR FLOODING with comments. After reading the lifecycle of Maven, I had a good understanding. I just changed the <phase> to site (indicated earlier) and ran "mvn site". This works well. So, I dont need to specify the goal (ex: surefire-report:report) as it is being said "site" generates project's documentation. Also, goals are executed in phases which help determine the order goals get executed in.
Thanks for taking the time to read the docs I suggested. It will serve you well as you continue to use Maven.