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