5

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

matt b
  • 138,234
  • 66
  • 282
  • 345
moonese
  • 483
  • 1
  • 6
  • 15

5 Answers5

4

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.

If you're sure this is the right approach, you can use the maven-changes-plugin to 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.

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">
                someones@email.com</toAddress>
              <toAddress implementation="java.lang.String">
                 someoneelse@email.com</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>
Rich Seller
  • 83,208
  • 23
  • 172
  • 177
  • Thanks for your info and recommendation. As far as my understanding, the content sent by the Maven Changes Plugin is what defined in the template. But what I want exactly is those output by maven build in the console. Since I am new to Maven, and with previous knowledge of Ant build, I just feel un-convient not able to find how to achieve this in Maven after hours of search in google. Maybe this is not just Maven desired to do. But still I want to get me out of the wonder why Maven just doesn't do this, by sending out email notifications. – moonese Aug 07 '09 at 11:12
  • I will try to use a CI tool later, and just want to know the considerations of design why Maven don't send out email notifications just like Ant does. – moonese Aug 07 '09 at 11:22
  • The problem with doing within Maven is that the build hasn't yet finished. You could use the exec plugin to fork the actual build, redirecting the output to a file, then mail that file, but you'd be best placed using an external CI tool or script to handle the mail – Rich Seller Aug 07 '09 at 11:26
4

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

mvn install 2>&1 | tee build.log && cat build.log | mail -s 'Maven build output' user@example.com && rm -f build.log

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

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
1

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

Innokenty
  • 3,001
  • 1
  • 27
  • 30
1

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 FAQ for 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.

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

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
0

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

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
  • Thanks for reply. I have searched in google and have already read the post, but the Maven Notify Plugin is not exactly what I want from the article. It's purpose is to send out email notification for 'deploy' phase, with a template in name changes.xml. But what I want is a way to send out email on each maven build, so that I can get build success / failure notifications, with contents just the same as what's in the maven console. – moonese Aug 07 '09 at 07:31