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