@@ -54,13 +54,17 @@ public class ChangeManagedDependencyGroupIdAndArtifactId extends Recipe {
5454 String oldArtifactId ;
5555
5656 @ Option (displayName = "New groupId" ,
57- description = "The new groupId to use." ,
58- example = "corp.internal.openrewrite.recipe" )
57+ description = "The new groupId to use. Defaults to the existing group id." ,
58+ example = "corp.internal.openrewrite.recipe" ,
59+ required = false )
60+ @ Nullable
5961 String newGroupId ;
6062
6163 @ Option (displayName = "New artifactId" ,
62- description = "The new artifactId to use." ,
63- example = "rewrite-testing-frameworks" )
64+ description = "The new artifactId to use. Defaults to the existing artifact id." ,
65+ example = "rewrite-testing-frameworks" ,
66+ required = false )
67+ @ Nullable
6468 String newArtifactId ;
6569
6670 @ Option (displayName = "New version" ,
@@ -78,7 +82,7 @@ public class ChangeManagedDependencyGroupIdAndArtifactId extends Recipe {
7882 @ Nullable
7983 String versionPattern ;
8084
81- public ChangeManagedDependencyGroupIdAndArtifactId (String oldGroupId , String oldArtifactId , String newGroupId , String newArtifactId , @ Nullable String newVersion ) {
85+ public ChangeManagedDependencyGroupIdAndArtifactId (String oldGroupId , String oldArtifactId , @ Nullable String newGroupId , @ Nullable String newArtifactId , @ Nullable String newVersion ) {
8286 this .oldGroupId = oldGroupId ;
8387 this .oldArtifactId = oldArtifactId ;
8488 this .newGroupId = newGroupId ;
@@ -88,7 +92,7 @@ public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String old
8892 }
8993
9094 @ JsonCreator
91- public ChangeManagedDependencyGroupIdAndArtifactId (String oldGroupId , String oldArtifactId , String newGroupId , String newArtifactId , @ Nullable String newVersion , @ Nullable String versionPattern ) {
95+ public ChangeManagedDependencyGroupIdAndArtifactId (String oldGroupId , String oldArtifactId , @ Nullable String newGroupId , @ Nullable String newArtifactId , @ Nullable String newVersion , @ Nullable String versionPattern ) {
9296 this .oldGroupId = oldGroupId ;
9397 this .oldArtifactId = oldArtifactId ;
9498 this .newGroupId = newGroupId ;
@@ -126,12 +130,10 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
126130 final VersionComparator versionComparator = newVersion != null ? Semver .validate (newVersion , versionPattern ).getValue () : null ;
127131 @ Nullable
128132 private Collection <String > availableVersions ;
129- private boolean isNewDependencyPresent ;
130133 private Set <String > safeVersionPlaceholdersToChange = new HashSet <>();
131134
132135 @ Override
133136 public Xml .Document visitDocument (Xml .Document document , ExecutionContext ctx ) {
134- isNewDependencyPresent = checkIfNewDependencyPresent (newGroupId , newArtifactId , newVersion );
135137 safeVersionPlaceholdersToChange = getSafeVersionPlaceholdersToChange (oldGroupId , oldArtifactId , ctx );
136138 return super .visitDocument (document , ctx );
137139 }
@@ -147,29 +149,26 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
147149 }
148150 }
149151
150- if (! newGroupId . equals ( oldGroupId ) ) {
152+ if (newGroupId != null ) {
151153 t = (Xml .Tag ) new ChangeTagValueVisitor <>(t .getChild ("groupId" ).orElse (null ), newGroupId ).visitNonNull (t , ctx );
152154 }
153- if (! newArtifactId . equals ( oldArtifactId ) ) {
155+ if (newArtifactId != null ) {
154156 t = (Xml .Tag ) new ChangeTagValueVisitor <>(t .getChild ("artifactId" ).orElse (null ), newArtifactId ).visitNonNull (t , ctx );
155157 }
156158 if (newVersion != null ) {
157159 try {
158160 Optional <Xml .Tag > versionTag = t .getChild ("version" );
159161 if (versionTag .isPresent ()) {
160- String resolvedGroupId = t .getChildValue ("groupId" ).orElse (newGroupId );
161- String resolvedArtifactId = t .getChildValue ("artifactId" ).orElse (newArtifactId );
162+ ResolvedPom pom = getResolutionResult ().getPom ();
163+ String resolvedGroupId = t .getChildValue ("groupId" ).orElse (newGroupId != null ? newGroupId : oldGroupId );
164+ String resolvedArtifactId = t .getChildValue ("artifactId" ).orElse (newArtifactId != null ? newArtifactId : oldArtifactId );
162165 if (resolvedArtifactId .contains ("${" )) {
163- ResolvedPom pom = getResolutionResult ().getPom ();
164- Map <String , String > properties = pom .getProperties ();
165- resolvedArtifactId = ResolvedPom .placeholderHelper .replacePlaceholders (resolvedArtifactId , properties ::get );
166+ resolvedArtifactId = ResolvedPom .placeholderHelper .replacePlaceholders (resolvedArtifactId , pom .getProperties ()::get );
166167 }
167168 if (resolvedGroupId .contains ("${" )) {
168- ResolvedPom pom = getResolutionResult ().getPom ();
169- Map <String , String > properties = pom .getProperties ();
170- resolvedGroupId = ResolvedPom .placeholderHelper .replacePlaceholders (resolvedGroupId , properties ::get );
169+ resolvedGroupId = ResolvedPom .placeholderHelper .replacePlaceholders (resolvedGroupId , pom .getProperties ()::get );
171170 }
172- String resolvedNewVersion = resolveSemverVersion (ctx , resolvedGroupId , resolvedArtifactId , getResolutionResult (). getPom () .getValue (versionTag .get ().getValue ().orElse (null )));
171+ String resolvedNewVersion = resolveSemverVersion (ctx , resolvedGroupId , resolvedArtifactId , pom .getValue (versionTag .get ().getValue ().orElse (null )));
173172 String versionTagValue = t .getChildValue ("version" ).orElse (null );
174173 if (versionTagValue == null || !safeVersionPlaceholdersToChange .contains (versionTagValue )) {
175174 t = (Xml .Tag ) new ChangeTagValueVisitor <>(versionTag .get (), resolvedNewVersion ).visitNonNull (t , ctx );
@@ -184,7 +183,9 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
184183 if (t != tag ) {
185184 maybeUpdateModel ();
186185 doAfterVisit (new RemoveRedundantDependencyVersions (null , null , null , null ).getVisitor ());
187- if (isNewDependencyPresent ) {
186+ String effectiveGroupId = newGroupId != null ? newGroupId : tag .getChildValue ("groupId" ).orElse (null );
187+ String effectiveArtifactId = newArtifactId != null ? newArtifactId : tag .getChildValue ("artifactId" ).orElse (null );
188+ if (checkIfNewDependencyPresent (effectiveGroupId , effectiveArtifactId , newVersion )) {
188189 doAfterVisit (new RemoveContentVisitor <>(t , true , true ));
189190 maybeUpdateModel ();
190191 }
0 commit comments