Skip to content

Commit 67cd777

Browse files
Revert "Fixed situation where a changing a managed dependency that does not h…" (#6564)
This reverts commit d890c71.
1 parent 8d0b753 commit 67cd777

5 files changed

Lines changed: 24 additions & 545 deletions

File tree

rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
147147

148148
@Override
149149
public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
150-
isNewDependencyPresent = checkIfNewDependencyPresent(newGroupId, newArtifactId, newVersion);
150+
isNewDependencyPresent = checkIfNewDependencyPresents(newGroupId, newArtifactId, newVersion);
151151
if (!oldGroupId.contains("*") && !oldArtifactId.contains("*") &&
152152
(changeManagedDependency == null || changeManagedDependency)) {
153153
doAfterVisit(new ChangeManagedDependencyGroupIdAndArtifactId(
@@ -170,26 +170,16 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
170170
}
171171
boolean isPluginDependency = isPluginDependencyTag(oldGroupId, oldArtifactId);
172172
boolean isAnnotationProcessorPath = isAnnotationProcessorPathTag(oldGroupId, oldArtifactId);
173-
boolean deferUpdate = false;
174173
if (isOldDependencyTag || isPluginDependency || isAnnotationProcessorPath) {
175-
ResolvedPom rp = getResolutionResult().getPom();
176174
String groupId = newGroupId;
177175
if (groupId != null) {
178-
Optional<Xml.Tag> groupIdTag = t.getChild("groupId");
179-
Optional<String> groupIdValue = t.getChildValue("groupId");
180-
if (groupIdTag.isPresent() && !groupId.equals(rp.getValue(groupIdValue.orElse(null)))) {
181-
t = changeChildTagValue(t, "groupId", groupId, ctx);
182-
}
176+
t = changeChildTagValue(t, "groupId", groupId, ctx);
183177
} else {
184178
groupId = t.getChildValue("groupId").orElseThrow(NoSuchElementException::new);
185179
}
186180
String artifactId = newArtifactId;
187181
if (artifactId != null) {
188-
Optional<Xml.Tag> artifactIdTag = t.getChild("artifactId");
189-
Optional<String> artifactIdValue = t.getChildValue("artifactId");
190-
if (artifactIdTag.isPresent() && !artifactId.equals(rp.getValue(artifactIdValue.orElse(null)))) {
191-
t = changeChildTagValue(t, "artifactId", artifactId, ctx);
192-
}
182+
t = changeChildTagValue(t, "artifactId", artifactId, ctx);
193183
} else {
194184
artifactId = t.getChildValue("artifactId").orElseThrow(NoSuchElementException::new);
195185
}
@@ -202,37 +192,33 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
202192
Scope scope = scopeTag.map(xml -> Scope.fromName(xml.getValue().orElse("compile"))).orElse(Scope.Compile);
203193
Optional<Xml.Tag> versionTag = t.getChild("version");
204194

205-
boolean configuredToOverrideManagedVersion = overrideManagedVersion != null && overrideManagedVersion; // False by default
195+
boolean configuredToOverrideManageVersion = overrideManagedVersion != null && overrideManagedVersion; // False by default
206196
boolean configuredToChangeManagedDependency = changeManagedDependency == null || changeManagedDependency; // True by default
207197

208198
boolean versionTagPresent = versionTag.isPresent();
209199
// dependencyManagement does not apply to plugin dependencies or annotation processor paths
210-
boolean oldDependencyDefinedManaged = isOldDependencyTag && canAffectManagedDependency(getResolutionResult(), scope, oldGroupId, oldArtifactId);
200+
boolean oldDependencyManaged = isOldDependencyTag && isDependencyManaged(scope, oldGroupId, oldArtifactId);
211201
boolean newDependencyManaged = isOldDependencyTag && isDependencyManaged(scope, groupId, artifactId);
212-
213202
if (versionTagPresent) {
214203
// If the previous dependency had a version but the new artifact is managed, removed the version tag.
215-
if (!configuredToOverrideManagedVersion && newDependencyManaged || (oldDependencyDefinedManaged && configuredToChangeManagedDependency)) {
204+
if (!configuredToOverrideManageVersion && newDependencyManaged || (oldDependencyManaged && configuredToChangeManagedDependency)) {
216205
t = (Xml.Tag) new RemoveContentVisitor<>(versionTag.get(), false, true).visit(t, ctx);
217206
} else {
218207
// Otherwise, change the version to the new value.
219208
t = changeChildTagValue(t, "version", resolvedNewVersion, ctx);
220209
}
221-
} else if (configuredToOverrideManagedVersion || (!newDependencyManaged && !(oldDependencyDefinedManaged && configuredToChangeManagedDependency))) {
222-
// If the version is not present, add the version if we are explicitly overriding a managed version or if no managed version exists.
210+
} else if (configuredToOverrideManageVersion || !newDependencyManaged) {
211+
//If the version is not present, add the version if we are explicitly overriding a managed version or if no managed version exists.
223212
Xml.Tag newVersionTag = Xml.Tag.build("<version>" + resolvedNewVersion + "</version>");
224213
//noinspection ConstantConditions
225214
t = (Xml.Tag) new AddToTagVisitor<ExecutionContext>(t, newVersionTag, new MavenTagInsertionComparator(t.getChildren())).visitNonNull(t, ctx, getCursor().getParent());
226-
} else if (!newDependencyManaged) {
227-
// We leave it up to the managed dependency update to call `maybeUpdateModel()` instead to avoid interim dependency resolution failure
228-
deferUpdate = true;
229215
}
230216
} catch (MavenDownloadingException e) {
231217
return e.warn(tag);
232218
}
233219
}
234220

235-
if (t != tag && !deferUpdate) {
221+
if (t != tag) {
236222
maybeUpdateModel();
237223
}
238224
}
@@ -241,7 +227,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
241227
return t;
242228
}
243229

244-
private boolean checkIfNewDependencyPresent(@Nullable String groupId, @Nullable String artifactId, @Nullable String version) {
230+
private boolean checkIfNewDependencyPresents(@Nullable String groupId, @Nullable String artifactId, @Nullable String version) {
245231
if ((groupId == null) || (artifactId == null)) {
246232
return false;
247233
}
@@ -262,23 +248,6 @@ private boolean isDependencyManaged(Scope scope, String groupId, String artifact
262248
return false;
263249
}
264250

265-
private boolean canAffectManagedDependency(MavenResolutionResult result, Scope scope, String groupId, String artifactId) {
266-
// We're only going to be able to effect managed dependencies that are either direct or are brought in as direct via a local parent
267-
// `ChangeManagedDependencyGroupIdAndArtifactId` cannot manipulate BOM imported managed dependencies nor direct dependencies from remote parents
268-
Pom requestedPom = result.getPom().getRequested();
269-
for (ManagedDependency requestedManagedDependency : requestedPom.getDependencyManagement()) {
270-
if (groupId.equals(requestedManagedDependency.getGroupId()) && artifactId.equals(requestedManagedDependency.getArtifactId())) {
271-
if (requestedManagedDependency instanceof ManagedDependency.Defined) {
272-
return scope.isInClasspathOf(Scope.fromName(((ManagedDependency.Defined) requestedManagedDependency).getScope()));
273-
}
274-
}
275-
}
276-
if (result.parentPomIsProjectPom() && result.getParent() != null) {
277-
return canAffectManagedDependency(result.getParent(), scope, groupId, artifactId);
278-
}
279-
return false;
280-
}
281-
282251
@SuppressWarnings("ConstantConditions")
283252
private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId, @Nullable String currentVersion) throws MavenDownloadingException {
284253
if (versionComparator == null) {

rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.openrewrite.maven.tree.ResolvedPom;
2727
import org.openrewrite.semver.Semver;
2828
import org.openrewrite.semver.VersionComparator;
29-
import org.openrewrite.xml.AddToTagVisitor;
3029
import org.openrewrite.xml.RemoveContentVisitor;
3130
import org.openrewrite.xml.tree.Xml;
3231

@@ -129,7 +128,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
129128

130129
@Override
131130
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
132-
isNewDependencyPresent = checkIfNewDependencyPresent(newGroupId, newArtifactId, newVersion);
131+
isNewDependencyPresent = checkIfNewDependencyPresents(newGroupId, newArtifactId, newVersion);
133132
return super.visitDocument(document, ctx);
134133
}
135134

@@ -155,9 +154,6 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
155154
}
156155
String resolvedNewVersion = resolveSemverVersion(ctx, newGroupId, resolvedArtifactId, getResolutionResult().getPom().getValue(versionTag.get().getValue().orElse(null)));
157156
t = changeChildTagValue(t, "version", resolvedNewVersion, ctx);
158-
} else {
159-
Xml.Tag newChild = Xml.Tag.build("<version>" + newVersion + "</version>");
160-
t = (Xml.Tag) new AddToTagVisitor<ExecutionContext>(t, newChild, new MavenTagInsertionComparator(t.getChildren())).visitNonNull(t, ctx, getCursor().getParentOrThrow());
161157
}
162158
} catch (MavenDownloadingException e) {
163159
return e.warn(t);
@@ -175,7 +171,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
175171
return t;
176172
}
177173

178-
private boolean checkIfNewDependencyPresent(@Nullable String groupId, @Nullable String artifactId, @Nullable String version) {
174+
private boolean checkIfNewDependencyPresents(@Nullable String groupId, @Nullable String artifactId, @Nullable String version) {
179175
if ((groupId == null) || (artifactId == null)) {
180176
return false;
181177
}

0 commit comments

Comments
 (0)