@@ -52,11 +52,6 @@ public class UpgradeDependencyVersion extends ScanningRecipe<UpgradeDependencyVe
5252 @ EqualsAndHashCode .Exclude
5353 transient MavenMetadataFailures metadataFailures = new MavenMetadataFailures (this );
5454
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-
6055 @ Option (displayName = "Group" ,
6156 description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression." ,
6257 example = "com.fasterxml.jackson*" )
@@ -156,8 +151,7 @@ public Xml.Tag visitTag(final Xml.Tag tag, final ExecutionContext ctx) {
156151 Optional <Xml .Tag > version = tag .getChild ("version" );
157152 if (version .isPresent ()) {
158153 String requestedVersion = d .getRequested ().getVersion ();
159- if (requestedVersion != null && requestedVersion .startsWith ("${" ) &&
160- !implicitlyDefinedVersionProperties .contains (requestedVersion )) {
154+ if (isProperty (requestedVersion )) {
161155 String propertyName = requestedVersion .substring (2 , requestedVersion .length () - 1 );
162156 if (!getResolutionResult ().getPom ().getRequested ().getProperties ().containsKey (propertyName )) {
163157 storeParentPomProperty (getResolutionResult ().getParent (), propertyName , newerVersion );
@@ -180,8 +174,7 @@ public Xml.Tag visitTag(final Xml.Tag tag, final ExecutionContext ctx) {
180174 * @param propertyName the name of the property to update, if found in any the parent pom source file
181175 * @param newerVersion the resolved newer version that any matching parent pom property should be updated to
182176 */
183- private void storeParentPomProperty (
184- @ Nullable MavenResolutionResult currentMavenResolutionResult , String propertyName , String newerVersion ) {
177+ private void storeParentPomProperty (@ Nullable MavenResolutionResult currentMavenResolutionResult , String propertyName , String newerVersion ) {
185178 if (currentMavenResolutionResult == null ) {
186179 return ; // No parent contained the property; might then be in the same source file, or an import BOM
187180 }
@@ -270,10 +263,8 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
270263 }
271264
272265 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 ()));
277268 }
278269 }
279270
@@ -284,26 +275,13 @@ private Xml.Tag upgradeDependency(ExecutionContext ctx, Xml.Tag t) throws MavenD
284275 // as a source file, attempt to find a new version.
285276 String newerVersion = findNewerVersion (d .getGroupId (), d .getArtifactId (), d .getVersion (), ctx );
286277 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 );
298280 } else if (Boolean .TRUE .equals (overrideManagedVersion )) {
299281 ResolvedManagedDependency dm = findManagedDependency (t );
300282 // if a managed dependency is expressed as a property, change the property value
301283 // 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 ) {
307285 doAfterVisit (new ChangePropertyValue (dm .getRequested ().getVersion ().substring (2 ,
308286 dm .getRequested ().getVersion ().length () - 1 ),
309287 newerVersion , overrideManagedVersion , false ).getVisitor ());
@@ -359,20 +337,14 @@ private Xml.Tag upgradePluginDependency(ExecutionContext ctx, Xml.Tag t) throws
359337 if (groupId != null && artifactId != null && version != null ) {
360338 String newerVersion = findNewerVersion (groupId , artifactId , resolveVersion (version ), ctx );
361339 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 );
369341 }
370342 }
371343 return t ;
372344 }
373345
374346 private String resolveVersion (String version ) {
375- if (version . startsWith ( "${" ) && ! implicitlyDefinedVersionProperties . contains (version )) {
347+ if (isProperty (version )) {
376348 Map <String , String > properties = getResolutionResult ().getPom ().getProperties ();
377349 String property = version .substring (2 , version .length () - 1 );
378350 return properties .getOrDefault (property , version );
@@ -384,7 +356,7 @@ private String resolveVersion(String version) {
384356 String newerVersion = findNewerVersion (groupId , artifactId , version2 , ctx );
385357 if (newerVersion == null ) {
386358 return null ;
387- } else if (requestedVersion != null && requestedVersion . startsWith ( "${" )) {
359+ } else if (isProperty ( requestedVersion )) {
388360 //noinspection unchecked
389361 return (TreeVisitor <Xml , ExecutionContext >) new ChangePropertyValue (requestedVersion .substring (2 , requestedVersion .length () - 1 ), newerVersion , overrideManagedVersion , false )
390362 .getVisitor ();
0 commit comments