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