/
AbstractVersionsReport.java
344 lines (306 loc) · 10.3 KB
/
AbstractVersionsReport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package org.codehaus.mojo.versions;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Settings;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.DefaultVersionsHelper;
import org.codehaus.mojo.versions.api.VersionsHelper;
import org.codehaus.plexus.i18n.I18N;
import java.io.File;
import java.util.List;
import java.util.Locale;
/**
* Base class for all versions reports.
*
* @author Stephen Connolly
* @since 1.0-alpha-3
*/
public abstract class AbstractVersionsReport
extends AbstractMavenReport
{
/**
* Internationalization component.
*
* @since 1.0-alpha-3
*/
@Component
protected I18N i18n;
@Component
protected RepositorySystem repositorySystem;
/**
* @since 1.0-alpha-3
*/
@Component
private ArtifactResolver resolver;
/**
* Skip entire check.
*
* @since 1.0-alpha-3
*/
@Parameter( property = "versions.skip" )
private boolean skip;
/**
* The artifact metadata source to use.
*
* @since 1.0-alpha-1
*/
@Component
protected ArtifactMetadataSource artifactMetadataSource;
/**
* @since 1.0-alpha-3
*/
@Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true )
protected List<ArtifactRepository> remoteArtifactRepositories;
/**
* @since 1.0-alpha-3
*/
@Parameter( defaultValue = "${project.pluginArtifactRepositories}", readonly = true )
protected List<ArtifactRepository> remotePluginRepositories;
/**
* @since 1.0-alpha-1
*/
@Parameter( defaultValue = "${localRepository}", readonly = true )
protected ArtifactRepository localRepository;
/**
* @since 1.0-alpha-3
*/
@Component
private WagonManager wagonManager;
/**
* @since 1.0-alpha-3
*/
@Parameter( defaultValue = "${settings}", readonly = true )
private Settings settings;
/**
* settings.xml's server id for the URL. This is used when wagon needs extra authentication information.
*
* @since 1.0-alpha-3
*/
@Parameter( property = "maven.version.rules.serverId", defaultValue = "serverId" )
private String serverId;
/**
* URI of a ruleSet file containing the rules that control how to compare
* version numbers. The URI could be either a Wagon URI or a classpath URI
* (e.g. <code>classpath:///package/sub/package/rules.xml</code>).
*
* @since 1.0-alpha-3
*/
@Parameter( property = "maven.version.rules" )
private String rulesUri;
/**
* The versioning rule to use when comparing versions. Valid values are <code>maven</code>, <code>numeric</code>
* which will handle long version numbers provided all components are numeric, or <code>mercury</code> which will
* use the mercury version number comparison rules.
*
* @since 1.0-alpha-1
*/
@Parameter( property = "comparisonMethod" )
protected String comparisonMethod;
/**
* Whether to allow snapshots when searching for the latest version of an artifact.
*
* @since 1.0-alpha-3
*/
@Parameter( property = "allowSnapshots", defaultValue = "false" )
protected boolean allowSnapshots;
/**
* Our versions helper.
*/
private VersionsHelper helper;
/**
* The Maven Session.
*
* @since 1.0-beta-1
*/
@Parameter( defaultValue = "${session}", required = true, readonly = true )
protected MavenSession session;
@Parameter( defaultValue = "${mojoExecution}", required = true, readonly = true )
private MojoExecution mojoExecution;
@Component
protected ArtifactResolver artifactResolver;
// --------------------- GETTER / SETTER METHODS ---------------------
public VersionsHelper getHelper()
throws MavenReportException
{
if ( helper == null )
{
try
{
helper = new DefaultVersionsHelper( repositorySystem, artifactResolver, artifactMetadataSource,
remoteArtifactRepositories, remotePluginRepositories,
localRepository, wagonManager, settings, serverId, rulesUri,
getLog(), session, mojoExecution );
}
catch ( MojoExecutionException e )
{
throw new MavenReportException( e.getMessage(), e );
}
}
return helper;
}
/**
* {@inheritDoc}
*/
protected void executeReport( Locale locale )
throws MavenReportException
{
if ( !skip )
{
try
{
doGenerateReport( locale, getSink() );
}
catch ( MojoExecutionException e )
{
throw new MavenReportException( e.getMessage(), e );
}
}
}
/**
* generates the report.
*
* @param locale the locale to generate the report for.
* @param sink the report formatting tool.
* @throws MavenReportException when things go wrong.
* @throws MojoExecutionException if something goes wrong.
*/
protected abstract void doGenerateReport( Locale locale, Sink sink )
throws MavenReportException, MojoExecutionException;
/**
* Finds the latest version of the specified artifact that matches the version range.
*
* @param artifact The artifact.
* @param versionRange The version range.
* @param allowingSnapshots <code>null</code> for no override, otherwise the local override to apply.
* @param usePluginRepositories Use plugin repositories
* @return The latest version of the specified artifact that matches the specified version range or
* <code>null</code> if no matching version could be found.
* @throws MavenReportException If the artifact metadata could not be found.
* @since 1.0-alpha-1
*/
protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange versionRange,
boolean allowingSnapshots, boolean usePluginRepositories )
throws MavenReportException
{
boolean includeSnapshots = this.allowSnapshots;
if ( allowingSnapshots )
{
includeSnapshots = true;
}
if ( allowingSnapshots )
{
includeSnapshots = false;
}
try
{
final ArtifactVersions artifactVersions =
getHelper().lookupArtifactVersions( artifact, usePluginRepositories );
return artifactVersions.getNewestVersion( versionRange, includeSnapshots );
}
catch ( ArtifactMetadataRetrievalException e )
{
throw new MavenReportException( e.getMessage(), e );
}
}
/**
* @see org.apache.maven.reporting.AbstractMavenReport#getProject()
*/
protected MavenProject getProject()
{
return project;
}
/**
* @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
*/
protected String getOutputDirectory()
{
if ( !outputDirectory.isAbsolute() )
{
outputDirectory = new File( project.getBasedir(), outputDirectory.getPath() );
}
return outputDirectory.getAbsolutePath();
}
/**
* @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
*/
protected Renderer getSiteRenderer()
{
return siteRenderer;
}
/**
* @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
*/
public String getDescription( Locale locale )
{
return getText( locale, "report.description" );
}
/**
* @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
*/
public String getName( Locale locale )
{
return getText( locale, "report.title" );
}
/**
* Gets the localized message for this report.
*
* @param locale the locale.
* @param key the message key.
* @return the message.
*/
public String getText( Locale locale, String key )
{
return i18n.getString( getOutputName(), locale, key );
}
public Boolean getAllowSnapshots()
{
return this.allowSnapshots;
}
public String getComparisonMethod()
{
return comparisonMethod;
}
public ArtifactResolver getResolver()
{
return resolver;
}
public I18N getI18n()
{
return i18n;
}
}