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 | 0 | 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 | 0 | private ProjectJavamailMailSender mailer = new ProjectJavamailMailSender(); |
157 | |
|
158 | |
public void execute() |
159 | |
throws MojoExecutionException |
160 | |
{ |
161 | 0 | File templateFile = new File( templateOutputDirectory, template ); |
162 | |
|
163 | 0 | ConsoleLogger logger = new ConsoleLogger( 0, "base" ); |
164 | |
|
165 | 0 | mailer.enableLogging( logger ); |
166 | |
|
167 | 0 | mailer.setSmtpHost( getSmtpHost() ); |
168 | |
|
169 | 0 | mailer.setSmtpPort( getSmtpPort() ); |
170 | |
|
171 | 0 | mailer.setSslMode( sslMode ); |
172 | |
|
173 | 0 | if ( username != null ) |
174 | |
{ |
175 | 0 | mailer.setUsername( username ); |
176 | |
} |
177 | |
|
178 | 0 | if ( password != null ) |
179 | |
{ |
180 | 0 | mailer.setPassword( password ); |
181 | |
} |
182 | 0 | mailer.initialize(); |
183 | |
|
184 | 0 | if ( getLog().isDebugEnabled() ) |
185 | |
{ |
186 | 0 | getLog().debug( "fromDeveloperId: " + getFromDeveloperId() ); |
187 | |
} |
188 | |
|
189 | 0 | if ( templateFile.isFile() ) |
190 | |
{ |
191 | 0 | getLog().info( "Connecting to Host: " + getSmtpHost() + ":" + getSmtpPort() ); |
192 | |
|
193 | 0 | sendMessage(); |
194 | |
} |
195 | |
else |
196 | |
{ |
197 | 0 | throw new MojoExecutionException( "Announcement template " + templateFile + " not found..." ); |
198 | |
} |
199 | 0 | } |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
protected void sendMessage() |
207 | |
throws MojoExecutionException |
208 | |
{ |
209 | 0 | File templateFile = new File( templateOutputDirectory, template ); |
210 | 0 | String email = ""; |
211 | 0 | final MailSender ms = getActualMailSender(); |
212 | 0 | final String fromName = ms.getName(); |
213 | 0 | final String fromAddress = ms.getEmail(); |
214 | 0 | if ( fromAddress == null || fromAddress.equals( "" ) ) |
215 | |
{ |
216 | 0 | throw new MojoExecutionException( "Invalid mail sender: name and email is mandatory (" + ms + ")." ); |
217 | |
} |
218 | 0 | getLog().info( "Using this sender for email announcement: " + fromAddress + " < " + fromName + " > " ); |
219 | |
try |
220 | |
{ |
221 | 0 | final Iterator it = getToAddresses().iterator(); |
222 | 0 | while ( it.hasNext() ) |
223 | |
{ |
224 | 0 | email = it.next().toString(); |
225 | 0 | getLog().info( "Sending mail to " + email + "..." ); |
226 | 0 | mailer.send( getSubject(), IOUtil.toString( readAnnouncement( templateFile ) ), email, "", fromAddress, |
227 | |
fromName ); |
228 | 0 | getLog().info( "Sent..." ); |
229 | |
} |
230 | |
} |
231 | 0 | catch ( IOException ioe ) |
232 | |
{ |
233 | 0 | throw new MojoExecutionException( "Failed to send email.", ioe ); |
234 | |
} |
235 | 0 | catch ( MailSenderException e ) |
236 | |
{ |
237 | 0 | throw new MojoExecutionException( "Failed to send email < " + email + " >", e ); |
238 | 0 | } |
239 | 0 | } |
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 | 0 | fileReader = new FileReader( file ); |
255 | |
} |
256 | 0 | catch ( FileNotFoundException fnfe ) |
257 | |
{ |
258 | 0 | throw new MojoExecutionException( "File not found. " + file ); |
259 | 0 | } |
260 | 0 | 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 | 0 | if ( mailSender != null ) |
279 | |
{ |
280 | 0 | return mailSender; |
281 | |
} |
282 | 0 | else if ( from == null || from.isEmpty() ) |
283 | |
{ |
284 | 0 | 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 | 0 | else if ( fromDeveloperId == null ) |
289 | |
{ |
290 | 0 | final Developer dev = (Developer) from.get( 0 ); |
291 | 0 | return new MailSender( dev.getName(), dev.getEmail() ); |
292 | |
} |
293 | |
else |
294 | |
{ |
295 | 0 | final Iterator it = from.iterator(); |
296 | 0 | while ( it.hasNext() ) |
297 | |
{ |
298 | 0 | Developer developer = (Developer) it.next(); |
299 | |
|
300 | 0 | if ( fromDeveloperId.equals( developer.getId() ) ) |
301 | |
{ |
302 | 0 | return new MailSender( developer.getName(), developer.getEmail() ); |
303 | |
} |
304 | |
} |
305 | 0 | 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 | 0 | return smtpHost; |
317 | |
} |
318 | |
|
319 | |
public void setSmtpHost( String smtpHost ) |
320 | |
{ |
321 | 0 | this.smtpHost = smtpHost; |
322 | 0 | } |
323 | |
|
324 | |
public int getSmtpPort() |
325 | |
{ |
326 | 0 | return smtpPort; |
327 | |
} |
328 | |
|
329 | |
public void setSmtpPort( int smtpPort ) |
330 | |
{ |
331 | 0 | this.smtpPort = smtpPort; |
332 | 0 | } |
333 | |
|
334 | |
public String getSubject() |
335 | |
{ |
336 | 0 | return subject; |
337 | |
} |
338 | |
|
339 | |
public void setSubject( String subject ) |
340 | |
{ |
341 | 0 | this.subject = subject; |
342 | 0 | } |
343 | |
|
344 | |
public List getFrom() |
345 | |
{ |
346 | 0 | return from; |
347 | |
} |
348 | |
|
349 | |
public void setFrom( List from ) |
350 | |
{ |
351 | 0 | this.from = from; |
352 | 0 | } |
353 | |
|
354 | |
public MavenProject getProject() |
355 | |
{ |
356 | 0 | return project; |
357 | |
} |
358 | |
|
359 | |
public void setProject( MavenProject project ) |
360 | |
{ |
361 | 0 | this.project = project; |
362 | 0 | } |
363 | |
|
364 | |
public List getToAddresses() |
365 | |
{ |
366 | 0 | return toAddresses; |
367 | |
} |
368 | |
|
369 | |
public void setToAddresses( List toAddresses ) |
370 | |
{ |
371 | 0 | this.toAddresses = toAddresses; |
372 | 0 | } |
373 | |
|
374 | |
public String getFromDeveloperId() |
375 | |
{ |
376 | 0 | return fromDeveloperId; |
377 | |
} |
378 | |
|
379 | |
public void setFromDeveloperId( String fromDeveloperId ) |
380 | |
{ |
381 | 0 | this.fromDeveloperId = fromDeveloperId; |
382 | 0 | } |
383 | |
|
384 | |
|
385 | |
public String getUsername() |
386 | |
{ |
387 | 0 | return username; |
388 | |
} |
389 | |
|
390 | |
public void setUsername( String username ) |
391 | |
{ |
392 | 0 | this.username = username; |
393 | 0 | } |
394 | |
|
395 | |
public String getPassword() |
396 | |
{ |
397 | 0 | return password; |
398 | |
} |
399 | |
|
400 | |
public void setPassword( String password ) |
401 | |
{ |
402 | 0 | this.password = password; |
403 | 0 | } |
404 | |
|
405 | |
public boolean isSslMode() |
406 | |
{ |
407 | 0 | return sslMode; |
408 | |
} |
409 | |
|
410 | |
public void setSslMode( boolean sslMode ) |
411 | |
{ |
412 | 0 | this.sslMode = sslMode; |
413 | 0 | } |
414 | |
|
415 | |
public MailSender getMailSender() |
416 | |
{ |
417 | 0 | return mailSender; |
418 | |
} |
419 | |
|
420 | |
public void setMailSender( MailSender mailSender ) |
421 | |
{ |
422 | 0 | this.mailSender = mailSender; |
423 | 0 | } |
424 | |
|
425 | |
public File getTemplateOutputDirectory() |
426 | |
{ |
427 | 0 | return templateOutputDirectory; |
428 | |
} |
429 | |
|
430 | |
public void setTemplateOutputDirectory( File templateOutputDirectory ) |
431 | |
{ |
432 | 0 | this.templateOutputDirectory = templateOutputDirectory; |
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 | |
} |