1 |
|
package org.apache.maven.announcement; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
import org.apache.maven.announcement.mailsender.ProjectJavamailMailSender; |
20 |
|
import org.apache.maven.model.Developer; |
21 |
|
import org.apache.maven.plugin.AbstractMojo; |
22 |
|
import org.apache.maven.plugin.MojoExecutionException; |
23 |
|
import org.apache.maven.project.MavenProject; |
24 |
|
import org.codehaus.plexus.logging.console.ConsoleLogger; |
25 |
|
import org.codehaus.plexus.mailsender.MailSenderException; |
26 |
|
import org.codehaus.plexus.util.IOUtil; |
27 |
|
|
28 |
|
import java.io.File; |
29 |
|
import java.io.FileNotFoundException; |
30 |
|
import java.io.FileReader; |
31 |
|
import java.io.IOException; |
32 |
|
import java.util.List; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
0 |
public class AnnouncementMailMojo extends AbstractMojo |
43 |
|
{ |
44 |
|
|
45 |
0 |
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
private MavenProject project; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private String smtpHost; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private int smtpPort; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private String username; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
private String password; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
private boolean sslMode; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
private String subject; |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
private List toAddresses; |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
private List from; |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
0 |
|
121 |
|
private String templateOutputDirectory; |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
0 |
|
126 |
|
|
127 |
0 |
|
128 |
|
|
129 |
0 |
private String template; |
130 |
|
|
131 |
0 |
private ProjectJavamailMailSender mailer = new ProjectJavamailMailSender(); |
132 |
|
|
133 |
0 |
|
134 |
|
public void execute() throws MojoExecutionException |
135 |
0 |
{ |
136 |
0 |
template = templateOutputDirectory + "/" + template; |
137 |
0 |
|
138 |
0 |
ConsoleLogger logger = new ConsoleLogger( 0, "base" ); |
139 |
0 |
|
140 |
0 |
mailer.enableLogging( logger ); |
141 |
|
|
142 |
0 |
mailer.setSmtpHost( getSmtpHost() ); |
143 |
|
|
144 |
0 |
mailer.setSmtpPort( getSmtpPort() ); |
145 |
|
|
146 |
0 |
mailer.setSslMode( sslMode ); |
147 |
|
|
148 |
0 |
if ( username != null ) |
149 |
|
{ |
150 |
0 |
mailer.setUsername( username ); |
151 |
|
} |
152 |
0 |
|
153 |
0 |
if ( password != null ) |
154 |
|
{ |
155 |
0 |
mailer.setPassword( password ); |
156 |
0 |
} |
157 |
0 |
mailer.initialize(); |
158 |
0 |
|
159 |
0 |
if ( isTextFileExisting( template ) ) |
160 |
0 |
{ |
161 |
0 |
getLog().info( "Connecting to Host: " + getSmtpHost() + ":" + getSmtpPort() ); |
162 |
0 |
|
163 |
0 |
sendMessage( ); |
164 |
|
} |
165 |
|
else |
166 |
0 |
{ |
167 |
0 |
if ( template != null ) |
168 |
|
{ |
169 |
0 |
if ( isTextFileExisting( template ) ) |
170 |
|
{ |
171 |
0 |
getLog().info( "Connecting to Host: " + getSmtpHost() + " : " + getSmtpPort() ); |
172 |
|
|
173 |
0 |
sendMessage( ); |
174 |
0 |
} |
175 |
|
else |
176 |
|
{ |
177 |
0 |
throw new MojoExecutionException( "Announcement template " + template + " not found..." ); |
178 |
|
} |
179 |
|
} |
180 |
|
else |
181 |
|
{ |
182 |
0 |
throw new MojoExecutionException( "Announcement template " + template + " not found..." ); |
183 |
|
} |
184 |
0 |
} |
185 |
0 |
} |
186 |
|
|
187 |
|
|
188 |
0 |
|
189 |
|
|
190 |
0 |
|
191 |
|
|
192 |
0 |
protected void sendMessage() throws MojoExecutionException |
193 |
|
{ |
194 |
0 |
String email = ""; |
195 |
|
|
196 |
0 |
try |
197 |
|
{ |
198 |
0 |
int i = 0; |
199 |
|
|
200 |
0 |
String[] from = getFirstDevInfo( getFrom() ); |
201 |
|
|
202 |
0 |
while ( i < getToAddresses().size() ) |
203 |
|
{ |
204 |
0 |
email = getToAddresses().get( i ).toString(); |
205 |
0 |
|
206 |
0 |
getLog().info( "Sending mail... " + email ); |
207 |
0 |
|
208 |
0 |
mailer.send( getSubject(), IOUtil.toString( readAnnouncement( template ) ), |
209 |
0 |
email, "", from[0], from[1] ); |
210 |
0 |
|
211 |
0 |
getLog().info( "Sent..." ); |
212 |
0 |
|
213 |
0 |
i++; |
214 |
|
} |
215 |
0 |
} |
216 |
0 |
catch ( IOException ioe ) |
217 |
0 |
{ |
218 |
0 |
throw new MojoExecutionException( "Failed to send email.", ioe ); |
219 |
0 |
} |
220 |
0 |
catch ( MailSenderException e ) |
221 |
0 |
{ |
222 |
0 |
throw new MojoExecutionException( "Failed to send email < " + email + " >", e ); |
223 |
0 |
} |
224 |
0 |
} |
225 |
0 |
|
226 |
|
protected boolean isTextFileExisting( String fileName ) |
227 |
0 |
{ |
228 |
0 |
boolean found = false; |
229 |
0 |
|
230 |
0 |
File f = new File( fileName ); |
231 |
0 |
|
232 |
0 |
if ( f.exists() ) |
233 |
0 |
{ |
234 |
0 |
found = true; |
235 |
0 |
} |
236 |
0 |
return found; |
237 |
|
} |
238 |
|
|
239 |
0 |
|
240 |
|
|
241 |
0 |
|
242 |
|
|
243 |
0 |
|
244 |
|
public FileReader readAnnouncement( String fileName ) throws MojoExecutionException |
245 |
0 |
{ |
246 |
0 |
FileReader fileReader = null; |
247 |
0 |
|
248 |
|
try |
249 |
0 |
{ |
250 |
0 |
File file = new File( fileName ); |
251 |
0 |
|
252 |
0 |
fileReader = new FileReader( file ); |
253 |
0 |
} |
254 |
0 |
catch ( FileNotFoundException fnfe ) |
255 |
0 |
{ |
256 |
0 |
throw new MojoExecutionException( "File not found. " + fileName ); |
257 |
0 |
} |
258 |
0 |
return fileReader; |
259 |
0 |
} |
260 |
|
|
261 |
0 |
|
262 |
|
|
263 |
0 |
|
264 |
|
|
265 |
0 |
|
266 |
|
public String[] getFirstDevInfo( List fromNames ) throws MojoExecutionException |
267 |
0 |
{ |
268 |
0 |
String fromAddress = ""; |
269 |
0 |
|
270 |
0 |
String fromName = ""; |
271 |
0 |
|
272 |
0 |
String[] info = new String[2]; |
273 |
0 |
|
274 |
0 |
if ( fromNames.size() > 0 ) |
275 |
0 |
{ |
276 |
0 |
Developer developer = ( Developer ) fromNames.get( 0 ); |
277 |
0 |
|
278 |
0 |
fromAddress = developer.getEmail(); |
279 |
0 |
|
280 |
0 |
fromName = developer.getName(); |
281 |
0 |
|
282 |
0 |
info[0] = fromAddress; |
283 |
0 |
|
284 |
0 |
info[1] = fromName; |
285 |
0 |
|
286 |
0 |
getLog().info( "email retrieved. " + fromAddress + " < " + fromName + " > " ); |
287 |
0 |
|
288 |
0 |
if ( fromAddress == null || fromAddress.equals( "" ) ) |
289 |
0 |
{ |
290 |
0 |
throw new MojoExecutionException( "Email address in <developers> section is required." ); |
291 |
|
} |
292 |
|
} |
293 |
|
else |
294 |
0 |
{ |
295 |
0 |
throw new MojoExecutionException( "Email address in <developers> section is required." ); |
296 |
0 |
} |
297 |
0 |
return info; |
298 |
|
} |
299 |
|
|
300 |
0 |
|
301 |
0 |
|
302 |
|
|
303 |
|
|
304 |
|
public String getSmtpHost() |
305 |
0 |
{ |
306 |
0 |
return smtpHost; |
307 |
|
} |
308 |
|
|
309 |
|
public void setSmtpHost( String smtpHost ) |
310 |
0 |
{ |
311 |
0 |
this.smtpHost = smtpHost; |
312 |
0 |
} |
313 |
|
|
314 |
|
public int getSmtpPort() |
315 |
0 |
{ |
316 |
0 |
return smtpPort; |
317 |
|
} |
318 |
|
|
319 |
|
public void setSmtpPort( int smtpPort ) |
320 |
0 |
{ |
321 |
0 |
this.smtpPort = smtpPort; |
322 |
0 |
} |
323 |
|
|
324 |
|
public String getSubject() |
325 |
0 |
{ |
326 |
0 |
return subject; |
327 |
|
} |
328 |
|
|
329 |
|
public void setSubject( String subject ) |
330 |
0 |
{ |
331 |
0 |
this.subject = subject; |
332 |
0 |
} |
333 |
|
|
334 |
|
public List getFrom() |
335 |
0 |
{ |
336 |
0 |
return from; |
337 |
|
} |
338 |
|
|
339 |
|
public void setFrom( List from ) |
340 |
0 |
{ |
341 |
0 |
this.from = from; |
342 |
0 |
} |
343 |
|
|
344 |
|
public MavenProject getProject() |
345 |
0 |
{ |
346 |
0 |
return project; |
347 |
|
} |
348 |
|
|
349 |
|
public void setProject( MavenProject project ) |
350 |
0 |
{ |
351 |
0 |
this.project = project; |
352 |
0 |
} |
353 |
|
|
354 |
|
public List getToAddresses() |
355 |
0 |
{ |
356 |
0 |
return toAddresses; |
357 |
|
} |
358 |
|
|
359 |
|
public void setToAddresses( List toAddresses ) |
360 |
0 |
{ |
361 |
0 |
this.toAddresses = toAddresses; |
362 |
0 |
} |
363 |
|
} |