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