views:

945

answers:

3

Hi,

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

A: 

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

ire_and_curses
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
+1  A: 

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">
                [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>
Rich Seller
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
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
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
OK, I see. Thanks.
moonese
+3  A: 

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' [email protected] && 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
Thanks for your script and kindly words. I will try Hudson next.
moonese