@@ -52,6 +52,11 @@ 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+
5560 @ Option (displayName = "Group" ,
5661 description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression." ,
5762 example = "com.fasterxml.jackson*" )
@@ -151,7 +156,8 @@ public Xml.Tag visitTag(final Xml.Tag tag, final ExecutionContext ctx) {
151156 Optional <Xml .Tag > version = tag .getChild ("version" );
152157 if (version .isPresent ()) {
153158 String requestedVersion = d .getRequested ().getVersion ();
154- if (isProperty (requestedVersion )) {
159+ if (requestedVersion != null && requestedVersion .startsWith ("${" ) &&
160+ !implicitlyDefinedVersionProperties .contains (requestedVersion )) {
155161 String propertyName = requestedVersion .substring (2 , requestedVersion .length () - 1 );
156162 if (!getResolutionResult ().getPom ().getRequested ().getProperties ().containsKey (propertyName )) {
157163 storeParentPomProperty (getResolutionResult ().getParent (), propertyName , newerVersion );
@@ -174,7 +180,8 @@ public Xml.Tag visitTag(final Xml.Tag tag, final ExecutionContext ctx) {
174180 * @param propertyName the name of the property to update, if found in any the parent pom source file
175181 * @param newerVersion the resolved newer version that any matching parent pom property should be updated to
176182 */
177- private void storeParentPomProperty (@ Nullable MavenResolutionResult currentMavenResolutionResult , String propertyName , String newerVersion ) {
183+ private void storeParentPomProperty (
184+ @ Nullable MavenResolutionResult currentMavenResolutionResult , String propertyName , String newerVersion ) {
178185 if (currentMavenResolutionResult == null ) {
179186 return ; // No parent contained the property; might then be in the same source file, or an import BOM
180187 }
@@ -263,8 +270,10 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
263270 }
264271
265272 private void retainVersions () {
266- RetainVersions .plan (this , retainVersions == null ? emptyList () : retainVersions )
267- .forEach (it -> doAfterVisit (it .getVisitor ()));
273+ for (Recipe retainVersionRecipe : RetainVersions .plan (this , retainVersions == null ?
274+ emptyList () : retainVersions )) {
275+ doAfterVisit (retainVersionRecipe .getVisitor ());
276+ }
268277 }
269278 }
270279
@@ -275,13 +284,26 @@ private Xml.Tag upgradeDependency(ExecutionContext ctx, Xml.Tag t) throws MavenD
275284 // as a source file, attempt to find a new version.
276285 String newerVersion = findNewerVersion (d .getGroupId (), d .getArtifactId (), d .getVersion (), ctx );
277286 if (newerVersion != null ) {
278- if (t .getChild ("version" ).isPresent ()) {
279- t = changeChildTagValue (t , "version" , newerVersion , overrideManagedVersion , ctx );
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+ }
280298 } else if (Boolean .TRUE .equals (overrideManagedVersion )) {
281299 ResolvedManagedDependency dm = findManagedDependency (t );
282300 // if a managed dependency is expressed as a property, change the property value
283301 // do this only when a requested bom is absent, otherwise changing property has no effect
284- if (dm != null && isProperty (dm .getRequested ().getVersion ()) && dm .getRequestedBom () == null ) {
302+ if (dm != null &&
303+ dm .getRequested ().getVersion () != null &&
304+ dm .getRequested ().getVersion ().startsWith ("${" ) &&
305+ !implicitlyDefinedVersionProperties .contains (dm .getRequested ().getVersion ()) &&
306+ dm .getRequestedBom () == null ) {
285307 doAfterVisit (new ChangePropertyValue (dm .getRequested ().getVersion ().substring (2 ,
286308 dm .getRequested ().getVersion ().length () - 1 ),
287309 newerVersion , overrideManagedVersion , false ).getVisitor ());
@@ -337,14 +359,20 @@ private Xml.Tag upgradePluginDependency(ExecutionContext ctx, Xml.Tag t) throws
337359 if (groupId != null && artifactId != null && version != null ) {
338360 String newerVersion = findNewerVersion (groupId , artifactId , resolveVersion (version ), ctx );
339361 if (newerVersion != null ) {
340- t = changeChildTagValue (t , "version" , newerVersion , overrideManagedVersion , ctx );
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+ }
341369 }
342370 }
343371 return t ;
344372 }
345373
346374 private String resolveVersion (String version ) {
347- if (isProperty (version )) {
375+ if (version . startsWith ( "${" ) && ! implicitlyDefinedVersionProperties . contains (version )) {
348376 Map <String , String > properties = getResolutionResult ().getPom ().getProperties ();
349377 String property = version .substring (2 , version .length () - 1 );
350378 return properties .getOrDefault (property , version );
@@ -356,7 +384,7 @@ private String resolveVersion(String version) {
356384 String newerVersion = findNewerVersion (groupId , artifactId , version2 , ctx );
357385 if (newerVersion == null ) {
358386 return null ;
359- } else if (isProperty ( requestedVersion )) {
387+ } else if (requestedVersion != null && requestedVersion . startsWith ( "${" )) {
360388 //noinspection unchecked
361389 return (TreeVisitor <Xml , ExecutionContext >) new ChangePropertyValue (requestedVersion .substring (2 , requestedVersion .length () - 1 ), newerVersion , overrideManagedVersion , false )
362390 .getVisitor ();
0 commit comments