使用Maven发送带有附件的电子邮件 [英] Sending email with attachment using Maven

查看:87
本文介绍了使用Maven发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用surefire插件和邮递员插件,我能够生成一个surefire报告并将电子邮件发送给收件人.但是surefire报告(html)没有随电子邮件一起发送.收件人收到一封没有附件的电子邮件.如果我再次运行该项目,则电子邮件已随附件一起发送.以下是我的pom.xml.我不知道我在想什么.请帮忙.

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>

推荐答案

通常,我发现在尝试引入Eclipse之前,从命令行运行Maven构建会更有用.

Generally, I find that it is more helpful to get a Maven build working from the command line before attempting to introduce Eclipse.

您是否设置了远程存储库(例如Nexus,Artifactory)?如果没有,那么如果您要继续定期使用Maven,将其设置在适当的位置将是一个很好的选择.远程存储库存在后,您将需要在以下位置配置项目的distributionManagement 元素以便将工件发布到该存储库.

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.

现在,回到您的原始问题. surefire-report:report是一个报告目标,默认情况下作为报告生命周期的一部分运行.配置完成后,它与构建生命周期完全没有关系.在您的POM中,postman插件绑定到test阶段,该阶段 是默认生命周期的一部分.

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.

根据文档运行命令mvn surefire-report:report Maven在test阶段之前一直运行构建生命周期. (文档中的关键词是在执行生命周期阶段测试之前先执行它.")

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.").

因此,运行mvn surefire-report:report时的操作顺序为:

So, the order of operations when you run mvn surefire-report:report is:

  • Maven在幕后提供了"MVN测试"构建
  • postman:send-mail作为测试阶段的一部分运行
  • surefire-report:report创建报告
  • 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.

您遇到的问题是,您是否打算不时在命令行中运行一次以便在有人要求时发送报告?如果是这样,那么您可以简单地从邮递员插件中删除phase配置并使用Maven命令mvn surefire-report:report postman:send-mail.这将按照正确的顺序执行步骤.

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.

如果您希望电子邮件每次都发生(即与每个mvn clean install site一起发生),则需要将postman:send-mail目标绑定到生成报告后运行的阶段.我会尝试site阶段.如果这不起作用,请使用post-site并将Maven命令更改为mvn clean install post-site.

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.如果您是Maven的新手,我强烈建议您学习不同生命周期以及阶段与目标之间的差异.没有这些知识,您就无法真正有效地使用Maven.

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.

这篇关于使用Maven发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆