java 如何发送 Maven 构建的电子邮件通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1243326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:47:06  来源:igfitidea点击:

How to send out email notification for Maven build

javamaven-2buildnotifications

提问by moonese

Is there a simple way to send out email notifications in Maven for each build without outside CI tools, just like Ant?

有没有一种简单的方法可以在 Maven 中为每个构建发送电子邮件通知,而无需外部 CI 工具,就像 Ant 一样?

采纳答案by Robert Munteanu

If CI is not an option I would use a simple script wrapper:

如果 CI 不是一个选项,我将使用一个简单的脚本包装器:

mvn install 2>&1 | tee build.log && cat build.log | mail -s 'Maven build output' [email protected] && rm -f build.log


If you do decide to use a CI tool, I would strongly recommend Hudson. The installation pageshows how easy it is to run. Continuous integration serversounds pompous and enterprisey, but Hudson is dead-simple.

如果您决定使用 CI 工具,我强烈推荐 Hudson。在安装页面显示了它是多么容易运行。持续集成服务器听起来很浮夸,很有进取心,但 Hudson 非常简单。

回答by Rich Seller

I'd strongly recommend using a CI tool to manage this for you, I personally like to configure which emails are to be sent on the build to avoid spamming. For example only notify when the build starts failing, or starts working again, not on every failure.

我强烈建议您使用 CI 工具来管理它,我个人喜欢配置要在构建中发送的电子邮件以避免垃圾邮件。例如,仅在构建开始失败或再次开始工作时通知,而不是在每次失败时通知。

If you're sure this is the right approach, you can use the maven-changes-pluginto send an email on each build. You can customise the mail template with velocity, and bind the execution of the goals to an appropriate phase so it is sent when you want it.

如果您确定这是正确的方法,您可以使用maven-changes-plugin在每次构建时发送电子邮件。您可以使用 velocity 自定义邮件模板,并将目标的执行绑定到适当的阶段,以便在您需要时发送。

I'd also put the configuration in a profile so it is sent when you want it to be (i.e. when the profile is active).

我还会将配置放在一个配置文件中,以便在您需要时发送它(即当配置文件处于活动状态时)。

The configuration looks something like this:

配置如下所示:

<profiles>
  <profile>
    <id>notify</id>
    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-changes-plugin</artifactId>
          <executions>
            <execution>
              <!--send an email in the install phase, 
                could be changed depending on your needs-->
              <phase>install</phase>
              <goals>
                <goal>announcement-mail</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <smtpHost>mail.yourhost.com</smtpHost>
            <smtpPort implementation="java.lang.Integer">25</smtpPort>
            <toAddresses>
              <toAddress implementation="java.lang.String">
                [email protected]</toAddress>
              <toAddress implementation="java.lang.String">
                 [email protected]</toAddress>
            </toAddresses>
            <!--using a custom velocity template in 
              src/main/resources/mailTemplate/announcement.vm -->
            <template>announcement.vm</template>
            <templateDirectory>mailTemplate</templateDirectory>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

回答by Markus W Mahlberg

Though the question is a bit older, it might be possible that someone still needs a different solution: I have created a plugin for just that purpose. Have a look at the plugin's page at github.com. Multipart-alternative MimeMessages aren't implemented (have a look at the FAQfor the reasons), as well as a decent templating, but as long as you simply want to send a text mail with attachements it should do the trick.

虽然这个问题有点老,但可能有人仍然需要不同的解决方案:我为此目的创建了一个插件。在 github.com 上查看插件页面。Multipart-alternative MimeMessages 没有实现(查看常见问题解答的原因),以及一个不错的模板,但只要你只是想发送带有附件的文本邮件,它应该可以解决问题。

I plan to submit it to mojo.codehaus.org, hence the gId. For now, you have to compile it yourself - sorry for that inconvenience.

我打算将它提交到 mojo.codehaus.org,因此是 gId。现在,您必须自己编译它 - 很抱歉给您带来不便。

回答by Innokenty

There is also a highly customizable Postman mail pluginwhich gives you options to send test results or even an arbitrary html specifying a source file. Here'sa good thread explaining how it should be configured.

还有一个高度可定制的Postman 邮件插件,它为您提供发送测试结果的选项,甚至是指定源文件的任意 html。这是一个很好的线程,解释了它应该如何配置。

回答by ire_and_curses

I don't use Maven, but I think you can do this with a plugin.

我不使用 Maven,但我认为您可以使用plugin来做到这一点。