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
213 return new NexusRequest(nexusServerUrl, repositoryId, groupId, artifactId);
214 }
215
216 private static DeploymentRepository getDeploymentRepository(
217 final DistributionManagement distributionManagement)
218 {
219 if (distributionManagement != null)
220 {
221 final DeploymentRepository repository =
222 distributionManagement.getRepository();
223 return repository;
224 }
225 return null;
226 }
227
228 private String getNexusServerUrl(final DeploymentRepository repository)
229 throws MojoExecutionException
230 {
231 if (StringUtils.isNotBlank(nexusServer))
232 {
233 return nexusServer;
234 }
235
236 if (repository != null)
237 {
238 final String url = repository.getUrl();
239
240 if (StringUtils.isBlank(url))
241 {
242 throw new MojoExecutionException(
243 "No valid Nexus server URL can be determined. Neiter deployment"
244 + " repository in the POM specified an URL nor is it specified"
245 + " via nexusServer.");
246 }
247
248 final String nexusServer = NexusUtils.calcNexusServerUrl(url);
249 if (StringUtils.isNotBlank(nexusServer))
250 {
251 return nexusServer;
252 }
253 }
254
255 throw new MojoExecutionException(
256 "No valid Nexus server URL can be determined. Neither a deployment"
257 + " repository specified in the POM nor specified via nexusServer.");
258 }
259
260 private String getRepositoryId(final DeploymentRepository repository)
261 throws MojoExecutionException
262 {
263 final String id;
264
265 if (StringUtils.isNotBlank(repositoryId))
266 {
267 id = repositoryId;
268 }
269 else if (repository != null)
270 {
271 id = repository.getId();
272 if (StringUtils.isBlank(id))
273 {
274 throw new MojoExecutionException(
275 "No valid repository ID can be determined. Not found in the"
276 + " release repository's element nor specified via repositoryId.");
277 }
278 }
279 else
280 {
281 throw new MojoExecutionException(
282 "No valid repository ID can be determined. Neither a deployment"
283 + " repository specified in the POM nor specified via repositoryId.");
284 }
285
286 return id;
287 }
288
289 }