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

其他-使用Maven发送带有附件的电子邮件

(其他 - Sending email with attachment using Maven)

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

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

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

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

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

当你mvn surefire-report:report按照文档运行命令时Maven会运行直到test阶段(包括该阶段)的构建生命周期(文档中的关键词是“在执行生命周期阶段测试之前先执行它。”)

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

  • Maven在幕后筹集了``MVN测试''构建
  • 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。