Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,15 @@ public Validated<Object> validate() {
));
}

private boolean isGlobPattern() {
return oldGroupId.contains("*") || oldGroupId.contains("?") ||
oldArtifactId.contains("*") || oldArtifactId.contains("?");
}

public static class Accumulator {
Map<String, Object> versionVariableUpdates = new HashMap<>();
Map<String, Set<GroupArtifact>> versionVariableUsages = new HashMap<>();
Set<GroupArtifact> failedResolutions = new HashSet<>();
}

@Override
Expand Down Expand Up @@ -232,7 +238,14 @@ private void resolveAndRecordVersion(String varName, J.MethodInvocation m, Gradl
acc.versionVariableUpdates.put(varName, resolvedVersion);
}
} catch (MavenDownloadingException e) {
acc.versionVariableUpdates.put(varName, e);
if (isGlobPattern()) {
acc.failedResolutions.add(new GroupArtifact(dep.getGroupId(), dep.getArtifactId()));
// Don't overwrite a successful resolution with a failure from a different artifact
// sharing the same version variable (e.g. hibernate-core resolved but hibernate-validator didn't)
acc.versionVariableUpdates.putIfAbsent(varName, e);
} else {
acc.versionVariableUpdates.put(varName, e);
}
}
}
};
Expand Down Expand Up @@ -347,6 +360,10 @@ private boolean canUpdateVariable(String varName) {
}

private J.MethodInvocation updateDependency(J.MethodInvocation m, GradleDependency dep, ExecutionContext ctx) {
if (acc.failedResolutions.contains(new GroupArtifact(dep.getGroupId(), dep.getArtifactId()))) {
return m;
}

GradleDependency updated = dep;

if (!StringUtils.isBlank(newGroupId)) {
Expand Down Expand Up @@ -514,6 +531,9 @@ private boolean canUpdateVariable(String varName, DependencyMatcher depMatcher,
if (!depMatcher.matches(ga.getGroupId(), ga.getArtifactId())) {
return false;
}
if (acc.failedResolutions.contains(ga)) {
return false;
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1104,4 +1104,43 @@ void isNotAcceptableForPlainTextWithGradleProjectMarker() {
.build())));
assertThat(visitor.isAcceptable(sourceFile, new InMemoryExecutionContext())).isFalse();
}

@Test
void doesNotChangeGroupIdWhenNewCoordinatesDontResolve() {
rewriteRun(
spec -> spec.recipe(new ChangeDependency("org.hibernate", "hibernate-*", "org.hibernate.orm", null, "6.0.x", null, null, true)),
buildGradle(
"""
plugins {
id "java-library"
}

repositories {
mavenCentral()
}

def hibernateVersion = '5.6.15.Final'
dependencies {
implementation "org.hibernate:hibernate-core:${hibernateVersion}"
implementation "org.hibernate:hibernate-validator:${hibernateVersion}"
}
""",
"""
plugins {
id "java-library"
}

repositories {
mavenCentral()
}

def hibernateVersion = '5.6.15.Final'
dependencies {
implementation "org.hibernate.orm:hibernate-core:6.0.2.Final"
implementation "org.hibernate:hibernate-validator:${hibernateVersion}"
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
deferUpdate = true;
}
} catch (MavenDownloadingException e) {
if (oldGroupId.contains("*") || oldGroupId.contains("?") ||
oldArtifactId.contains("*") || oldArtifactId.contains("?")) {
// Glob matched an artifact that doesn't exist under the new coordinates
return tag;
}
return e.warn(tag);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
}
}
} catch (MavenDownloadingException e) {
if (oldGroupId.contains("*") || oldGroupId.contains("?") ||
oldArtifactId.contains("*") || oldArtifactId.contains("?")) {
// Glob matched an artifact that doesn't exist under the new coordinates
return tag;
}
return e.warn(t);
}
}
Expand Down
Loading