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