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