@@ -52,11 +52,6 @@ public class UpgradeDependencyVersion extends ScanningRecipe<UpgradeDependencyVe
52
52
@ EqualsAndHashCode .Exclude
53
53
transient MavenMetadataFailures metadataFailures = new MavenMetadataFailures (this );
54
54
55
- // there are several implicitly defined version properties that we should never attempt to update
56
- private static final Collection <String > implicitlyDefinedVersionProperties = Arrays .asList (
57
- "${version}" , "${project.version}" , "${pom.version}" , "${project.parent.version}"
58
- );
59
-
60
55
@ Option (displayName = "Group" ,
61
56
description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression." ,
62
57
example = "com.fasterxml.jackson*" )
@@ -156,8 +151,7 @@ public Xml.Tag visitTag(final Xml.Tag tag, final ExecutionContext ctx) {
156
151
Optional <Xml .Tag > version = tag .getChild ("version" );
157
152
if (version .isPresent ()) {
158
153
String requestedVersion = d .getRequested ().getVersion ();
159
- if (requestedVersion != null && requestedVersion .startsWith ("${" ) &&
160
- !implicitlyDefinedVersionProperties .contains (requestedVersion )) {
154
+ if (isProperty (requestedVersion )) {
161
155
String propertyName = requestedVersion .substring (2 , requestedVersion .length () - 1 );
162
156
if (!getResolutionResult ().getPom ().getRequested ().getProperties ().containsKey (propertyName )) {
163
157
storeParentPomProperty (getResolutionResult ().getParent (), propertyName , newerVersion );
@@ -180,8 +174,7 @@ public Xml.Tag visitTag(final Xml.Tag tag, final ExecutionContext ctx) {
180
174
* @param propertyName the name of the property to update, if found in any the parent pom source file
181
175
* @param newerVersion the resolved newer version that any matching parent pom property should be updated to
182
176
*/
183
- private void storeParentPomProperty (
184
- @ Nullable MavenResolutionResult currentMavenResolutionResult , String propertyName , String newerVersion ) {
177
+ private void storeParentPomProperty (@ Nullable MavenResolutionResult currentMavenResolutionResult , String propertyName , String newerVersion ) {
185
178
if (currentMavenResolutionResult == null ) {
186
179
return ; // No parent contained the property; might then be in the same source file, or an import BOM
187
180
}
@@ -270,10 +263,8 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
270
263
}
271
264
272
265
private void retainVersions () {
273
- for (Recipe retainVersionRecipe : RetainVersions .plan (this , retainVersions == null ?
274
- emptyList () : retainVersions )) {
275
- doAfterVisit (retainVersionRecipe .getVisitor ());
276
- }
266
+ RetainVersions .plan (this , retainVersions == null ? emptyList () : retainVersions )
267
+ .forEach (it -> doAfterVisit (it .getVisitor ()));
277
268
}
278
269
}
279
270
@@ -284,26 +275,13 @@ private Xml.Tag upgradeDependency(ExecutionContext ctx, Xml.Tag t) throws MavenD
284
275
// as a source file, attempt to find a new version.
285
276
String newerVersion = findNewerVersion (d .getGroupId (), d .getArtifactId (), d .getVersion (), ctx );
286
277
if (newerVersion != null ) {
287
- Optional <Xml .Tag > version = t .getChild ("version" );
288
- if (version .isPresent ()) {
289
- String requestedVersion = d .getRequested ().getVersion ();
290
- if (requestedVersion != null && requestedVersion .startsWith ("${" ) && !implicitlyDefinedVersionProperties .contains (requestedVersion )) {
291
- String propertyName = requestedVersion .substring (2 , requestedVersion .length () - 1 );
292
- if (getResolutionResult ().getPom ().getRequested ().getProperties ().containsKey (propertyName )) {
293
- doAfterVisit (new ChangePropertyValue (propertyName , newerVersion , overrideManagedVersion , false ).getVisitor ());
294
- }
295
- } else {
296
- t = (Xml .Tag ) new ChangeTagValueVisitor <>(version .get (), newerVersion ).visitNonNull (t , 0 , getCursor ().getParentOrThrow ());
297
- }
278
+ if (t .getChild ("version" ).isPresent ()) {
279
+ t = changeChildTagValue (t , "version" , newerVersion , overrideManagedVersion , ctx );
298
280
} else if (Boolean .TRUE .equals (overrideManagedVersion )) {
299
281
ResolvedManagedDependency dm = findManagedDependency (t );
300
282
// if a managed dependency is expressed as a property, change the property value
301
283
// do this only when a requested bom is absent, otherwise changing property has no effect
302
- if (dm != null &&
303
- dm .getRequested ().getVersion () != null &&
304
- dm .getRequested ().getVersion ().startsWith ("${" ) &&
305
- !implicitlyDefinedVersionProperties .contains (dm .getRequested ().getVersion ()) &&
306
- dm .getRequestedBom () == null ) {
284
+ if (dm != null && isProperty (dm .getRequested ().getVersion ()) && dm .getRequestedBom () == null ) {
307
285
doAfterVisit (new ChangePropertyValue (dm .getRequested ().getVersion ().substring (2 ,
308
286
dm .getRequested ().getVersion ().length () - 1 ),
309
287
newerVersion , overrideManagedVersion , false ).getVisitor ());
@@ -359,20 +337,14 @@ private Xml.Tag upgradePluginDependency(ExecutionContext ctx, Xml.Tag t) throws
359
337
if (groupId != null && artifactId != null && version != null ) {
360
338
String newerVersion = findNewerVersion (groupId , artifactId , resolveVersion (version ), ctx );
361
339
if (newerVersion != null ) {
362
- if (version .startsWith ("${" ) && !implicitlyDefinedVersionProperties .contains (version )) {
363
- doAfterVisit (new ChangePropertyValue (version .substring (2 , version .length () - 1 ), newerVersion , overrideManagedVersion , false ).getVisitor ());
364
- } else {
365
- Optional <Xml .Tag > versionTag = t .getChild ("version" );
366
- assert versionTag .isPresent ();
367
- t = (Xml .Tag ) new ChangeTagValueVisitor <>(versionTag .get (), newerVersion ).visitNonNull (t , 0 , getCursor ().getParentOrThrow ());
368
- }
340
+ t = changeChildTagValue (t , "version" , newerVersion , overrideManagedVersion , ctx );
369
341
}
370
342
}
371
343
return t ;
372
344
}
373
345
374
346
private String resolveVersion (String version ) {
375
- if (version . startsWith ( "${" ) && ! implicitlyDefinedVersionProperties . contains (version )) {
347
+ if (isProperty (version )) {
376
348
Map <String , String > properties = getResolutionResult ().getPom ().getProperties ();
377
349
String property = version .substring (2 , version .length () - 1 );
378
350
return properties .getOrDefault (property , version );
@@ -384,7 +356,7 @@ private String resolveVersion(String version) {
384
356
String newerVersion = findNewerVersion (groupId , artifactId , version2 , ctx );
385
357
if (newerVersion == null ) {
386
358
return null ;
387
- } else if (requestedVersion != null && requestedVersion . startsWith ( "${" )) {
359
+ } else if (isProperty ( requestedVersion )) {
388
360
//noinspection unchecked
389
361
return (TreeVisitor <Xml , ExecutionContext >) new ChangePropertyValue (requestedVersion .substring (2 , requestedVersion .length () - 1 ), newerVersion , overrideManagedVersion , false )
390
362
.getVisitor ();
0 commit comments