Added new <code>announcement:mail</code> goal to automatically send the generated announcement by email.

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@115934 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
vmassol 2004-08-07 15:49:37 +00:00
parent fddf2bc732
commit b736ddc415
7 changed files with 271 additions and 0 deletions

View File

@ -40,6 +40,17 @@
<maven:property var="announcementFile" name="maven.announcement.file"
defaultValue="${maven.gen.docs}/announcements/announcement-${versionVariable}.txt"/>
<!-- If from email address is empty, find the first non empty email
address from the POM. -->
<j:if test="${context.getVariable('maven.announcement.mail.from') == null}">
<j:forEach var="developer" items="${pom.developers}">
<j:if test="${not empty(developer.email)}">
<j:set var="maven.announcement.mail.from" value="${developer.email}"/>
<j:break/>
</j:if>
</j:forEach>
</j:if>
<!-- Ensure the directory where the announcement will be created exists -->
<ant:dirname property="announcementDir" file="${announcementFile}"/>
<ant:mkdir dir="${announcementDir}"/>
@ -49,6 +60,8 @@
<maven:property var="distributionUrl" name="maven.announcement.distributionUrl"
defaultValue="${maven.repo.remote}/${pom.groupId}/plugins"/>
<j:useBean var="stringUtils" class="org.apache.commons.lang.StringUtils"/>
</goal>
<goal name="announcement:check-version" prereqs="announcement:init">
@ -97,6 +110,13 @@
</goal>
<goal name="announcement:mail" prereqs="announcement:generate"
description="Send an email containing the release announcement">
<announcement:mail file="${announcementFile}"/>
</goal>
<!-- Define common jelly script used by different goals as a jelly tag -->
<define:taglib uri="announcement">
<define:tag name="generate">
@ -118,6 +138,36 @@
</j:file>
</define:tag>
<define:tag name="mail">
<!-- @file: Announcement file location -->
<maven:param-check value="${maven.announcement.mail.server}" fail="true">The SMTP server name must be specified using the maven.announcement.mail.server property</maven:param-check>
<maven:param-check value="${maven.announcement.mail.from}" fail="true">The From address must be specified using the maven.announcement.mail.from property or by ensuring that at least one developer in the POM has an email address specified</maven:param-check>
<maven:param-check value="${maven.announcement.mail.to}" fail="true">The To address must be specified using the maven.announcement.mail.to property</maven:param-check>
<!-- Resolve dynamic %% templates from maven.announcement.mail.subject -->
<j:set var="maven.announcement.mail.subject"
value="${stringUtils.replace(context.getVariable('maven.announcement.mail.subject'), '%VERSION%', versionVariable)}"/>
<echo>Sending mail using SMTP server "${maven.announcement.mail.server}", client ${maven.announcement.mail.client} and subject "${maven.announcement.mail.subject}"...</echo>
<echo>Sending from "${maven.announcement.mail.from}" to "${maven.announcement.mail.to}"...</echo>
<util:loadText uri="file:${file}" var="announcementContent"/>
<j:invokeStatic className="org.apache.maven.announcement.MailUtils" method="sendMail" var="result">
<j:arg type="java.lang.String" value="${maven.announcement.mail.server}"/>
<j:arg type="java.lang.String" value="${maven.announcement.mail.client}"/>
<j:arg type="java.lang.String" value="${maven.announcement.mail.from}"/>
<j:arg type="java.lang.String" value="${maven.announcement.mail.to}"/>
<j:arg type="java.lang.String" value="${maven.announcement.mail.subject}"/>
<j:arg type="java.lang.String" value="${announcementContent}"/>
</j:invokeStatic>
<j:if test="${result != 'ok'}">
<fail>Could not send message. Reason: ${result}</fail>
</j:if>
</define:tag>
</define:taglib>
</project>

View File

@ -33,3 +33,19 @@
# Stylesheet to use to generate the text announcement
maven.announcement.stylesheet.path = ${plugin.resources}/announcement.jsl
# IP address or name of the SMTP server used to send an announcement email
# maven.announcement.mail.server =
# IP address or name of the host used to log on the SMTP server
maven.announcement.mail.client = localhost
# From address to use when sending an announcement email. If not defined,
# the first email address found in the POM will be used.
# maven.announcement.mail.from =
# Comma-separated list of addresses to use when sending an announcement email.
# maven.announcement.mail.to =
# Mail subject to use when sending an announcement email.
maven.announcement.mail.subject = [ANN] ${pom.name} %VERSION% released

View File

@ -87,5 +87,15 @@
<version>20030211.142705</version>
<url>http://jakarta.apache.org/commons/jelly/libs/xml/</url>
</dependency>
<dependency>
<id>commons-lang</id>
<version>2.0</version>
<url>http://jakarta.apache.org/commons/lang/</url>
</dependency>
<dependency>
<id>commons-net</id>
<version>1.2.1</version>
<url>http://jakarta.apache.org/commons/net/</url>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,137 @@
package org.apache.maven.announcement;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.commons.net.smtp.SimpleSMTPHeader;
/* ====================================================================
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
/**
* Send an email message using Jakarta Commons Net
*
* @author Felipe Leme
*
* @version $Id: MailUtils.java,v 1.1 2004/08/07 15:49:37 vmassol Exp $
*/
public class MailUtils
{
private static final String ADDRESS_SEPARATOR = ",";
public static String sendMail(String server, String client,
String from, String to, String subject, String msg)
{
// Check arguments
if (server == null)
{
return "no smtp server configured";
}
if (client == null)
{
return "no smtp client configured";
}
if (to == null)
{
return "no to address specified";
}
if (from == null)
{
return "no from address specified";
}
if (msg == null)
{
return "no message specified";
}
if (subject == null)
{
return "no subject specified";
}
// create a SMTP connection
SMTPClient smtpClient = new SMTPClient();
System.out.println("Connecting to SMTP Server [" + server + "]");
try
{
smtpClient.connect(server);
// checks the server reply
int reply = smtpClient.getReplyCode();
if (!SMTPReply.isPositiveCompletion(reply))
{
smtpClient.disconnect();
return "SMTP server [" + server + "] refused connection.";
}
// Login
smtpClient.login(client);
// Set the sender and recipient(s)
smtpClient.setSender(from);
// parse out the to addresses
StringTokenizer st = new StringTokenizer(to.toString(),
ADDRESS_SEPARATOR);
while (st.hasMoreTokens())
{
smtpClient.addRecipient(st.nextToken().trim());
}
System.out.println("Sending message from [" + from + "] to ["
+ to + "]");
// Use the SimpleSMTPHeader class to build the header
Writer clientWriter = smtpClient.sendMessageData();
if (clientWriter == null)
{
return "Could not send data to the SMTP server";
}
PrintWriter writer = new PrintWriter(clientWriter);
SimpleSMTPHeader header = new SimpleSMTPHeader(from, to, subject);
//NOTE: if would be nice to add some Maven info here
// header.addHeaderField("Organization", "xxx");
// Write the header to the SMTP Server
writer.write(header.toString());
// Write the body of the message
writer.write(msg);
// Close the writer
writer.close();
if (!smtpClient.completePendingCommand())
{
return "Could not send the SMTP message";
}
// Logout from the e-mail server (QUIT)
smtpClient.logout();
// Close the connection
smtpClient.disconnect();
// everything is fine
return "ok";
}
catch (Exception e)
{
e.printStackTrace();
return e.getMessage();
}
}
}

View File

@ -25,6 +25,10 @@
</properties>
<body>
<release version="1.3-SNAPSHOT" date="in CVS">
<action dev="vmassol" type="add" issue="MPANNOUNCEMENT-9" due-to="Felipe Leme">
Added new <code>announcement:mail</code> goal to automatically
send the generated announcement by email.
</action>
<action dev="vmassol" type="add" issue="MPANNOUNCEMENT-11" due-to="Felipe Leme">
Added new optional <code>maven.announcement.stylesheet.path</code>property
that defines what stylesheet to use to generate the text announcement.

View File

@ -48,6 +48,12 @@
found in <code>changes.xml</code>
</td>
</tr>
<tr>
<td>announcement:mail</td>
<td>
Send the generated announcement message by email.
</td>
</tr>
</table>
</section>
</body>

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
/*
* Copyright 2001-2004 The Apache Software Foundation.
@ -71,6 +72,53 @@
<code>${plugin.resources}/announcement.jsl</code>
</td>
</tr>
<tr>
<td>maven.announcement.mail.server</td>
<td>Required for announcement:mail goal</td>
<td>
Address of the SMTP server used to send the email message.
</td>
<td>none
</td>
</tr>
<tr>
<td>maven.announcement.mail.client</td>
<td>Yes</td>
<td>
Name of the host/domain used to log on the SMTP server.
</td>
<td>localhost
</td>
</tr>
<tr>
<td>maven.announcement.mail.subject</td>
<td>Yes</td>
<td>
Subject of the announcement email message.
</td>
<td>
<code>[ANN] ${pom.name} %VERSION% released</code>
</td>
</tr>
<tr>
<td>maven.announcement.mail.from</td>
<td>Yes</td>
<td>
Sender (email address) of the announcement message.
</td>
<td>
First developer email found in the POM
</td>
</tr>
<tr>
<td>maven.announcement.mail.to</td>
<td>Required for announcement:mail goal</td>
<td>
Comma-separated list of To: address used as recipients of the announcement message.
</td>
<td>none
</td>
</tr>
</table>
</section>
</body>