Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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 @@ -341,7 +341,7 @@ private boolean dependsOnNewerVersion(GroupArtifactVersion searchGav, GroupArtif
try {
List<ResolvedDependency> resolved = mpd.download(toSearch, null, null, repositories)
.resolve(emptyList(), mpd, repositories, ctx)
.resolveDependencies(Scope.Runtime, mpd, ctx);
.resolveDependencies(Scope.Runtime, mpd, ResolutionStrategy.NEWEST_WINS, ctx);
for (ResolvedDependency r : resolved) {
if (Objects.equals(searchGav.getGroupId(), r.getGroupId()) && Objects.equals(searchGav.getArtifactId(), r.getArtifactId())) {
return searchGav.getVersion() == null || matchesComparator(r.getVersion(), searchGav.getVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ public static GradleProject replaceVersion(GradleProject gp, ExecutionContext ct
MavenPomDownloader mpd = new MavenPomDownloader(ctx);
Pom pom = mpd.download(gav, null, null, gp.getMavenRepositories());
ResolvedPom resolvedPom = pom.resolve(emptyList(), mpd, gp.getMavenRepositories(), ctx);
List<ResolvedDependency> transitiveDependencies = resolvedPom.resolveDependencies(Scope.Runtime, mpd, ctx);
List<ResolvedDependency> transitiveDependencies = resolvedPom.resolveDependencies(Scope.Runtime, mpd, ResolutionStrategy.NEWEST_WINS, ctx);
org.openrewrite.maven.tree.Dependency newRequested = org.openrewrite.maven.tree.Dependency.builder()
.gav(gav)
.build();
Expand Down Expand Up @@ -1170,7 +1170,7 @@ private static List<ResolvedDependency> updateDirectResolved(
}

List<ResolvedDependency> transitiveDependencies = filterTransitiveDependencies(
resolvedPom.resolveDependencies(Scope.Runtime, mpd, ctx),
resolvedPom.resolveDependencies(Scope.Runtime, mpd, ResolutionStrategy.NEWEST_WINS, ctx),
existingTransitiveGAs,
newTransitiveGAs
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static JavaSourceFile addDependency(
Pom pom = mpd.download(gav, null, null, gp.getMavenRepositories());
ResolvedPom resolvedPom = pom.resolve(emptyList(), mpd, gp.getMavenRepositories(), ctx);
resolvedGav = resolvedPom.getGav();
transitiveDependencies = resolvedPom.resolveDependencies(Scope.Runtime, mpd, ctx);
transitiveDependencies = resolvedPom.resolveDependencies(Scope.Runtime, mpd, ResolutionStrategy.NEWEST_WINS, ctx);
}
Map<String, GradleDependencyConfiguration> nameToConfiguration = gp.getNameToConfiguration();
Map<String, GradleDependencyConfiguration> newNameToConfiguration = new HashMap<>(nameToConfiguration.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import org.openrewrite.*;
import org.openrewrite.maven.internal.MavenPomDownloader;
import org.openrewrite.maven.internal.RawPom;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.maven.tree.Parent;
import org.openrewrite.maven.tree.Pom;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.maven.tree.*;
import org.openrewrite.tree.ParseError;
import org.openrewrite.xml.XmlParser;
import org.openrewrite.xml.tree.Xml;
Expand Down Expand Up @@ -118,7 +115,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
effectivelyActiveProfiles,
properties);
if (!skipDependencyResolution) {
model = model.resolveDependencies(downloader, ctx);
model = model.resolveDependencies(downloader, ResolutionStrategy.NEAREST_WINS, ctx);
}
parsed.add(docToPom.getKey().withMarkers(docToPom.getKey().getMarkers().compute(model, (old, n) -> n)));
} catch (MavenDownloadingExceptions e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private MavenResolutionResult updateResult(ExecutionContext ctx, MavenResolution
return module;
}
}))
.resolveDependencies(downloader, ctx);
.resolveDependencies(downloader, ResolutionStrategy.NEAREST_WINS, ctx);
if (exceptions.get() != null) {
throw exceptions.get();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
* Copyright 2020 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.maven.internal;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.jspecify.annotations.Nullable;
import org.openrewrite.maven.MavenDownloadingException;
import org.openrewrite.maven.internal.grammar.VersionRangeLexer;
import org.openrewrite.maven.internal.grammar.VersionRangeParser;
import org.openrewrite.maven.internal.grammar.VersionRangeParserBaseVisitor;
import org.openrewrite.maven.tree.Version;

import java.util.Iterator;

import static java.util.stream.Collectors.toList;

@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class NearestWins extends VersionRequirement {

@Nullable
private final NearestWins nearer;

private final VersionSpec versionSpec;

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (nearer != null) {
builder.append(nearer).append(", ");
}
builder.append(versionSpec);
return builder.toString();
}

public NearestWins addRequirement(String requested) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public NearestWins addRequirement(String requested) {
@Override
public NearestWins addRequirement(String requested) {

if (versionSpec instanceof DirectRequirement) {
return this;
}

VersionSpec newRequirement = buildVersionSpec(requested, false);

NearestWins next = this;
while (next != null) {
if (next.versionSpec.equals(newRequirement)) {
return this;
}
next = next.nearer;
}

return new NearestWins(this, newRequirement);
}

static VersionSpec buildVersionSpec(String requested, boolean direct) {
if ("LATEST".equals(requested)) {
return DynamicVersion.LATEST;
} else if ("RELEASE".equals(requested)) {
return DynamicVersion.RELEASE;
} else if (requested.contains("[") || requested.contains("(")) {
// for things like the profile activation block of where the range is unclosed but maven still handles it, e.g.
// https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.12.0-rc2/jackson-databind-2.12.0-rc2.pom
if (!(requested.contains("]") || requested.contains(")"))) {
requested += "]";
}

VersionRangeParser parser = new VersionRangeParser(new CommonTokenStream(new VersionRangeLexer(
CharStreams.fromString(requested))));

parser.removeErrorListeners();
parser.addErrorListener(new PrintingErrorListener());

return new VersionRangeParserBaseVisitor<VersionSpec>() {
@Override
public VersionSpec visitVersionRequirement(VersionRangeParser.VersionRequirementContext ctx) {
return new RangeSet(ctx.range().stream()
.map(range -> {
Version lower, upper;
if (range.bounds().boundedLower() != null) {
Iterator<TerminalNode> versionIter = range.bounds().boundedLower().Version().iterator();
lower = versionIter.hasNext() ? toVersion(versionIter.next()) : null;
upper = versionIter.hasNext() ? toVersion(versionIter.next()) : null;
} else if (range.bounds().unboundedLower() != null) {
TerminalNode upperVersionNode = range.bounds().unboundedLower().Version();
lower = null;
upper = upperVersionNode != null ? toVersion(upperVersionNode) : null;
} else {
lower = toVersion(range.bounds().exactly().Version());
upper = toVersion(range.bounds().exactly().Version());
}
return new Range(
range.CLOSED_RANGE_OPEN() != null, lower,
range.CLOSED_RANGE_CLOSE() != null, upper
);
})
.collect(toList())
);
}

private Version toVersion(TerminalNode version) {
return new Version(version.getText());
}
}.visit(parser.versionRequirement());
}
return direct ?
new DirectRequirement(requested) :
new SoftRequirement(requested);
}

@Value
@EqualsAndHashCode(callSuper = true)
private static class DirectRequirement extends SoftRequirement {
private DirectRequirement(String version) {
super(version);
}

@Override
public String toString() {
return "Version='" + version + "'";
}
}

protected @Nullable String cacheResolved(DownloadOperation<Iterable<String>> availableVersions) throws MavenDownloadingException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected @Nullable String cacheResolved(DownloadOperation<Iterable<String>> availableVersions) throws MavenDownloadingException {
@Override
protected @Nullable String cacheResolved(DownloadOperation<Iterable<String>> availableVersions) throws MavenDownloadingException {

String nearestSoftRequirement = null;
NearestWins next = this;
NearestWins nearestHardRequirement = null;

while (next != null) {
VersionSpec spec = next.versionSpec;
if (spec instanceof DirectRequirement) {
// dependencies defined in the project POM always win
return ((DirectRequirement) spec).getVersion();
} else if (spec instanceof SoftRequirement) {
nearestSoftRequirement = ((SoftRequirement) spec).version;
} else {
nearestHardRequirement = next;
}
next = next.nearer;
}

if (nearestHardRequirement == null) {
return nearestSoftRequirement;
}
VersionSpec hardRequirement = nearestHardRequirement.versionSpec;
Version latest = null;
for (String availableVersion : availableVersions.call()) {
Version version = new Version(availableVersion);

if ((hardRequirement instanceof DynamicVersion || hardRequirement instanceof RangeSet) && hardRequirement.matches(version)) {
if (latest == null || version.compareTo(latest) > 0) {
latest = version;
}
}
}

if (latest == null) {
// No version matches the hard requirement.
return null;
}

return latest.toString();
}

private static class PrintingErrorListener extends BaseErrorListener {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine, String msg, RecognitionException e) {
System.out.printf("Syntax error at line %d:%d %s%n", line, charPositionInLine, msg);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2020 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.maven.internal;

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.jspecify.annotations.Nullable;
import org.openrewrite.maven.MavenDownloadingException;
import org.openrewrite.maven.internal.grammar.VersionRangeLexer;
import org.openrewrite.maven.internal.grammar.VersionRangeParser;
import org.openrewrite.maven.internal.grammar.VersionRangeParserBaseVisitor;
import org.openrewrite.maven.tree.Version;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import java.util.stream.Collectors;
import static java.util.stream.Collectors.joining;


import static java.util.stream.Collectors.toList;

public class NewestWins extends VersionRequirement {

private final Set<VersionSpec> requestedVersions = new HashSet<>();

NewestWins(VersionSpec requestedVersions) {
this.requestedVersions.add(requestedVersions);
}

@Override
public String toString() {
return requestedVersions.stream().map(Object::toString).collect(Collectors.joining(", "));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return requestedVersions.stream().map(Object::toString).collect(Collectors.joining(", "));
return requestedVersions.stream().map(Object::toString).collect(joining(", "));
@Override

}

public NewestWins addRequirement(String requested) {
VersionSpec requestedSpec = buildVersionSpec(requested);
if (requestedVersions.add(requestedSpec)) {
super.selected = null;
}
return this;
}

static VersionSpec buildVersionSpec(String requested) {
if ("LATEST".equals(requested)) {
return DynamicVersion.LATEST;
} else if ("RELEASE".equals(requested)) {
return DynamicVersion.RELEASE;
} else if (requested.contains("[") || requested.contains("(")) {
// for things like the profile activation block of where the range is unclosed but maven still handles it, e.g.
// https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.12.0-rc2/jackson-databind-2.12.0-rc2.pom
if (!(requested.contains("]") || requested.contains(")"))) {
requested += "]";
}

VersionRangeParser parser = new VersionRangeParser(new CommonTokenStream(new VersionRangeLexer(
CharStreams.fromString(requested))));

parser.removeErrorListeners();
parser.addErrorListener(new PrintingErrorListener());

return new VersionRangeParserBaseVisitor<VersionSpec>() {
@Override
public VersionSpec visitVersionRequirement(VersionRangeParser.VersionRequirementContext ctx) {
return new RangeSet(ctx.range().stream()
.map(range -> {
Version lower, upper;
if (range.bounds().boundedLower() != null) {
Iterator<TerminalNode> versionIter = range.bounds().boundedLower().Version().iterator();
lower = versionIter.hasNext() ? toVersion(versionIter.next()) : null;
upper = versionIter.hasNext() ? toVersion(versionIter.next()) : null;
} else if (range.bounds().unboundedLower() != null) {
TerminalNode upperVersionNode = range.bounds().unboundedLower().Version();
lower = null;
upper = upperVersionNode != null ? toVersion(upperVersionNode) : null;
} else {
lower = toVersion(range.bounds().exactly().Version());
upper = toVersion(range.bounds().exactly().Version());
}
return new Range(
range.CLOSED_RANGE_OPEN() != null, lower,
range.CLOSED_RANGE_CLOSE() != null, upper
);
})
.collect(toList())
);
}

private Version toVersion(TerminalNode version) {
return new Version(version.getText());
}
}.visit(parser.versionRequirement());
}
return new SoftRequirement(requested);
}

protected @Nullable String cacheResolved(DownloadOperation<Iterable<String>> availableVersions) throws MavenDownloadingException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected @Nullable String cacheResolved(DownloadOperation<Iterable<String>> availableVersions) throws MavenDownloadingException {
@Override
protected @Nullable String cacheResolved(DownloadOperation<Iterable<String>> availableVersions) throws MavenDownloadingException {

Version latestVersion = null;
for (String availableVersion : availableVersions.call()) {
Version version = new Version(availableVersion);
if (requestedVersions.stream().anyMatch(spec -> spec.matches(version))) {
if (latestVersion == null || version.compareTo(latestVersion) > 0) {
latestVersion = version;
}
}
}
return latestVersion != null ? latestVersion.toString() : null;
}

private static class PrintingErrorListener extends BaseErrorListener {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine, String msg, RecognitionException e) {
System.out.printf("Syntax error at line %d:%d %s%n", line, charPositionInLine, msg);
}
}
}
Loading
Loading