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 | |
private String username; |
166 | |
|
167 | 0 | private ProjectJavamailMailSender mailer = new ProjectJavamailMailSender(); |
168 | |
|
169 | |
public void execute() |
170 | |
throws MojoExecutionException |
171 | |
{ |
172 | |
|
173 | 0 | if ( runOnlyAtExecutionRoot && !isThisTheExecutionRoot() ) |
174 | |
{ |
175 | 0 | getLog().info( "Skipping the announcement mail in this project because it's not the Execution Root" ); |
176 | |
} |
177 | |
else |
178 | |
{ |
179 | 0 | File templateFile = new File( templateOutputDirectory, template ); |
180 | |
|
181 | 0 | ConsoleLogger logger = new ConsoleLogger( Logger.LEVEL_INFO, "base" ); |
182 | |
|
183 | 0 | if ( getLog().isDebugEnabled() ) |
184 | |
{ |
185 | 0 | logger.setThreshold( Logger.LEVEL_DEBUG ); |
186 | |
} |
187 | |
|
188 | 0 | mailer.enableLogging( logger ); |
189 | |
|
190 | 0 | mailer.setSmtpHost( getSmtpHost() ); |
191 | |
|
192 | 0 | mailer.setSmtpPort( getSmtpPort() ); |
193 | |
|
194 | 0 | mailer.setSslMode( sslMode ); |
195 | |
|
196 | 0 | if ( username != null ) |
197 | |
{ |
198 | 0 | mailer.setUsername( username ); |
199 | |
} |
200 | |
|
201 | 0 | if ( password != null ) |
202 | |
{ |
203 | 0 | mailer.setPassword( password ); |
204 | |
} |
205 | |
|
206 | 0 | mailer.initialize(); |
207 | |
|
208 | 0 | if ( getLog().isDebugEnabled() ) |
209 | |
{ |
210 | 0 | getLog().debug( "fromDeveloperId: " + getFromDeveloperId() ); |
211 | |
} |
212 | |
|
213 | 0 | if ( templateFile.isFile() ) |
214 | |
{ |
215 | 0 | getLog().info( "Connecting to Host: " + getSmtpHost() + ":" + getSmtpPort() ); |
216 | |
|
217 | 0 | sendMessage(); |
218 | |
} |
219 | |
else |
220 | |
{ |
221 | 0 | throw new MojoExecutionException( "Announcement template " + templateFile + " not found..." ); |
222 | |
} |
223 | |
} |
224 | 0 | } |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
protected void sendMessage() |
232 | |
throws MojoExecutionException |
233 | |
{ |
234 | 0 | File templateFile = new File( templateOutputDirectory, template ); |
235 | 0 | String email = ""; |
236 | 0 | final MailSender ms = getActualMailSender(); |
237 | 0 | final String fromName = ms.getName(); |
238 | 0 | final String fromAddress = ms.getEmail(); |
239 | 0 | if ( fromAddress == null || fromAddress.equals( "" ) ) |
240 | |
{ |
241 | 0 | throw new MojoExecutionException( "Invalid mail sender: name and email is mandatory (" + ms + ")." ); |
242 | |
} |
243 | 0 | getLog().info( "Using this sender for email announcement: " + fromAddress + " < " + fromName + " > " ); |
244 | |
try |
245 | |
{ |
246 | 0 | MailMessage mailMsg = new MailMessage(); |
247 | 0 | mailMsg.setSubject( getSubject() ); |
248 | 0 | mailMsg.setContent( IOUtil.toString( readAnnouncement( templateFile ) ) ); |
249 | 0 | mailMsg.setContentType( this.mailContentType ); |
250 | 0 | mailMsg.setFrom( fromAddress, fromName ); |
251 | 0 | final Iterator it = getToAddresses().iterator(); |
252 | 0 | while ( it.hasNext() ) |
253 | |
{ |
254 | 0 | email = it.next().toString(); |
255 | 0 | getLog().info( "Sending mail to " + email + "..." ); |
256 | 0 | mailMsg.addTo( email, "" ); |
257 | |
|
258 | |
} |
259 | 0 | mailer.send( mailMsg ); |
260 | 0 | getLog().info( "Sent..." ); |
261 | |
} |
262 | 0 | catch ( IOException ioe ) |
263 | |
{ |
264 | 0 | throw new MojoExecutionException( "Failed to send email.", ioe ); |
265 | |
} |
266 | 0 | catch ( MailSenderException e ) |
267 | |
{ |
268 | 0 | throw new MojoExecutionException( "Failed to send email < " + email + " >", e ); |
269 | 0 | } |
270 | 0 | } |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
protected FileReader readAnnouncement( File file ) |
280 | |
throws MojoExecutionException |
281 | |
{ |
282 | |
FileReader fileReader; |
283 | |
try |
284 | |
{ |
285 | 0 | fileReader = new FileReader( file ); |
286 | |
} |
287 | 0 | catch ( FileNotFoundException fnfe ) |
288 | |
{ |
289 | 0 | throw new MojoExecutionException( "File not found. " + file ); |
290 | 0 | } |
291 | 0 | return fileReader; |
292 | |
} |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
protected MailSender getActualMailSender() |
307 | |
throws MojoExecutionException |
308 | |
{ |
309 | 0 | if ( mailSender != null && mailSender.getEmail() != null ) |
310 | |
{ |
311 | 0 | return mailSender; |
312 | |
} |
313 | 0 | else if ( from == null || from.isEmpty() ) |
314 | |
{ |
315 | 0 | throw new MojoExecutionException( |
316 | |
"The <developers> section in your pom should not be empty. Add a <developer> entry or set the " |
317 | |
+ "mailSender parameter." ); |
318 | |
} |
319 | 0 | else if ( fromDeveloperId == null ) |
320 | |
{ |
321 | 0 | final Developer dev = (Developer) from.get( 0 ); |
322 | 0 | return new MailSender( dev.getName(), dev.getEmail() ); |
323 | |
} |
324 | |
else |
325 | |
{ |
326 | 0 | final Iterator it = from.iterator(); |
327 | 0 | while ( it.hasNext() ) |
328 | |
{ |
329 | 0 | Developer developer = (Developer) it.next(); |
330 | |
|
331 | 0 | if ( fromDeveloperId.equals( developer.getId() ) ) |
332 | |
{ |
333 | 0 | return new MailSender( developer.getName(), developer.getEmail() ); |
334 | |
} |
335 | 0 | } |
336 | 0 | throw new MojoExecutionException( |
337 | |
"Missing developer with id '" + fromDeveloperId + "' in the <developers> section in your pom." ); |
338 | |
} |
339 | |
} |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
public List getFrom() |
346 | |
{ |
347 | 0 | return from; |
348 | |
} |
349 | |
|
350 | |
public void setFrom( List from ) |
351 | |
{ |
352 | 0 | this.from = from; |
353 | 0 | } |
354 | |
|
355 | |
public String getFromDeveloperId() |
356 | |
{ |
357 | 0 | return fromDeveloperId; |
358 | |
} |
359 | |
|
360 | |
public void setFromDeveloperId( String fromDeveloperId ) |
361 | |
{ |
362 | 0 | this.fromDeveloperId = fromDeveloperId; |
363 | 0 | } |
364 | |
|
365 | |
public MailSender getMailSender() |
366 | |
{ |
367 | 0 | return mailSender; |
368 | |
} |
369 | |
|
370 | |
public void setMailSender( MailSender mailSender ) |
371 | |
{ |
372 | 0 | this.mailSender = mailSender; |
373 | 0 | } |
374 | |
|
375 | |
public String getPassword() |
376 | |
{ |
377 | 0 | return password; |
378 | |
} |
379 | |
|
380 | |
public void setPassword( String password ) |
381 | |
{ |
382 | 0 | this.password = password; |
383 | 0 | } |
384 | |
|
385 | |
public MavenProject getProject() |
386 | |
{ |
387 | 0 | return project; |
388 | |
} |
389 | |
|
390 | |
public void setProject( MavenProject project ) |
391 | |
{ |
392 | 0 | this.project = project; |
393 | 0 | } |
394 | |
|
395 | |
public String getSmtpHost() |
396 | |
{ |
397 | 0 | return smtpHost; |
398 | |
} |
399 | |
|
400 | |
public void setSmtpHost( String smtpHost ) |
401 | |
{ |
402 | 0 | this.smtpHost = smtpHost; |
403 | 0 | } |
404 | |
|
405 | |
public int getSmtpPort() |
406 | |
{ |
407 | 0 | return smtpPort; |
408 | |
} |
409 | |
|
410 | |
public void setSmtpPort( int smtpPort ) |
411 | |
{ |
412 | 0 | this.smtpPort = smtpPort; |
413 | 0 | } |
414 | |
|
415 | |
public boolean isSslMode() |
416 | |
{ |
417 | 0 | return sslMode; |
418 | |
} |
419 | |
|
420 | |
public void setSslMode( boolean sslMode ) |
421 | |
{ |
422 | 0 | this.sslMode = sslMode; |
423 | 0 | } |
424 | |
|
425 | |
public String getSubject() |
426 | |
{ |
427 | 0 | return subject; |
428 | |
} |
429 | |
|
430 | |
public void setSubject( String subject ) |
431 | |
{ |
432 | 0 | this.subject = subject; |
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 | |
|
445 | |
public File getTemplateOutputDirectory() |
446 | |
{ |
447 | 0 | return templateOutputDirectory; |
448 | |
} |
449 | |
|
450 | |
public void setTemplateOutputDirectory( File templateOutputDirectory ) |
451 | |
{ |
452 | 0 | this.templateOutputDirectory = templateOutputDirectory; |
453 | 0 | } |
454 | |
|
455 | |
public List getToAddresses() |
456 | |
{ |
457 | 0 | return toAddresses; |
458 | |
} |
459 | |
|
460 | |
public void setToAddresses( List toAddresses ) |
461 | |
{ |
462 | 0 | this.toAddresses = toAddresses; |
463 | 0 | } |
464 | |
|
465 | |
public String getUsername() |
466 | |
{ |
467 | 0 | return username; |
468 | |
} |
469 | |
|
470 | |
public void setUsername( String username ) |
471 | |
{ |
472 | 0 | this.username = username; |
473 | 0 | } |
474 | |
} |