1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.bugzilla;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.maven.model.IssueManagement;
20 import org.apache.maven.plugin.AbstractMojo;
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.plugin.logging.Log;
24 import org.apache.maven.project.MavenProject;
25 import org.apache.maven.settings.Server;
26 import org.apache.maven.settings.Settings;
27
28 import de.smartics.maven.issue.command.CommandTarget;
29 import de.smartics.maven.issue.util.LogLevel;
30 import de.smartics.maven.issue.util.MavenProjectWrapper;
31
32
33
34
35 public abstract class AbstractIssueMojo extends AbstractMojo
36 {
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 private MavenProject project;
54
55
56
57
58
59
60
61
62
63 protected Settings settings;
64
65
66
67
68
69
70
71
72
73
74
75
76 protected String issueServerId;
77
78
79
80
81
82
83 protected IssueServer server;
84
85
86
87
88 protected Console console;
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 protected String verbose;
105
106
107
108
109
110
111
112
113
114
115
116
117 protected static final class IssueServer extends Server
118 {
119
120
121
122
123
124
125 private static final long serialVersionUID = 1L;
126
127
128
129
130 private final String url;
131
132
133
134
135
136
137
138 private IssueServer(final Server server, final String url)
139 {
140 this.url = url;
141
142 if (server != null)
143 {
144 this.setId(server.getId());
145 this.setUsername(server.getUsername());
146
147 this.setPassword(server.getPassword());
148 this.setPassphrase(server.getPassphrase());
149 this.setPrivateKey(server.getPrivateKey());
150
151 this.setDirectoryPermissions(server.getDirectoryPermissions());
152 this.setFilePermissions(server.getFilePermissions());
153 this.setConfiguration(server.getConfiguration());
154
155 this.setSourceLevel(server.getSourceLevel());
156 }
157 }
158
159
160
161
162
163
164 public String getUrl()
165 {
166 return url;
167 }
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181 protected final MavenProject getProject()
182 {
183 return new MavenProjectWrapper(project);
184 }
185
186
187
188
189
190
191
192
193
194
195 protected final IssueServer getIssueServer() throws MojoExecutionException
196 {
197 final IssueManagement issueManagement = project.getIssueManagement();
198 if (issueManagement == null)
199 {
200 throw new MojoExecutionException(
201 "No issue management specified. Please provide issue information"
202 + " within the 'issueManagement' block of your POM.");
203 }
204
205 final Server server = getIssueServerFromSettings(issueManagement);
206 final String ciUrl = issueManagement.getUrl();
207 return new IssueServer(server, ciUrl);
208 }
209
210
211
212
213
214
215
216 @Override
217 public void execute() throws MojoExecutionException, MojoFailureException
218
219 {
220 final Log log = getLog();
221
222 server = getIssueServer();
223 final String url = server.getUrl();
224 final LogLevel logLevel = new LogLevel(verbose);
225 final CommandTarget target = new CommandTarget(url);
226
227 console = new Console(target, log, logLevel);
228 }
229
230 private Server getIssueServerFromSettings(
231 final IssueManagement issueManagement) throws MojoExecutionException
232 {
233 final String issueUrl = issueManagement.getUrl();
234 if (StringUtils.isBlank(issueUrl))
235 {
236 throw new MojoExecutionException(
237 "No issue URL specified. Please provide issue information within the"
238 + " 'issueManagement/url' block of your POM.");
239 }
240
241 Server server;
242 if (StringUtils.isNotBlank(issueServerId))
243 {
244 server = settings.getServer(issueServerId);
245 if (server != null)
246 {
247 return server;
248 }
249 }
250
251 final String ciName = issueManagement.getSystem();
252 if (StringUtils.isNotBlank(ciName))
253 {
254 server = settings.getServer(ciName);
255 if (server != null)
256 {
257 return server;
258 }
259 }
260
261 server = settings.getServer(issueUrl);
262
263 return server;
264 }
265
266
267
268
269
270
271
272 protected final boolean isToSkip()
273 {
274 return !project.isExecutionRoot();
275 }
276
277
278
279 }