1 | |
package org.apache.maven.plugin.announcement; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileNotFoundException; |
24 | |
import java.io.FileReader; |
25 | |
import java.io.IOException; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
|
29 | |
import javax.mail.internet.AddressException; |
30 | |
import javax.mail.internet.InternetAddress; |
31 | |
|
32 | |
import org.apache.maven.model.Developer; |
33 | |
import org.apache.maven.plugin.MojoExecutionException; |
34 | |
import org.apache.maven.plugin.announcement.mailsender.ProjectJavamailMailSender; |
35 | |
import org.apache.maven.project.MavenProject; |
36 | |
import org.codehaus.plexus.logging.Logger; |
37 | |
import org.codehaus.plexus.logging.console.ConsoleLogger; |
38 | |
import org.codehaus.plexus.mailsender.MailMessage; |
39 | |
import org.codehaus.plexus.mailsender.MailSenderException; |
40 | |
import org.codehaus.plexus.util.IOUtil; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class AnnouncementMailMojo |
53 | |
extends AbstractAnnouncementMojo |
54 | |
{ |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private List from; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
private String fromDeveloperId; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private String mailContentType; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
private MailSender mailSender; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
private String senderString; |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
private String password; |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
private MavenProject project; |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
private String smtpHost; |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
private int smtpPort; |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
private boolean sslMode; |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
private String subject; |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
private String template; |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
private File templateOutputDirectory; |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
private List toAddresses; |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
private List ccAddresses; |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
private List bccAddresses; |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
private String username; |
200 | |
|
201 | 0 | private ProjectJavamailMailSender mailer = new ProjectJavamailMailSender(); |
202 | |
|
203 | |
public void execute() |
204 | |
throws MojoExecutionException |
205 | |
{ |
206 | |
|
207 | 0 | if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() ) |
208 | |
{ |
209 | 0 | getLog().info( "Skipping the announcement mail in this project because it's not the Execution Root" ); |
210 | |
} |
211 | |
else |
212 | |
{ |
213 | 0 | File templateFile = new File( templateOutputDirectory, template ); |
214 | |
|
215 | 0 | ConsoleLogger logger = new ConsoleLogger( Logger.LEVEL_INFO, "base" ); |
216 | |
|
217 | 0 | if ( getLog().isDebugEnabled() ) |
218 | |
{ |
219 | 0 | logger.setThreshold( Logger.LEVEL_DEBUG ); |
220 | |
} |
221 | |
|
222 | 0 | mailer.enableLogging( logger ); |
223 | |
|
224 | 0 | mailer.setSmtpHost( getSmtpHost() ); |
225 | |
|
226 | 0 | mailer.setSmtpPort( getSmtpPort() ); |
227 | |
|
228 | 0 | mailer.setSslMode( sslMode ); |
229 | |
|
230 | 0 | if ( username != null ) |
231 | |
{ |
232 | 0 | mailer.setUsername( username ); |
233 | |
} |
234 | |
|
235 | 0 | if ( password != null ) |
236 | |
{ |
237 | 0 | mailer.setPassword( password ); |
238 | |
} |
239 | |
|
240 | 0 | mailer.initialize(); |
241 | |
|
242 | 0 | if ( getLog().isDebugEnabled() ) |
243 | |
{ |
244 | 0 | getLog().debug( "fromDeveloperId: " + getFromDeveloperId() ); |
245 | |
} |
246 | |
|
247 | 0 | if ( templateFile.isFile() ) |
248 | |
{ |
249 | 0 | getLog().info( "Connecting to Host: " + getSmtpHost() + ":" + getSmtpPort() ); |
250 | |
|
251 | 0 | sendMessage(); |
252 | |
} |
253 | |
else |
254 | |
{ |
255 | 0 | throw new MojoExecutionException( "Announcement template " + templateFile + " not found..." ); |
256 | |
} |
257 | |
} |
258 | 0 | } |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
protected void sendMessage() |
266 | |
throws MojoExecutionException |
267 | |
{ |
268 | 0 | File templateFile = new File( templateOutputDirectory, template ); |
269 | 0 | String email = ""; |
270 | 0 | final MailSender ms = getActualMailSender(); |
271 | 0 | final String fromName = ms.getName(); |
272 | 0 | final String fromAddress = ms.getEmail(); |
273 | 0 | if ( fromAddress == null || fromAddress.equals( "" ) ) |
274 | |
{ |
275 | 0 | throw new MojoExecutionException( "Invalid mail sender: name and email is mandatory (" + ms + ")." ); |
276 | |
} |
277 | 0 | getLog().info( "Using this sender for email announcement: " + fromAddress + " < " + fromName + " > " ); |
278 | |
try |
279 | |
{ |
280 | 0 | MailMessage mailMsg = new MailMessage(); |
281 | 0 | mailMsg.setSubject( getSubject() ); |
282 | 0 | mailMsg.setContent( IOUtil.toString( readAnnouncement( templateFile ) ) ); |
283 | 0 | mailMsg.setContentType( this.mailContentType ); |
284 | 0 | mailMsg.setFrom( fromAddress, fromName ); |
285 | |
|
286 | 0 | final Iterator it = getToAddresses().iterator(); |
287 | 0 | while ( it.hasNext() ) |
288 | |
{ |
289 | 0 | email = it.next().toString(); |
290 | 0 | getLog().info( "Sending mail to " + email + "..." ); |
291 | 0 | mailMsg.addTo( email, "" ); |
292 | |
} |
293 | |
|
294 | 0 | if ( getCcAddresses() != null ) |
295 | |
{ |
296 | 0 | final Iterator it2 = getCcAddresses().iterator(); |
297 | 0 | while ( it2.hasNext() ) |
298 | |
{ |
299 | 0 | email = it2.next().toString(); |
300 | 0 | getLog().info( "Sending cc mail to " + email + "..." ); |
301 | 0 | mailMsg.addCc( email, "" ); |
302 | |
} |
303 | |
} |
304 | |
|
305 | 0 | if ( getBccAddresses() != null ) |
306 | |
{ |
307 | 0 | final Iterator it3 = getBccAddresses().iterator(); |
308 | 0 | while ( it3.hasNext() ) |
309 | |
{ |
310 | 0 | email = it3.next().toString(); |
311 | 0 | getLog().info( "Sending bcc mail to " + email + "..." ); |
312 | 0 | mailMsg.addBcc( email, "" ); |
313 | |
} |
314 | |
} |
315 | |
|
316 | 0 | mailer.send( mailMsg ); |
317 | 0 | getLog().info( "Sent..." ); |
318 | |
} |
319 | 0 | catch ( IOException ioe ) |
320 | |
{ |
321 | 0 | throw new MojoExecutionException( "Failed to send email.", ioe ); |
322 | |
} |
323 | 0 | catch ( MailSenderException e ) |
324 | |
{ |
325 | 0 | throw new MojoExecutionException( "Failed to send email < " + email + " >", e ); |
326 | 0 | } |
327 | 0 | } |
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
protected FileReader readAnnouncement( File file ) |
337 | |
throws MojoExecutionException |
338 | |
{ |
339 | |
FileReader fileReader; |
340 | |
try |
341 | |
{ |
342 | 0 | fileReader = new FileReader( file ); |
343 | |
} |
344 | 0 | catch ( FileNotFoundException fnfe ) |
345 | |
{ |
346 | 0 | throw new MojoExecutionException( "File not found. " + file ); |
347 | 0 | } |
348 | 0 | return fileReader; |
349 | |
} |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
protected MailSender getActualMailSender() |
364 | |
throws MojoExecutionException |
365 | |
{ |
366 | 0 | if ( senderString != null ) |
367 | |
{ |
368 | |
try |
369 | |
{ |
370 | 0 | InternetAddress ia = new InternetAddress( senderString, true ); |
371 | 0 | return new MailSender( ia.getPersonal(), ia.getAddress() ); |
372 | |
} |
373 | 0 | catch ( AddressException e ) |
374 | |
{ |
375 | 0 | throw new MojoExecutionException( "Invalid value for change.sender: ", e ); |
376 | |
} |
377 | |
} |
378 | 0 | if ( mailSender != null && mailSender.getEmail() != null ) |
379 | |
{ |
380 | 0 | return mailSender; |
381 | |
} |
382 | 0 | else if ( from == null || from.isEmpty() ) |
383 | |
{ |
384 | 0 | throw new MojoExecutionException( |
385 | |
"The <developers> section in your pom should not be empty. Add a <developer> entry or set the " |
386 | |
+ "mailSender parameter." ); |
387 | |
} |
388 | 0 | else if ( fromDeveloperId == null ) |
389 | |
{ |
390 | 0 | final Developer dev = (Developer) from.get( 0 ); |
391 | 0 | return new MailSender( dev.getName(), dev.getEmail() ); |
392 | |
} |
393 | |
else |
394 | |
{ |
395 | 0 | final Iterator it = from.iterator(); |
396 | 0 | while ( it.hasNext() ) |
397 | |
{ |
398 | 0 | Developer developer = (Developer) it.next(); |
399 | |
|
400 | 0 | if ( fromDeveloperId.equals( developer.getId() ) ) |
401 | |
{ |
402 | 0 | return new MailSender( developer.getName(), developer.getEmail() ); |
403 | |
} |
404 | 0 | } |
405 | 0 | throw new MojoExecutionException( |
406 | |
"Missing developer with id '" + fromDeveloperId + "' in the <developers> section in your pom." ); |
407 | |
} |
408 | |
} |
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
public List getBccAddresses() |
415 | |
{ |
416 | 0 | return bccAddresses; |
417 | |
} |
418 | |
|
419 | |
public void setBccAddresses( List bccAddresses ) |
420 | |
{ |
421 | 0 | this.bccAddresses = bccAddresses; |
422 | 0 | } |
423 | |
|
424 | |
public List getCcAddresses() |
425 | |
{ |
426 | 0 | return ccAddresses; |
427 | |
} |
428 | |
|
429 | |
public void setCcAddresses( List ccAddresses ) |
430 | |
{ |
431 | 0 | this.ccAddresses = ccAddresses; |
432 | 0 | } |
433 | |
|
434 | |
public List getFrom() |
435 | |
{ |
436 | 0 | return from; |
437 | |
} |
438 | |
|
439 | |
public void setFrom( List from ) |
440 | |
{ |
441 | 0 | this.from = from; |
442 | 0 | } |
443 | |
|
444 | |
public String getFromDeveloperId() |
445 | |
{ |
446 | 0 | return fromDeveloperId; |
447 | |
} |
448 | |
|
449 | |
public void setFromDeveloperId( String fromDeveloperId ) |
450 | |
{ |
451 | 0 | this.fromDeveloperId = fromDeveloperId; |
452 | 0 | } |
453 | |
|
454 | |
public MailSender getMailSender() |
455 | |
{ |
456 | 0 | return mailSender; |
457 | |
} |
458 | |
|
459 | |
public void setMailSender( MailSender mailSender ) |
460 | |
{ |
461 | 0 | this.mailSender = mailSender; |
462 | 0 | } |
463 | |
|
464 | |
public String getPassword() |
465 | |
{ |
466 | 0 | return password; |
467 | |
} |
468 | |
|
469 | |
public void setPassword( String password ) |
470 | |
{ |
471 | 0 | this.password = password; |
472 | 0 | } |
473 | |
|
474 | |
public MavenProject getProject() |
475 | |
{ |
476 | 0 | return project; |
477 | |
} |
478 | |
|
479 | |
public void setProject( MavenProject project ) |
480 | |
{ |
481 | 0 | this.project = project; |
482 | 0 | } |
483 | |
|
484 | |
public String getSmtpHost() |
485 | |
{ |
486 | 0 | return smtpHost; |
487 | |
} |
488 | |
|
489 | |
public void setSmtpHost( String smtpHost ) |
490 | |
{ |
491 | 0 | this.smtpHost = smtpHost; |
492 | 0 | } |
493 | |
|
494 | |
public int getSmtpPort() |
495 | |
{ |
496 | 0 | return smtpPort; |
497 | |
} |
498 | |
|
499 | |
public void setSmtpPort( int smtpPort ) |
500 | |
{ |
501 | 0 | this.smtpPort = smtpPort; |
502 | 0 | } |
503 | |
|
504 | |
public boolean isSslMode() |
505 | |
{ |
506 | 0 | return sslMode; |
507 | |
} |
508 | |
|
509 | |
public void setSslMode( boolean sslMode ) |
510 | |
{ |
511 | 0 | this.sslMode = sslMode; |
512 | 0 | } |
513 | |
|
514 | |
public String getSubject() |
515 | |
{ |
516 | 0 | return subject; |
517 | |
} |
518 | |
|
519 | |
public void setSubject( String subject ) |
520 | |
{ |
521 | 0 | this.subject = subject; |
522 | 0 | } |
523 | |
|
524 | |
public String getTemplate() |
525 | |
{ |
526 | 0 | return template; |
527 | |
} |
528 | |
|
529 | |
public void setTemplate( String template ) |
530 | |
{ |
531 | 0 | this.template = template; |
532 | 0 | } |
533 | |
|
534 | |
public File getTemplateOutputDirectory() |
535 | |
{ |
536 | 0 | return templateOutputDirectory; |
537 | |
} |
538 | |
|
539 | |
public void setTemplateOutputDirectory( File templateOutputDirectory ) |
540 | |
{ |
541 | 0 | this.templateOutputDirectory = templateOutputDirectory; |
542 | 0 | } |
543 | |
|
544 | |
public List getToAddresses() |
545 | |
{ |
546 | 0 | return toAddresses; |
547 | |
} |
548 | |
|
549 | |
public void setToAddresses( List toAddresses ) |
550 | |
{ |
551 | 0 | this.toAddresses = toAddresses; |
552 | 0 | } |
553 | |
|
554 | |
public String getUsername() |
555 | |
{ |
556 | 0 | return username; |
557 | |
} |
558 | |
|
559 | |
public void setUsername( String username ) |
560 | |
{ |
561 | 0 | this.username = username; |
562 | 0 | } |
563 | |
} |