forked from mojohaus/versions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractVersionsDependencyUpdaterMojo.java
514 lines (460 loc) · 16.1 KB
/
AbstractVersionsDependencyUpdaterMojo.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Parent;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
/**
* Base class for a mojo that updates dependency versions.
*
* @author Paul Gier
* @author Stephen Connolly
* @since 1.0-alpha-3
*/
public abstract class AbstractVersionsDependencyUpdaterMojo
extends AbstractVersionsUpdaterMojo
{
private static final String END_RANGE_CHARS = "])";
private static final String START_RANGE_CHARS = "[(";
/**
* A comma separated list of artifact patterns to include. Follows the pattern
* "groupId:artifactId:type:classifier:version". Designed to allow specifing the set of includes from the command
* line. When specifying includes from the pom, use the {@link #includes} configuration instead. If this property is
* specified then the {@link # include} configuration is ignored.
*
* @since 1.0-beta-1
*/
@Parameter( property = "includes" )
private String includesList = null;
/**
* A comma separated list of artifact patterns to exclude. Follows the pattern
* "groupId:artifactId:type:classifier:version". Designed to allow specifing the set of excludes from the command
* line. When specifying excludes from the pom, use the {@link #excludes} configuration instead. If this property is
* specified then the {@link # exclude} configuration is ignored.
*
* @since 1.0-beta-1
*/
@Parameter( property = "excludes" )
private String excludesList = null;
/**
* A list of artifact patterns to include. Follows the pattern "groupId:artifactId:type:classifier:version". This
* configuration setting is ignored if {@link #includesList} is defined.
*
* @since 1.0-beta-1
*/
@Parameter
private String[] includes = null;
/**
* A list of artifact patterns to exclude. Follows the pattern "groupId:artifactId:type:classifier:version". This
* configuration setting is ignored if {@link #excludesList} is defined.
*
* @since 1.0-beta-1
*/
@Parameter
private String[] excludes = null;
/**
* Whether to process the dependencies section of the project.
*
* @since 1.0-alpha-3
*/
@Parameter( property = "processDependencies", defaultValue = "true" )
private boolean processDependencies;
/**
* Whether to process the dependencyManagement section of the project.
*
* @since 1.0-alpha-3
*/
@Parameter( property = "processDependencyManagement", defaultValue = "true" )
private boolean processDependencyManagement;
/**
* Whether to process the parent section of the project. If not set will default to false.
*
* @since 2.3
*/
@Parameter( property = "processParent", defaultValue = "false" )
private boolean processParent = false;
/**
* Artifact filter to determine if artifact should be included
*
* @since 1.0-alpha-3
*/
private PatternIncludesArtifactFilter includesFilter;
/**
* Artifact filter to determine if artifact should be excluded
*
* @since 1.0-alpha-3
*/
private PatternExcludesArtifactFilter excludesFilter;
/**
* Whether to skip processing dependencies that are produced as part of the current reactor.
*
* @since 1.0-alpha-3
*/
@Parameter( property = "excludeReactor", defaultValue = "true" )
private boolean excludeReactor;
/**
* Should the project/dependencies section of the pom be processed.
*
* @return returns <code>true</code> if the project/dependencies section of the pom should be processed.
* @since 1.0-alpha-3
*/
public boolean isProcessingDependencies()
{
return processDependencies;
}
/**
* Should the project/dependencyManagement section of the pom be processed.
*
* @return returns <code>true</code> if the project/dependencyManagement section of the pom should be processed.
* @since 1.0-alpha-3
*/
public boolean isProcessingDependencyManagement()
{
return processDependencyManagement;
}
/**
* Should the project/parent section of the pom be processed.
*
* @return returns <code>true</code> if the project/parent section of the pom should be processed.
* @since 2.3
*/
public boolean isProcessingParent()
{
return processParent;
}
/**
* Should the artifacts produced in the current reactor be excluded from processing.
*
* @return returns <code>true</code> if the artifacts produced in the current reactor should be excluded from processing.
* @since 1.0-alpha-3
*/
public boolean isExcludeReactor()
{
return excludeReactor;
}
/**
* Should the dependency be updated itself or is it handled by properties.
*
* @param dependency Dependency
* @return true if the version starts with '${'
* @since 2.8
*/
protected boolean isHandledByProperty(Dependency dependency) {
String version = dependency.getVersion();
return version.startsWith("${");
}
/**
* Try to find the dependency artifact that matches the given dependency.
*
* @param dependency Dependency
* @return Artifact
* @since 1.0-alpha-3
*/
protected Artifact findArtifact( Dependency dependency )
{
if ( getProject().getDependencyArtifacts() == null )
{
return null;
}
for ( Artifact artifact : getProject().getDependencyArtifacts() )
{
if ( compare( artifact, dependency ) )
{
return artifact;
}
}
return null;
}
/**
* Try to find the dependency artifact that matches the given dependency.
*
* @param dependency Dependency
* @throws MojoExecutionException Mojo execution exception
* @return Artifact
* @since 1.0-alpha-3
*/
protected Artifact toArtifact( Dependency dependency )
throws MojoExecutionException
{
Artifact artifact = findArtifact( dependency );
if ( artifact == null )
{
try
{
return getHelper().createDependencyArtifact( dependency );
}
catch ( InvalidVersionSpecificationException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
return artifact;
}
protected Artifact toArtifact( Parent model )
throws MojoExecutionException
{
Dependency d = new Dependency();
d.setArtifactId( model.getArtifactId() );
d.setGroupId( model.getGroupId() );
d.setVersion( model.getVersion() );
d.setType( "pom" );
d.setScope( Artifact.SCOPE_COMPILE );
return this.toArtifact( d );
}
protected String toString( MavenProject project )
{
StringBuilder buf = new StringBuilder();
buf.append( project.getGroupId() );
buf.append( ':' );
buf.append( project.getArtifactId() );
if ( project.getVersion() != null && project.getVersion().length() > 0 )
{
buf.append( ":" );
buf.append( project.getVersion() );
}
return buf.toString();
}
protected String toString( Dependency d )
{
return getString(d.getGroupId(), d.getArtifactId(), d.getType(), d.getClassifier(), d.getVersion());
}
protected String toString( Artifact a )
{
return getString(a.getGroupId(), a.getArtifactId(), a.getType(), a.getClassifier(), a.getVersion());
}
private String getString(String groupId, String artifactId, String type, String classifier, String version) {
StringBuilder buf = new StringBuilder();
buf.append(groupId);
buf.append( ':' );
buf.append(artifactId);
if ( type != null && type.length() > 0 )
{
buf.append( ':' );
buf.append(type);
}
else
{
buf.append( ":jar" );
}
if ( classifier != null && classifier.length() > 0 )
{
buf.append( ':' );
buf.append(classifier);
}
if ( version != null && version.length() > 0 )
{
buf.append( ":" );
buf.append(version);
}
return buf.toString();
}
/**
* Returns <code>true</code> if the dependency is produced by the current reactor.
*
* @param dependency the dependency to heck.
* @return <code>true</code> if the dependency is produced by the current reactor.
* @since 1.0-alpha-3
*/
protected boolean isProducedByReactor( Dependency dependency )
{
for ( Object reactorProject : reactorProjects ) {
MavenProject project = (MavenProject) reactorProject;
if ( compare(project, dependency) ) {
return true;
}
}
return false;
}
/**
* Compare a project to a dependency. Returns true only if the groupId and artifactId are all equal.
*
* @param project the project
* @param dep the dependency
* @return true if project and dep refer to the same artifact
*/
private boolean compare( MavenProject project, Dependency dep )
{
if ( !StringUtils.equals( project.getGroupId(), dep.getGroupId() ) )
{
return false;
}
return project.getArtifactId().equals( dep.getArtifactId() );
}
/**
* Compare and artifact to a dependency. Returns true only if the groupId, artifactId, type, and classifier are all
* equal.
*
* @param artifact Artifact
* @param dep Dependency
* @return true if artifact and dep refer to the same artifact
*/
private boolean compare( Artifact artifact, Dependency dep )
{
if ( !StringUtils.equals( artifact.getGroupId(), dep.getGroupId() ) )
{
return false;
}
if ( !StringUtils.equals( artifact.getArtifactId(), dep.getArtifactId() ) )
{
return false;
}
if ( !StringUtils.equals( artifact.getType(), dep.getType() ) )
{
return false;
}
return StringUtils.equals( artifact.getClassifier(), dep.getClassifier() );
}
/**
* Determine if the artifact is included in the list of artifacts to be processed.
*
* @param artifact The artifact we want to check.
* @return true if the artifact should be processed, false otherwise.
*/
protected boolean isIncluded( Artifact artifact )
{
boolean result = true;
ArtifactFilter includesFilter = this.getIncludesArtifactFilter();
if ( includesFilter != null )
{
result = includesFilter.include( artifact );
}
ArtifactFilter excludesFilter = this.getExcludesArtifactFilter();
if ( excludesFilter != null )
{
result = result && excludesFilter.include( artifact );
}
return result;
}
/**
* Indicates whether any includes were specified via the 'includes' or 'includesList' options.
*
* @return true if includes were specified, false otherwise.
*/
protected boolean hasIncludes()
{
return includes != null || includesList != null;
}
private ArtifactFilter getIncludesArtifactFilter()
{
if ( includesFilter == null && ( includes != null || includesList != null ) )
{
List<String> patterns = new ArrayList<>();
if ( this.includesList != null )
{
patterns.addAll( separatePatterns( includesList ) );
}
else if ( includes != null )
{
patterns.addAll( Arrays.asList( includes ) );
}
includesFilter = new PatternIncludesArtifactFilter( patterns );
}
return includesFilter;
}
private ArtifactFilter getExcludesArtifactFilter()
{
if ( excludesFilter == null && ( excludes != null || excludesList != null ) )
{
List<String> patterns = new ArrayList<>();
if ( excludesList != null )
{
patterns.addAll( separatePatterns( excludesList ) );
}
else if ( excludes != null )
{
patterns.addAll( Arrays.asList( excludes ) );
}
excludesFilter = new PatternExcludesArtifactFilter( patterns );
}
return excludesFilter;
}
/**
* To handle multiple includes with version range like "group:artifact:jar:[1.0.0,2.2)", we have to use a parsing a
* little bit more complex than split().
*
* @param includeString the string to parse
* @return list of patterns
*/
protected List<String> separatePatterns( String includeString )
{
if ( includeString == null )
{
return Collections.emptyList();
}
List<String> patterns = new ArrayList<>();
int indexOf = nextCommaIndex( includeString );
while ( indexOf >= 0 )
{
patterns.add( includeString.substring( 0, indexOf ) );
includeString = includeString.substring( indexOf + 1 );
indexOf = nextCommaIndex( includeString );
}
patterns.add( includeString );
return patterns;
}
private int nextCommaIndex( final String includeString )
{
int indexOfComma = includeString.indexOf( ',' );
int nextRangeStartDelimiterIndex = findFirstChar( includeString, START_RANGE_CHARS );
if ( nextRangeStartDelimiterIndex >= 0 )
{
if ( !( indexOfComma >= 0 && indexOfComma < nextRangeStartDelimiterIndex ) )
{
int nextStopDelimiterIndex = findFirstChar( includeString, END_RANGE_CHARS );
// recursive call
int tmp = nextCommaIndex( includeString.substring( nextStopDelimiterIndex + 1 ) );
indexOfComma = ( tmp >= 0 ) ? nextStopDelimiterIndex + 1 + tmp : -1;
}
}
return indexOfComma;
}
private int findFirstChar( final String includeString, final String chars )
{
int nextRangeStartDelimiterIndex = -1;
char[] delimiters = chars.toCharArray();
for ( char delimiter : delimiters )
{
int index = includeString.indexOf( delimiter );
if ( index >= 0 && nextRangeStartDelimiterIndex >= 0 )
{
nextRangeStartDelimiterIndex = Math.min( index, nextRangeStartDelimiterIndex );
}
else
{
if ( index >= 0 )
{
nextRangeStartDelimiterIndex = index;
}
}
}
return nextRangeStartDelimiterIndex;
}
}