Skip to content

Commit

Permalink
Resolves mojohaus#960: displayDependencyUpdates should display update…
Browse files Browse the repository at this point in the history
…s from lesser segments

Resolves mojohaus#299: allowAnyUpdates should be ignored with a warning message if any of: allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates is set to false
  • Loading branch information
jarmoniuk committed May 27, 2023
1 parent 0fe821e commit 586c39c
Show file tree
Hide file tree
Showing 36 changed files with 1,072 additions and 620 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,8 @@ public ArtifactVersions(Artifact artifact, List<ArtifactVersion> versions, Versi
this.versionComparator = versionComparator;
this.versions = new TreeSet<>(versionComparator);
this.versions.addAll(versions);
// DefaultArtifact objects are often built from raw model, without a version set
// (where the actual version is taken from parent or dependency/plugin management)
// this probably isn't the case in an actual Maven execution
if (artifact.getVersion() != null) {
setCurrentVersion(artifact.getVersion());
}
setCurrentVersion(artifact.getVersion());
setCurrentVersionRange(artifact.getVersionRange());
}

/**
Expand All @@ -94,6 +90,7 @@ public ArtifactVersions(ArtifactVersions other) {
versionComparator = other.versionComparator;
versions = other.versions;
setCurrentVersion(other.getCurrentVersion());
setCurrentVersionRange(other.getCurrentVersionRange());
}

@SuppressWarnings("checkstyle:InnerAssignment")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.apache.commons.lang3.tuple.Triple;

/**
* Utility providing a cached {@link ArtifactVersions#getNewestUpdate(Optional, boolean)} API
* Utility providing a cached {@link ArtifactVersions#getNewestUpdateWithinSegment(Optional, boolean)} API
*/
public class ArtifactVersionsCache {
private TriFunction<AbstractVersionDetails, Optional<Segment>, Boolean, ?> cachedFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -688,11 +689,10 @@ public Map<Property, PropertyVersions> getVersionPropertiesMap(VersionProperties
if (dependencies != null) {
for (Dependency dependency : dependencies) {
getLog().debug("Property ${" + property.getName() + "}: Adding association to " + dependency);
builder.addAssociation(this.createDependencyArtifact(dependency), false);
builder.withAssociation(this.createDependencyArtifact(dependency), false);
}
}
try {
final PropertyVersions versions = builder.newPropertyVersions();
if (property.isAutoLinkDependencies()
&& StringUtils.isEmpty(property.getVersion())
&& !StringUtils.isEmpty(builder.getVersionRange())) {
Expand All @@ -702,8 +702,17 @@ public Map<Property, PropertyVersions> getVersionPropertiesMap(VersionProperties
}
final String currentVersion =
request.getMavenProject().getProperties().getProperty(property.getName());
versions.setCurrentVersion(currentVersion);
property.setValue(currentVersion);
final PropertyVersions versions;
try {
if (currentVersion != null) {
builder.withCurrentVersion(DefaultArtifactVersionCache.of(currentVersion))
.withCurrentVersionRange(VersionRange.createFromVersionSpec(currentVersion));
}
} catch (InvalidVersionSpecificationException e) {
throw new RuntimeException(e);
}
versions = builder.build();
propertyVersions.put(property, versions);
} catch (VersionRetrievalException e) {
throw new MojoExecutionException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ private static void addPluginAssociations(
}
// might as well capture the current value
String evaluatedVersion = (String) expressionEvaluator.evaluate(plugin.getVersion());
property.addAssociation(
property.withAssociation(
helper.createPluginArtifact(groupId, artifactId, evaluatedVersion), true);
if (!propertyRef.equals(version)) {
addBounds(property, version, propertyRef);
Expand Down Expand Up @@ -1066,7 +1066,7 @@ private static void addReportPluginAssociations(
}
// might as well capture the current value
String versionEvaluated = (String) expressionEvaluator.evaluate(plugin.getVersion());
property.addAssociation(
property.withAssociation(
helper.createPluginArtifact(groupId, artifactId, versionEvaluated), true);
if (!propertyRef.equals(version)) {
addBounds(property, version, propertyRef);
Expand Down Expand Up @@ -1111,7 +1111,7 @@ private static void addDependencyAssocations(
}
// might as well capture the current value
String versionEvaluated = (String) expressionEvaluator.evaluate(dependency.getVersion());
property.addAssociation(
property.withAssociation(
helper.createDependencyArtifact(
groupId,
artifactId,
Expand All @@ -1138,15 +1138,15 @@ private static void addBounds(PropertyVersionsBuilder builder, String rawVersion
boolean includeLower = "[".equals(m.group(1));
String lowerLimit = m.group(2);
if (StringUtils.isNotEmpty(lowerLimit)) {
builder.addLowerBound(lowerLimit, includeLower);
builder.withLowerBound(lowerLimit, includeLower);
}
}
m = upperBound.matcher(rawVersionRange);
if (m.find()) {
boolean includeUpper = "[".equals(m.group(3));
String upperLimit = m.group(2);
if (StringUtils.isNotEmpty(upperLimit)) {
builder.addUpperBound(upperLimit, includeUpper);
builder.withUpperBound(upperLimit, includeUpper);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public ArtifactVersion getNewestVersion(
? null
: upperBoundSegment
.map(s -> (ArtifactVersion) new BoundArtifactVersion(
currentVersion, s.isMajorTo(SUBINCREMENTAL) ? Segment.of(s.value() + 1) : s))
currentVersion, s.isMajorTo(SUBINCREMENTAL) ? Segment.minorTo(s) : s))
.orElse(null);
if (helper.getLog().isDebugEnabled()) {
helper.getLog().debug("Property ${" + property.getName() + "}: upperBound is: " + upperBound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.codehaus.mojo.versions.ordering.VersionComparator;

/**
Expand All @@ -48,6 +49,10 @@ class PropertyVersionsBuilder {

private final Map<String, Boolean> lowerBounds = new LinkedHashMap<>();

private ArtifactVersion currentVersion;

private VersionRange currentVersionRange;

/**
* Constructs a new {@link org.codehaus.mojo.versions.api.PropertyVersions}.
*
Expand All @@ -62,12 +67,9 @@ class PropertyVersionsBuilder {
this.helper = helper;
}

public void addAssociation(Artifact artifact, boolean usePluginRepositories) {
public PropertyVersionsBuilder withAssociation(Artifact artifact, boolean usePluginRepositories) {
associations.add(new DefaultArtifactAssociation(artifact, usePluginRepositories));
}

public void removeAssociation(Artifact artifact, boolean usePluginRepositories) {
associations.remove(new DefaultArtifactAssociation(artifact, usePluginRepositories));
return this;
}

public void clearAssociations() {
Expand All @@ -82,8 +84,11 @@ public ArtifactAssociation[] getAssociations() {
return associations.toArray(new ArtifactAssociation[0]);
}

public PropertyVersions newPropertyVersions() throws VersionRetrievalException {
return new PropertyVersions(profileId, name, helper, associations);
public PropertyVersions build() throws VersionRetrievalException {
PropertyVersions instance = new PropertyVersions(profileId, name, helper, associations);
instance.setCurrentVersion(currentVersion);
instance.setCurrentVersionRange(currentVersionRange);
return instance;
}

public String getName() {
Expand Down Expand Up @@ -150,24 +155,26 @@ public String getVersionRange() {
return buf.toString();
}

public void addLowerBound(String lowerBound, boolean includeLower) {
public PropertyVersionsBuilder withLowerBound(String lowerBound, boolean includeLower) {
Boolean value = lowerBounds.get(lowerBound);
if (value == null) {
value = includeLower;
} else {
value = includeLower && value;
}
lowerBounds.put(lowerBound, value);
return this;
}

public void addUpperBound(String upperBound, boolean includeUpper) {
public PropertyVersionsBuilder withUpperBound(String upperBound, boolean includeUpper) {
Boolean value = upperBounds.get(upperBound);
if (value == null) {
value = includeUpper;
} else {
value = includeUpper && value;
}
upperBounds.put(upperBound, value);
return this;
}

private VersionComparator[] lookupComparators() {
Expand Down Expand Up @@ -202,4 +209,14 @@ private int innerCompare(ArtifactVersion v1, ArtifactVersion v2) {
return result;
}
}

public PropertyVersionsBuilder withCurrentVersion(ArtifactVersion currentVersion) {
this.currentVersion = currentVersion;
return this;
}

public PropertyVersionsBuilder withCurrentVersionRange(VersionRange currentVersionRange) {
this.currentVersionRange = currentVersionRange;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* under the License.
*/

import java.util.Optional;

/**
* Indicates the segment along with its 0-based index
*
Expand Down Expand Up @@ -46,6 +48,26 @@ public static Segment of(int index) {
return values()[index];
}

/**
* Creates a segment that has a greater scope than the given segment or {@code null}
* if the segment is already {@link #MAJOR}
* @param other segment that the new segment is to be based on
* @return that has a greater scope than the given segment or {@code null}
* if the segment is already {@link #MAJOR}
*/
public static Segment majorTo(Segment other) {
return Optional.ofNullable(other).map(s -> of(s.value() - 1)).orElse(null);
}

/**
* Creates a segment that has a lesser scope than the given segment
* @param other segment that the new segment is to be based on
* @return that has a lesser scope than the given segment
*/
public static Segment minorTo(Segment other) {
return Optional.ofNullable(other).map(s -> of(s.value() + 1)).orElse(MAJOR);
}

/**
* Returns true if the given segment is more major than the other
* @param other other segment to compare
Expand Down
Loading

0 comments on commit 586c39c

Please sign in to comment.