Coverage Report - org.apache.maven.plugin.announcement.AnnouncementMailMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AnnouncementMailMojo
0%
0/97
0%
0/26
1,8
 
 1  
 package org.apache.maven.plugin.announcement;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.maven.model.Developer;
 23  
 import org.apache.maven.plugin.AbstractMojo;
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.plugin.announcement.mailsender.ProjectJavamailMailSender;
 26  
 import org.apache.maven.project.MavenProject;
 27  
 import org.codehaus.plexus.logging.console.ConsoleLogger;
 28  
 import org.codehaus.plexus.mailsender.MailSenderException;
 29  
 import org.codehaus.plexus.util.IOUtil;
 30  
 
 31  
 import java.io.File;
 32  
 import java.io.FileNotFoundException;
 33  
 import java.io.FileReader;
 34  
 import java.io.IOException;
 35  
 import java.util.Iterator;
 36  
 import java.util.List;
 37  
 
 38  
 /**
 39  
  * Goal which sends an announcement through email.
 40  
  *
 41  
  * @author aramirez@exist.com
 42  
  * @version $Id: org.apache.maven.plugin.announcement.AnnouncementMailMojo.html 816584 2012-05-08 12:33:35Z hboutemy $
 43  
  * @goal announcement-mail
 44  
  * @execute goal="announcement-generate"
 45  
  */
 46  0
 public class AnnouncementMailMojo
 47  
     extends AbstractMojo
 48  
 {
 49  
     //=========================================
 50  
     // announcement-mail goal fields
 51  
     //=========================================
 52  
 
 53  
     /**
 54  
      * @parameter expression=${project}
 55  
      * @readonly
 56  
      */
 57  
     private MavenProject project;
 58  
 
 59  
     /**
 60  
      * Smtp Server.
 61  
      *
 62  
      * @parameter
 63  
      * @required
 64  
      */
 65  
     private String smtpHost;
 66  
 
 67  
     /**
 68  
      * Port.
 69  
      *
 70  
      * @parameter default-value="25";
 71  
      * @required
 72  
      */
 73  
     private int smtpPort;
 74  
 
 75  
     /**
 76  
      * The username used to send the email.
 77  
      *
 78  
      * @parameter
 79  
      */
 80  
     private String username;
 81  
 
 82  
     /**
 83  
      * The password used to send the email.
 84  
      *
 85  
      * @parameter
 86  
      */
 87  
     private String password;
 88  
 
 89  
     /**
 90  
      * If the email should be sent in SSL mode.
 91  
      *
 92  
      * @parameter default-value="false"
 93  
      */
 94  
     private boolean sslMode;
 95  
 
 96  
     /**
 97  
      * Subject for the email.
 98  
      *
 99  
      * @parameter default-value="[ANNOUNCEMENT] - ${project.name} ${project.version} released"
 100  
      * @required
 101  
      */
 102  
     private String subject;
 103  
 
 104  
     /**
 105  
      * The id of the developer sending the announcement mail. Only used if the <tt>mailSender</tt>
 106  
      * attribute is not set. In this case, this should match the id of one of the developers in
 107  
      * the pom. If a matching developer is not found, then the first developer in the pom will be
 108  
      * used.
 109  
      *
 110  
      * @parameter expression="${changes.fromDeveloperId}"
 111  
      */
 112  
     private String fromDeveloperId;
 113  
 
 114  
     /**
 115  
      * Defines the sender of the announcement if the list of developer is empty or
 116  
      * if the sender is not a member of the development team.
 117  
      *
 118  
      * @parameter
 119  
      */
 120  
     private MailSender mailSender;
 121  
 
 122  
 
 123  
     /**
 124  
      * Recipient email address.
 125  
      *
 126  
      * @parameter
 127  
      * @required
 128  
      */
 129  
     private List toAddresses;
 130  
 
 131  
     /**
 132  
      * Possible senders.
 133  
      *
 134  
      * @parameter expression="${project.developers}"
 135  
      * @required
 136  
      * @readonly
 137  
      */
 138  
     private List from;
 139  
 
 140  
     /**
 141  
      * Directory which contains the template for announcement email.
 142  
      *
 143  
      * @parameter expression="${project.build.directory}/announcement"
 144  
      * @required
 145  
      */
 146  
     private File templateOutputDirectory;
 147  
 
 148  
     /**
 149  
      * The Velocity template used to format the announcement.
 150  
      *
 151  
      * @parameter default-value="announcement.vm"
 152  
      * @required
 153  
      */
 154  
     private String template;
 155  
 
 156  0
     private ProjectJavamailMailSender mailer = new ProjectJavamailMailSender();
 157  
 
 158  
     public void execute()
 159  
         throws MojoExecutionException
 160  
     {
 161  0
         File templateFile = new File( templateOutputDirectory, template );
 162  
 
 163  0
         ConsoleLogger logger = new ConsoleLogger( 0, "base" );
 164  
 
 165  0
         mailer.enableLogging( logger );
 166  
 
 167  0
         mailer.setSmtpHost( getSmtpHost() );
 168  
 
 169  0
         mailer.setSmtpPort( getSmtpPort() );
 170  
 
 171  0
         mailer.setSslMode( sslMode );
 172  
 
 173  0
         if ( username != null )
 174  
         {
 175  0
             mailer.setUsername( username );
 176  
         }
 177  
 
 178  0
         if ( password != null )
 179  
         {
 180  0
             mailer.setPassword( password );
 181  
         }
 182  0
         mailer.initialize();
 183  
 
 184  0
         if ( getLog().isDebugEnabled() )
 185  
         {
 186  0
             getLog().debug( "fromDeveloperId: " + getFromDeveloperId() );
 187  
         }
 188  
 
 189  0
         if ( templateFile.isFile() )
 190  
         {
 191  0
             getLog().info( "Connecting to Host: " + getSmtpHost() + ":" + getSmtpPort() );
 192  
 
 193  0
             sendMessage();
 194  
         }
 195  
         else
 196  
         {
 197  0
             throw new MojoExecutionException( "Announcement template " + templateFile + " not found..." );
 198  
         }
 199  0
     }
 200  
 
 201  
     /**
 202  
      * Send the email.
 203  
      *
 204  
      * @throws MojoExecutionException if the mail could not be sent
 205  
      */
 206  
     protected void sendMessage()
 207  
         throws MojoExecutionException
 208  
     {
 209  0
         File templateFile = new File( templateOutputDirectory, template );
 210  0
         String email = "";
 211  0
         final MailSender ms = getActualMailSender();
 212  0
         final String fromName = ms.getName();
 213  0
         final String fromAddress = ms.getEmail();
 214  0
         if ( fromAddress == null || fromAddress.equals( "" ) )
 215  
         {
 216  0
             throw new MojoExecutionException( "Invalid mail sender: name and email is mandatory (" + ms + ")." );
 217  
         }
 218  0
         getLog().info( "Using this sender for email announcement: " + fromAddress + " < " + fromName + " > " );
 219  
         try
 220  
         {
 221  0
             final Iterator it = getToAddresses().iterator();
 222  0
             while ( it.hasNext() )
 223  
             {
 224  0
                 email = it.next().toString();
 225  0
                 getLog().info( "Sending mail to " + email + "..." );
 226  0
                 mailer.send( getSubject(), IOUtil.toString( readAnnouncement( templateFile ) ), email, "", fromAddress,
 227  
                              fromName );
 228  0
                 getLog().info( "Sent..." );
 229  
             }
 230  
         }
 231  0
         catch ( IOException ioe )
 232  
         {
 233  0
             throw new MojoExecutionException( "Failed to send email.", ioe );
 234  
         }
 235  0
         catch ( MailSenderException e )
 236  
         {
 237  0
             throw new MojoExecutionException( "Failed to send email < " + email + " >", e );
 238  0
         }
 239  0
     }
 240  
 
 241  
     /**
 242  
      * Read the announcement generated file.
 243  
      *
 244  
      * @param file the file to be read
 245  
      * @return fileReader Return the FileReader
 246  
      * @throws MojoExecutionException if the file could not be found
 247  
      */
 248  
     protected FileReader readAnnouncement( File file )
 249  
         throws MojoExecutionException
 250  
     {
 251  
         FileReader fileReader;
 252  
         try
 253  
         {
 254  0
             fileReader = new FileReader( file );
 255  
         }
 256  0
         catch ( FileNotFoundException fnfe )
 257  
         {
 258  0
             throw new MojoExecutionException( "File not found. " + file );
 259  0
         }
 260  0
         return fileReader;
 261  
     }
 262  
 
 263  
     /**
 264  
      * Returns the identify of the mail sender according to the plugin's configuration:
 265  
      * <ul>
 266  
      * <li>if the <tt>mailSender</tt> parameter is set, it is returned</li>
 267  
      * <li>if no <tt>fromDeveloperId</tt> is set, the first developer in the list is returned</li>
 268  
      * <li>if a <tt>fromDeveloperId</tt> is set, the developer with that id is returned</li>
 269  
      * <li>if the developers list is empty or if the specified id does not exist, an exception is thrown</li>
 270  
      * </ul>
 271  
      *
 272  
      * @return the mail sender to use
 273  
      * @throws MojoExecutionException if the mail sender could not be retrieved
 274  
      */
 275  
     protected MailSender getActualMailSender()
 276  
         throws MojoExecutionException
 277  
     {
 278  0
         if ( mailSender != null )
 279  
         {
 280  0
             return mailSender;
 281  
         }
 282  0
         else if ( from == null || from.isEmpty() )
 283  
         {
 284  0
             throw new MojoExecutionException(
 285  
                 "The <developers> section in your pom should not be empty. Add a <developer> entry or set the "
 286  
                     + "mailSender parameter." );
 287  
         }
 288  0
         else if ( fromDeveloperId == null )
 289  
         {
 290  0
             final Developer dev = (Developer) from.get( 0 );
 291  0
             return new MailSender( dev.getName(), dev.getEmail() );
 292  
         }
 293  
         else
 294  
         {
 295  0
             final Iterator it = from.iterator();
 296  0
             while ( it.hasNext() )
 297  
             {
 298  0
                 Developer developer = (Developer) it.next();
 299  
 
 300  0
                 if ( fromDeveloperId.equals( developer.getId() ) )
 301  
                 {
 302  0
                     return new MailSender( developer.getName(), developer.getEmail() );
 303  
                 }
 304  
             }
 305  0
             throw new MojoExecutionException(
 306  
                 "Missing developer with id '" + fromDeveloperId + "' in the <developers> section in your pom." );
 307  
         }
 308  
     }
 309  
 
 310  
     //================================
 311  
     // announcement-mail accessors
 312  
     //================================
 313  
 
 314  
     public String getSmtpHost()
 315  
     {
 316  0
         return smtpHost;
 317  
     }
 318  
 
 319  
     public void setSmtpHost( String smtpHost )
 320  
     {
 321  0
         this.smtpHost = smtpHost;
 322  0
     }
 323  
 
 324  
     public int getSmtpPort()
 325  
     {
 326  0
         return smtpPort;
 327  
     }
 328  
 
 329  
     public void setSmtpPort( int smtpPort )
 330  
     {
 331  0
         this.smtpPort = smtpPort;
 332  0
     }
 333  
 
 334  
     public String getSubject()
 335  
     {
 336  0
         return subject;
 337  
     }
 338  
 
 339  
     public void setSubject( String subject )
 340  
     {
 341  0
         this.subject = subject;
 342  0
     }
 343  
 
 344  
     public List getFrom()
 345  
     {
 346  0
         return from;
 347  
     }
 348  
 
 349  
     public void setFrom( List from )
 350  
     {
 351  0
         this.from = from;
 352  0
     }
 353  
 
 354  
     public MavenProject getProject()
 355  
     {
 356  0
         return project;
 357  
     }
 358  
 
 359  
     public void setProject( MavenProject project )
 360  
     {
 361  0
         this.project = project;
 362  0
     }
 363  
 
 364  
     public List getToAddresses()
 365  
     {
 366  0
         return toAddresses;
 367  
     }
 368  
 
 369  
     public void setToAddresses( List toAddresses )
 370  
     {
 371  0
         this.toAddresses = toAddresses;
 372  0
     }
 373  
 
 374  
     public String getFromDeveloperId()
 375  
     {
 376  0
         return fromDeveloperId;
 377  
     }
 378  
 
 379  
     public void setFromDeveloperId( String fromDeveloperId )
 380  
     {
 381  0
         this.fromDeveloperId = fromDeveloperId;
 382  0
     }
 383  
 
 384  
 
 385  
     public String getUsername()
 386  
     {
 387  0
         return username;
 388  
     }
 389  
 
 390  
     public void setUsername( String username )
 391  
     {
 392  0
         this.username = username;
 393  0
     }
 394  
 
 395  
     public String getPassword()
 396  
     {
 397  0
         return password;
 398  
     }
 399  
 
 400  
     public void setPassword( String password )
 401  
     {
 402  0
         this.password = password;
 403  0
     }
 404  
 
 405  
     public boolean isSslMode()
 406  
     {
 407  0
         return sslMode;
 408  
     }
 409  
 
 410  
     public void setSslMode( boolean sslMode )
 411  
     {
 412  0
         this.sslMode = sslMode;
 413  0
     }
 414  
 
 415  
     public MailSender getMailSender()
 416  
     {
 417  0
         return mailSender;
 418  
     }
 419  
 
 420  
     public void setMailSender( MailSender mailSender )
 421  
     {
 422  0
         this.mailSender = mailSender;
 423  0
     }
 424  
 
 425  
     public File getTemplateOutputDirectory()
 426  
     {
 427  0
         return templateOutputDirectory;
 428  
     }
 429  
 
 430  
     public void setTemplateOutputDirectory( File templateOutputDirectory )
 431  
     {
 432  0
         this.templateOutputDirectory = templateOutputDirectory;
 433  0
     }
 434  
 
 435  
     public String getTemplate()
 436  
     {
 437  0
         return template;
 438  
     }
 439  
 
 440  
     public void setTemplate( String template )
 441  
     {
 442  0
         this.template = template;
 443  0
     }
 444  
 }