Warm tip: This article is reproduced from serverfault.com, please click

Sending email with attachment using Maven

发布于 2015-02-10 15:28:58

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>
Questioner
Deepboy
Viewed
0
user944849 2015-02-11 23:49:05

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:

  • Maven forks a 'mvn test' build behind the scenes
  • postman:send-mail runs as part of test phase
  • surefire-report:report creates the reports

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