com.github.lucapino.jira.ReleaseNotesMailMojo Maven / Gradle / Ivy
/*
* Copyright 2013-2107 Luca Tagliani.
*
* 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.
*/
package com.github.lucapino.jira;
import com.github.lucapino.jira.helpers.ProjectJavamailMailSender;
import com.github.lucapino.jira.model.MailSender;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import org.apache.maven.model.Developer;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.codehaus.plexus.mailsender.MailMessage;
import org.codehaus.plexus.mailsender.MailSenderException;
import org.codehaus.plexus.util.IOUtil;
/**
* Goal that generates release notes based on a version in a JIRA project.
*
* @author Luca Tagliani
*/
@Mojo(name = "mail-release-notes")
@Execute(goal = "generate-release-notes")
public class ReleaseNotesMailMojo extends AbstractJiraMojo {
/**
* Possible senders.
*/
@Parameter(property = "project.developers", required = true, readonly = true)
private List from;
/**
* The id of the developer sending the announcement mail. Only used if the
* mailSender
* attribute is not set. In this case, this should match the id of one of
* the developers in the pom. If a matching developer is not found, then the
* first developer in the pom will be used.
*/
@Parameter
private String fromDeveloperId;
/**
* Mail content type to use.
*/
@Parameter(defaultValue = "text/plain", required = true)
private String mailContentType;
/**
* Defines the sender of the announcement email. This takes precedence over
* the list of developers specified in the POM. if the sender is not a
* member of the development team. Note that since this is a bean type, you
* cannot specify it from command level with
* -D
. Use
* -Dchanges.sender='Your Name <you@domain>'
instead.
*/
@Parameter
private MailSender mailSender;
/**
* Defines the sender of the announcement. This takes precedence over both
* ${changes.mailSender} and the list of developers in the POM.
*
* This parameter parses an email address in standard RFC822 format, e.g.
* -Dchanges.sender='Your Name <you@domain>'
.
*/
@Parameter
private String senderString;
/**
* The password used to send the email.
*/
@Parameter
private String smtpPassword;
/**
* Smtp Server.
*/
@Parameter(required = true)
private String smtpHost;
/**
* Port.
*/
@Parameter(defaultValue = "25", required = true)
private int smtpPort;
/**
* If the email should be sent in SSL mode.
*/
@Parameter(defaultValue = "false")
private boolean sslMode;
/**
* Subject for the email.
*/
@Parameter(defaultValue = "[ANNOUNCEMENT] - ${project.name} ${project.version} released", required = true)
private String subject;
/**
* Template file
*/
@Parameter(property = "templateFile")
File templateFile;
/**
* Target file
*/
@Parameter(property = "targetFile", defaultValue = "${project.build.directory}/releaseNotes.vm", required = true)
File targetFile;
/**
* Recipient email address.
*/
@Parameter
private List toAddresses;
/**
* Recipient cc email address.
*
* @parameter
*/
private List ccAddresses;
/**
* Recipient bcc email address.
*/
@Parameter
private List bccAddresses;
/**
* The username used to send the email.
*/
@Parameter
private String smtpUsername;
private final ProjectJavamailMailSender mailer = new ProjectJavamailMailSender();
@Override
public void doExecute() throws Exception {
// Run only at the execution root
if (runOnlyAtExecutionRoot && !isThisTheExecutionRoot()) {
getLog().info("Skipping the announcement mail in this project because it's not the Execution Root");
} else {
// retrieve the announcement crated at the generate-release-notes goal
ConsoleLogger logger = new ConsoleLogger(Logger.LEVEL_INFO, "base");
if (getLog().isDebugEnabled()) {
logger.setThreshold(Logger.LEVEL_DEBUG);
}
mailer.enableLogging(logger);
mailer.setSmtpHost(smtpHost);
mailer.setSmtpPort(smtpPort);
mailer.setSslMode(sslMode);
if (smtpUsername != null) {
mailer.setUsername(smtpUsername);
}
if (smtpPassword != null) {
mailer.setPassword(smtpPassword);
}
mailer.initialize();
if (getLog().isDebugEnabled()) {
getLog().debug("fromDeveloperId: " + fromDeveloperId);
}
if (targetFile.isFile()) {
getLog().info("Connecting to Host: " + smtpHost + ":" + smtpPort);
sendMessage();
} else {
throw new MojoExecutionException("Announcement template " + targetFile + " not found...");
}
// create the mail
// send the mail
}
}
/**
* Send the email.
*
* @throws MojoExecutionException if the mail could not be sent
*/
protected void sendMessage()
throws MojoExecutionException {
String email = "";
final MailSender ms = getActualMailSender();
final String fromName = ms.getName();
final String fromAddress = ms.getEmail();
if (fromAddress == null || fromAddress.equals("")) {
throw new MojoExecutionException("Invalid mail sender: name and email is mandatory (" + ms + ").");
}
if (toAddresses == null && bccAddresses == null && ccAddresses == null) {
throw new MojoExecutionException("Invalid mail recipients: a recipients (to, cc or bcc) is mandatory (" + ms + ").");
}
getLog().info("Using this sender for email announcement: " + fromAddress + " < " + fromName + " > ");
try {
MailMessage mailMsg = new MailMessage();
mailMsg.setSubject(subject);
mailMsg.setContent(IOUtil.toString(new FileInputStream(targetFile), "UTF-8"));
mailMsg.setContentType(this.mailContentType);
mailMsg.setHeader("Content-Transfer-Encoding", "quoted-printable");
mailMsg.setFrom(fromAddress, fromName);
if (toAddresses != null) {
final Iterator it = toAddresses.iterator();
while (it.hasNext()) {
email = it.next().toString();
getLog().info("Sending mail to " + email + "...");
mailMsg.addTo(email, "");
}
}
if (ccAddresses != null) {
final Iterator it2 = ccAddresses.iterator();
while (it2.hasNext()) {
email = it2.next().toString();
getLog().info("Sending cc mail to " + email + "...");
mailMsg.addCc(email, "");
}
}
if (bccAddresses != null) {
final Iterator it3 = bccAddresses.iterator();
while (it3.hasNext()) {
email = it3.next().toString();
getLog().info("Sending bcc mail to " + email + "...");
mailMsg.addBcc(email, "");
}
}
mailer.send(mailMsg);
getLog().info("Sent...");
} catch (IOException ioe) {
throw new MojoExecutionException("Failed to send email.", ioe);
} catch (MailSenderException e) {
throw new MojoExecutionException("Failed to send email < " + email + " >", e);
}
}
/**
* Returns the identify of the mail sender according to the plugin's
* configuration:
*
* - if the mailSender parameter is set, it is returned
* - if no fromDeveloperId is set, the first developer in the
* list is returned
* - if a fromDeveloperId is set, the developer with that id is
* returned
* - if the developers list is empty or if the specified id does not
* exist, an exception is thrown
*
*
* @return the mail sender to use
*
* @throws MojoExecutionException if the mail sender could not be retrieved
*/
protected MailSender getActualMailSender()
throws MojoExecutionException {
if (senderString != null) {
try {
InternetAddress ia = new InternetAddress(senderString, true);
return new MailSender(ia.getPersonal(), ia.getAddress());
} catch (AddressException e) {
throw new MojoExecutionException("Invalid value for change.sender: ", e);
}
}
if (mailSender != null && mailSender.getEmail() != null) {
return mailSender;
} else if (from == null || from.isEmpty()) {
throw new MojoExecutionException(
"The section in your pom should not be empty. Add a entry or set the "
+ "mailSender parameter.");
} else if (fromDeveloperId == null) {
final Developer dev = (Developer) from.get(0);
return new MailSender(dev.getName(), dev.getEmail());
} else {
final Iterator it = from.iterator();
while (it.hasNext()) {
Developer developer = (Developer) it.next();
if (fromDeveloperId.equals(developer.getId())) {
return new MailSender(developer.getName(), developer.getEmail());
}
}
throw new MojoExecutionException(
"Missing developer with id '" + fromDeveloperId + "' in the section in your pom.");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy