1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.bugzilla;
17
18 import static de.smartics.maven.issue.util.PackagingUtils.mapToExtension;
19
20 import org.apache.commons.httpclient.HttpClient;
21 import org.apache.maven.model.DeploymentRepository;
22 import org.apache.maven.model.DistributionManagement;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.plugin.logging.Log;
26 import org.apache.maven.project.MavenProject;
27 import org.codehaus.plexus.util.StringUtils;
28
29 import de.smartics.maven.issue.command.CommandException;
30 import de.smartics.maven.issue.util.LogLevel;
31 import de.smartics.maven.issue.util.VersionHelper;
32 import de.smartics.maven.nexus.NexusClient;
33 import de.smartics.maven.nexus.NexusRequest;
34 import de.smartics.maven.nexus.NexusUtils;
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class SyncProductMojo extends UpdateProductMojo
49 {
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 private String releasedVersion;
95
96
97
98
99
100
101
102
103
104
105 private String nexusServer;
106
107
108
109
110
111
112
113
114
115 private String extension;
116
117
118
119
120
121
122
123
124
125
126
127 private String repositoryId;
128
129
130
131
132
133
134
135
136
137 private boolean skipProductUpdate;
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 @Override
157 public final void run() throws MojoExecutionException, MojoFailureException
158 {
159 if (!skipProductUpdate)
160 {
161 super.run();
162 }
163
164 final MavenProject project = getProject();
165 final MojoHelperSync helper =
166 new MojoHelperSync(project, commandFactory, console);
167
168 final String previousVersion = calculatePreviousVersion(project);
169 helper.execute(previousVersion);
170 }
171
172
173
174 private String calculatePreviousVersion(final MavenProject project)
175 throws MojoExecutionException, MojoFailureException
176 {
177 final Log log = getLog();
178 final LogLevel logLevel = new LogLevel(verbose);
179
180 final String previousVersion;
181 if ("NEXUS".equals(releasedVersion))
182 {
183 final NexusClient client =
184 new NexusClient(new HttpClient(), log, logLevel);
185 final NexusRequest request = createNexusRequest(project);
186 try
187 {
188 previousVersion = client.getLatestReleaseVersion(request);
189 }
190 catch (final CommandException e)
191 {
192 throw new MojoFailureException(
193 "Cannot determine latest release version on Nexus.", e);
194 }
195 }
196 else
197 {
198 final VersionHelper versionHelper = new VersionHelper(project);
199 try
200 {
201 previousVersion =
202 versionHelper.calculatePreviousVersion(releasedVersion);
203
204 }
205 catch (final IllegalArgumentException e)
206 {
207 throw new MojoExecutionException("Cannot calculate previous version: "
208 + e.getMessage(), e);
209 }
210 }
211
212 return previousVersion;
213 }
214
215 private NexusRequest createNexusRequest(final MavenProject project)
216 throws MojoExecutionException
217 {
218 final DeploymentRepository repository =
219 getDeploymentRepository(project.getDistributionManagement());
220 final String nexusServerUrl = getNexusServerUrl(repository);
221 final String repositoryId = getRepositoryId(repository);
222 final String groupId = project.getGroupId();
223 final String artifactId = project.getArtifactId();
224 final String extension = map(project.getPackaging(), this.extension);
225 return new NexusRequest(nexusServerUrl, repositoryId, groupId, artifactId,
226 extension);
227 }
228
229 private static String map(final String packaging, final String extension)
230 {
231 if (StringUtils.isNotBlank(extension))
232 {
233 return extension;
234 }
235 return mapToExtension(packaging);
236 }
237
238 private static DeploymentRepository getDeploymentRepository(
239 final DistributionManagement distributionManagement)
240 {
241 if (distributionManagement != null)
242 {
243 final DeploymentRepository repository =
244 distributionManagement.getRepository();
245 return repository;
246 }
247 return null;
248 }
249
250 private String getNexusServerUrl(final DeploymentRepository repository)
251 throws MojoExecutionException
252 {
253 if (StringUtils.isNotBlank(nexusServer))
254 {
255 return nexusServer;
256 }
257
258 if (repository != null)
259 {
260 final String url = repository.getUrl();
261
262 if (StringUtils.isBlank(url))
263 {
264 throw new MojoExecutionException(
265 "No valid Nexus server URL can be determined. Neiter deployment"
266 + " repository in the POM specified an URL nor is it specified"
267 + " via nexusServer.");
268 }
269
270 final String nexusServer = NexusUtils.calcNexusServerUrl(url);
271 if (StringUtils.isNotBlank(nexusServer))
272 {
273 return nexusServer;
274 }
275 }
276
277 throw new MojoExecutionException(
278 "No valid Nexus server URL can be determined. Neither a deployment"
279 + " repository specified in the POM nor specified via nexusServer.");
280 }
281
282 private String getRepositoryId(final DeploymentRepository repository)
283 throws MojoExecutionException
284 {
285 final String id;
286
287 if (StringUtils.isNotBlank(repositoryId))
288 {
289 id = repositoryId;
290 }
291 else if (repository != null)
292 {
293 id = repository.getId();
294 if (StringUtils.isBlank(id))
295 {
296 throw new MojoExecutionException(
297 "No valid repository ID can be determined. Not found in the"
298 + " release repository's element nor specified via repositoryId.");
299 }
300 }
301 else
302 {
303 throw new MojoExecutionException(
304 "No valid repository ID can be determined. Neither a deployment"
305 + " repository specified in the POM nor specified via repositoryId.");
306 }
307
308 return id;
309 }
310
311 }