From c2cbbfda678a0b65296f46e980d31e605c8a31e4 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Fri, 19 May 2023 20:14:50 +0200 Subject: [PATCH] Resolves #921: Refactoring segments; added the special case for restrictionForIgnoreScope --- .../versions/api/AbstractVersionDetails.java | 60 +++++++++---------- .../mojo/versions/api/PropertyVersions.java | 38 ++++-------- .../codehaus/mojo/versions/api/Segment.java | 8 +-- .../mojo/versions/utils/SegmentUtilsTest.java | 25 +++++++- .../DisplayDependencyUpdatesMojo.java | 18 ++---- .../versions/DisplayPropertyUpdatesMojo.java | 28 ++++++++- .../mojo/versions/ResolveRangesMojo.java | 28 ++++++++- .../mojo/versions/UpdateParentMojo.java | 8 ++- .../versions/UpdatePropertiesMojoBase.java | 1 + .../mojo/versions/UseLatestReleasesMojo.java | 26 +++++++- .../mojo/versions/UseLatestSnapshotsMojo.java | 26 +++++++- .../mojo/versions/UseLatestVersionsMojo.java | 26 +++++++- .../mojo/versions/UseNextSnapshotsMojo.java | 26 +++++++- .../versions/reporting/OverviewStats.java | 20 +++---- .../reporting/PluginOverviewStats.java | 10 ++-- 15 files changed, 233 insertions(+), 115 deletions(-) diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java index a9c1b85005..9804fdb76a 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -19,26 +19,22 @@ * under the License. */ +import java.util.*; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; + import org.apache.maven.artifact.ArtifactUtils; -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.artifact.versioning.*; import org.codehaus.mojo.versions.ordering.BoundArtifactVersion; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; -import java.util.*; -import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.Stream; - import static java.util.Collections.reverseOrder; import static java.util.Optional.empty; import static java.util.Optional.of; -import static org.codehaus.mojo.versions.api.Segment.MAJOR; -import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.*; /** * Base class for {@link org.codehaus.mojo.versions.api.VersionDetails}. @@ -69,17 +65,15 @@ public Restriction restrictionForUnchangedSegment(Optional unchangedSeg // one range spec can have multiple restrictions, and multiple 'lower bound', we want the highest one. // [1.0,2.0),[3.0,4.0) -> 3.0 ArtifactVersion highestLowerBound = currentVersion; - if (currentVersion != null) { - try { - highestLowerBound = - VersionRange.createFromVersionSpec(currentVersion.toString()).getRestrictions().stream() - .map(Restriction::getLowerBound) - .filter(Objects::nonNull) - .max(getVersionComparator()) - .orElse(currentVersion); - } catch (InvalidVersionSpecificationException ignored) { - ignored.printStackTrace(System.err); - } + assert highestLowerBound != null; + try { + highestLowerBound = VersionRange.createFromVersionSpec(currentVersion.toString()).getRestrictions().stream() + .map(Restriction::getLowerBound) + .filter(Objects::nonNull) + .max(getVersionComparator()) + .orElse(currentVersion); + } catch (InvalidVersionSpecificationException e) { + e.printStackTrace(System.err); } final ArtifactVersion currentVersion = highestLowerBound; @@ -92,18 +86,23 @@ public Restriction restrictionForUnchangedSegment(Optional unchangedSeg nextVersion, false, unchangedSegment - .filter(MAJOR::isGreaterThan) .map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s)) .orElse(null), false); } @Override - public Restriction restrictionForIgnoreScope(Optional ignored) { - ArtifactVersion nextVersion = ignored - .map(Segment::ofGreaterThan) - .map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s)) - .orElse(currentVersion); + public Restriction restrictionForIgnoreScope(Optional ignoredSegment) { + ArtifactVersion nextVersion; + if (ignoredSegment.map(s -> s == MAJOR).orElse(false)) { + // special case: we will ignore all possible version updates + nextVersion = DefaultArtifactVersionCache.of(String.valueOf(Integer.MAX_VALUE)); + } else { + nextVersion = ignoredSegment + .map(Segment::ofGreaterThan) + .map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s)) + .orElse(currentVersion); + } return new Restriction(nextVersion, false, null, false); } @@ -217,8 +216,7 @@ public final ArtifactVersion[] getNewerVersions( .orElse(null) : currentVersion; ArtifactVersion upperBound = unchangedSegment - .map(s -> (ArtifactVersion) new BoundArtifactVersion( - currentVersion, s.isGreaterThan(SUBINCREMENTAL) ? Segment.of(s.value() + 1) : s)) + .map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s)) .orElse(null); Restriction restriction = new Restriction(lowerBound, allowDowngrade, upperBound, allowDowngrade); @@ -317,7 +315,7 @@ public ArtifactVersion[] getAllUpdates(VersionRange versionRange, boolean includ * implying that there is also no string designation of the lower bound version. * * @param version {@link ArtifactVersion} object specifying the version for which the lower bound is being computed - * @param unchangedSegment first segment not to be changed; empty() means anything can change + * @param unchangedSegment first segment not to be changed; empty() means any segment may change * @return {@link Optional} string containing the lowest artifact version with the given segment held * @throws InvalidSegmentException if the requested segment is outside of the bounds (less than 1 or greater than * the segment count) diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java index 0db17b9cd7..f0447c4872 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java @@ -19,32 +19,18 @@ * under the License. */ -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; - import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; -import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; -import org.apache.maven.artifact.versioning.OverConstrainedVersionException; -import org.apache.maven.artifact.versioning.Restriction; -import org.apache.maven.artifact.versioning.VersionRange; +import org.apache.maven.artifact.versioning.*; import org.apache.maven.project.MavenProject; import org.codehaus.mojo.versions.ordering.BoundArtifactVersion; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; +import java.util.*; + import static java.util.Optional.empty; -import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; /** * Manages a property that is associated with one or more artifacts. @@ -282,7 +268,7 @@ public ArtifactVersion getNewestVersion( * @param reactorProjects collection of reactor projects * @param helper VersionHelper object * @param allowDowngrade whether downgrades should be allowed - * @param upperBoundSegment the upper bound segment; empty() means no upper bound + * @param unchangedSegment segment not to be changed; empty() means any segment may change * @return newest artifact version fulfilling the criteria or null if no newer version could be found * @throws InvalidSegmentException thrown if the {@code unchangedSegment} is not valid (e.g. greater than the number * of segments in the version string) @@ -295,7 +281,7 @@ public ArtifactVersion getNewestVersion( Collection reactorProjects, VersionsHelper helper, boolean allowDowngrade, - Optional upperBoundSegment) + Optional unchangedSegment) throws InvalidSegmentException, InvalidVersionSpecificationException { final boolean includeSnapshots = !property.isBanSnapshots() && allowSnapshots; helper.getLog().debug("getNewestVersion(): includeSnapshots='" + includeSnapshots + "'"); @@ -308,7 +294,7 @@ public ArtifactVersion getNewestVersion( ArtifactVersion currentVersion = DefaultArtifactVersionCache.of(versionString); ArtifactVersion lowerBound = allowDowngrade - ? getLowerBound(currentVersion, upperBoundSegment) + ? getLowerBound(currentVersion, unchangedSegment) .map(DefaultArtifactVersionCache::of) .orElse(null) : currentVersion; @@ -316,12 +302,9 @@ public ArtifactVersion getNewestVersion( helper.getLog().debug("lowerBoundArtifactVersion: " + lowerBound); } - ArtifactVersion upperBound = !upperBoundSegment.isPresent() - ? null - : upperBoundSegment - .map(s -> (ArtifactVersion) new BoundArtifactVersion( - currentVersion, s.isGreaterThan(SUBINCREMENTAL) ? Segment.of(s.value() + 1) : s)) - .orElse(null); + ArtifactVersion upperBound = unchangedSegment + .map(s -> new BoundArtifactVersion(currentVersion, s)) + .orElse(null); if (helper.getLog().isDebugEnabled()) { helper.getLog().debug("Property ${" + property.getName() + "}: upperBound is: " + upperBound); } @@ -350,7 +333,8 @@ public ArtifactVersion getNewestVersion( } } } - if (fromReactor != null && (result != null || !currentVersion.equals(fromReactor.toString()))) { + if (fromReactor != null && (result != null || !String.valueOf(currentVersion) + .equals(fromReactor.toString()))) { if (property.isPreferReactor()) { helper.getLog() .debug("Property ${" + property.getName() diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/Segment.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/Segment.java index 345a34b0cf..e34b5c635e 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/Segment.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/Segment.java @@ -59,9 +59,7 @@ public static Segment of(int index) { * if the segment is already {@link #MAJOR} */ public static Segment ofGreaterThan(Segment other) { - return Optional.ofNullable(other) - .map(s -> of(s.value() - 1)) - .orElse(null); + return Optional.ofNullable(other).map(s -> of(s.value() - 1)).orElse(null); } /** @@ -70,9 +68,7 @@ public static Segment ofGreaterThan(Segment other) { * @return that has a lesser scope than the given segment */ public static Segment ofLesserThan(Segment other) { - return Optional.ofNullable(other) - .map(s -> of(s.value() + 1)) - .orElse(MAJOR); + return Optional.ofNullable(other).map(s -> of(s.value() + 1)).orElse(MAJOR); } /** diff --git a/versions-common/src/test/java/org/codehaus/mojo/versions/utils/SegmentUtilsTest.java b/versions-common/src/test/java/org/codehaus/mojo/versions/utils/SegmentUtilsTest.java index 7d22804b35..b5974b0bdf 100644 --- a/versions-common/src/test/java/org/codehaus/mojo/versions/utils/SegmentUtilsTest.java +++ b/versions-common/src/test/java/org/codehaus/mojo/versions/utils/SegmentUtilsTest.java @@ -18,6 +18,10 @@ * under the License. */ +import java.util.Optional; + +import org.apache.maven.plugin.logging.Log; +import org.codehaus.mojo.versions.api.Segment; import org.junit.Test; import static java.util.Optional.empty; @@ -53,6 +57,25 @@ public void testMajor() { @Test public void testEmpty() { - assertThat(determineUnchangedSegment(true, true, true, null), is(empty())); + Optional result; + boolean allowMinorUpdates = true; + boolean allowIncrementalUpdates = true; + Log log = null; + if (log != null && !allowIncrementalUpdates) { + log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false."); + } + + Optional unchangedSegment = allowIncrementalUpdates + ? empty() + : allowMinorUpdates && allowIncrementalUpdates + ? of(MAJOR) + : allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL); + if (log != null && log.isDebugEnabled()) { + log.debug(unchangedSegment + .map(s -> Segment.of(s.value() + 1).toString()) + .orElse("ALL") + " version changes allowed"); + } + result = unchangedSegment; + assertThat(result, is(empty())); } } diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java index ace8701a5a..949e27aa93 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java @@ -43,10 +43,7 @@ import org.codehaus.plexus.util.StringUtils; import static java.util.Collections.emptySet; -import static java.util.Optional.empty; -import static java.util.Optional.of; import static org.apache.commons.lang3.StringUtils.countMatches; -import static org.codehaus.mojo.versions.api.Segment.MAJOR; import static org.codehaus.mojo.versions.filtering.DependencyFilter.filterDependencies; import static org.codehaus.mojo.versions.utils.MavenProjectUtils.*; @@ -479,15 +476,6 @@ static void validateGAVList(List gavList, int numSections, String argume } } - private Optional calculateUpdateScope() { - return allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates - ? empty() - : of(SegmentUtils.determineUnchangedSegment( - allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()) - .map(s -> Segment.of(s.value() + 1)) - .orElse(MAJOR)); - } - private void logUpdates(Map updates, String section) { List withUpdates = new ArrayList<>(); List usingCurrent = new ArrayList<>(); @@ -495,16 +483,18 @@ private void logUpdates(Map updates, String sectio String left = " " + ArtifactUtils.versionlessKey(versions.getArtifact()) + " "; final String current; ArtifactVersion latest; + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( + allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); if (versions.isCurrentVersionDefined()) { current = versions.getCurrentVersion().toString(); - latest = versions.getNewestUpdate(calculateUpdateScope(), allowSnapshots); + latest = versions.getNewestUpdate(unchangedSegment, allowSnapshots); } else { ArtifactVersion newestVersion = versions.getNewestVersion(versions.getArtifact().getVersionRange(), allowSnapshots); current = versions.getArtifact().getVersionRange().toString(); latest = newestVersion == null ? null - : versions.getNewestUpdate(newestVersion, calculateUpdateScope(), allowSnapshots); + : versions.getNewestUpdate(newestVersion, unchangedSegment, allowSnapshots); if (latest != null && ArtifactVersions.isVersionInRange( latest, versions.getArtifact().getVersionRange())) { diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java index ba5a0061e8..4c01f591d3 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java @@ -31,6 +31,7 @@ import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.repository.RepositorySystem; @@ -42,7 +43,10 @@ import org.codehaus.mojo.versions.api.recording.ChangeRecorder; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; -import org.codehaus.mojo.versions.utils.SegmentUtils; + +import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.*; /** * Displays properties that are linked to artifact versions and have updates available. @@ -168,8 +172,26 @@ public void execute() throws MojoExecutionException, MojoFailureException { continue; } - Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( - allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); + Log log = getLog(); + if (log != null && !allowIncrementalUpdates) { + log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false."); + } + + if (log != null && !allowMinorUpdates) { + log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false."); + } + + Optional unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates + ? empty() + : allowMinorUpdates && allowIncrementalUpdates + ? of(MAJOR) + : allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL); + if (log != null && log.isDebugEnabled()) { + log.debug(unchangedSegment1 + .map(s -> Segment.of(s.value() + 1).toString()) + .orElse("ALL") + " version changes allowed"); + } + Optional unchangedSegment = unchangedSegment1; try { ArtifactVersion winner = version.getNewestVersion( currentVersion, diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java index f9ccfdad0e..e4bae9492a 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java @@ -35,6 +35,7 @@ import org.apache.maven.model.Dependency; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.repository.RepositorySystem; @@ -48,7 +49,10 @@ import org.codehaus.mojo.versions.api.recording.ChangeRecorder; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; -import org.codehaus.mojo.versions.utils.SegmentUtils; + +import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.*; /** * Attempts to resolve dependency version ranges to the specific version being used in the build. For example a version @@ -288,8 +292,26 @@ private void resolvePropertyRanges(ModifiedPomXMLEventReader pom) property.setVersion(currentVersion); - Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( - allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); + Log log = getLog(); + if (log != null && !allowIncrementalUpdates) { + log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false."); + } + + if (log != null && !allowMinorUpdates) { + log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false."); + } + + Optional unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates + ? empty() + : allowMinorUpdates && allowIncrementalUpdates + ? of(MAJOR) + : allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL); + if (log != null && log.isDebugEnabled()) { + log.debug(unchangedSegment1 + .map(s -> Segment.of(s.value() + 1).toString()) + .orElse("ALL") + " version changes allowed"); + } + Optional unchangedSegment = unchangedSegment1; // TODO: Check if we could add allowDowngrade ? try { updatePropertyToNewestVersion(pom, property, version, currentVersion, false, unchangedSegment); diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java index 376c95b2d4..ab930c1a9e 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java @@ -30,6 +30,7 @@ import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.repository.RepositorySystem; @@ -47,7 +48,10 @@ import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.utils.SegmentUtils; +import static java.util.Optional.empty; +import static java.util.Optional.of; import static org.apache.maven.shared.utils.StringUtils.isBlank; +import static org.codehaus.mojo.versions.api.Segment.*; /** * Sets the parent version to the latest parent version. @@ -212,8 +216,8 @@ protected ArtifactVersion resolveTargetVersion(String initialVersion) } final ArtifactVersions versions = getHelper().lookupArtifactVersions(artifact, false); - Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( - allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment(allowMajorUpdates, + allowMinorUpdates, allowIncrementalUpdates, getLog()); // currentVersion (set to parentVersion here) is not included in the version range for searching upgrades // unless we set allowDowngrade to true diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojoBase.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojoBase.java index 3274bb2f4f..995c17ff6e 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojoBase.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojoBase.java @@ -37,6 +37,7 @@ import org.codehaus.mojo.versions.recording.DefaultPropertyChangeRecord; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import static org.codehaus.mojo.versions.api.Segment.*; import static org.codehaus.mojo.versions.utils.SegmentUtils.determineUnchangedSegment; /** diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java index ea316a38f6..65df26be03 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java @@ -33,6 +33,7 @@ import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.repository.RepositorySystem; @@ -44,10 +45,11 @@ import org.codehaus.mojo.versions.api.recording.DependencyChangeRecord; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; -import org.codehaus.mojo.versions.utils.SegmentUtils; import static java.util.Collections.singletonList; import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.*; /** * Replaces any release versions (i.e. versions that are not snapshots and do not @@ -149,8 +151,26 @@ private void useLatestReleases( Collection dependencies, DependencyChangeRecord.ChangeKind changeKind) throws XMLStreamException, MojoExecutionException, VersionRetrievalException { - Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( - allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); + Log log = getLog(); + if (log != null && !allowIncrementalUpdates) { + log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false."); + } + + if (log != null && !allowMinorUpdates) { + log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false."); + } + + Optional unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates + ? empty() + : allowMinorUpdates && allowIncrementalUpdates + ? of(MAJOR) + : allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL); + if (log != null && log.isDebugEnabled()) { + log.debug(unchangedSegment1 + .map(s -> Segment.of(s.value() + 1).toString()) + .orElse("ALL") + " version changes allowed"); + } + Optional unchangedSegment = unchangedSegment1; useLatestVersions( pom, diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java index 7b00dac7f4..8fb86f56d0 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java @@ -29,6 +29,7 @@ import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.repository.RepositorySystem; @@ -40,10 +41,11 @@ import org.codehaus.mojo.versions.api.recording.DependencyChangeRecord; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; -import org.codehaus.mojo.versions.utils.SegmentUtils; import static java.util.Collections.singletonList; import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.*; /** * Replaces any release versions with the latest snapshot version (if it has been deployed). @@ -128,8 +130,26 @@ private void useLatestSnapshots( Collection dependencies, DependencyChangeRecord.ChangeKind changeKind) throws XMLStreamException, MojoExecutionException, VersionRetrievalException { - Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( - allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); + Log log = getLog(); + if (log != null && !allowIncrementalUpdates) { + log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false."); + } + + if (log != null && !allowMinorUpdates) { + log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false."); + } + + Optional unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates + ? empty() + : allowMinorUpdates && allowIncrementalUpdates + ? of(MAJOR) + : allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL); + if (log != null && log.isDebugEnabled()) { + log.debug(unchangedSegment1 + .map(s -> Segment.of(s.value() + 1).toString()) + .orElse("ALL") + " version changes allowed"); + } + Optional unchangedSegment = unchangedSegment1; useLatestVersions( pom, diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java index 1dc4615cc6..e4109d0461 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java @@ -27,6 +27,7 @@ import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.repository.RepositorySystem; @@ -38,10 +39,11 @@ import org.codehaus.mojo.versions.api.recording.DependencyChangeRecord; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; -import org.codehaus.mojo.versions.utils.SegmentUtils; import static java.util.Collections.singletonList; import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.*; /** * Replaces any version with the latest version found in the artifactory. @@ -143,8 +145,26 @@ private void useLatestVersions( Collection dependencies, DependencyChangeRecord.ChangeKind changeKind) throws XMLStreamException, MojoExecutionException, VersionRetrievalException { - Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( - allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); + Log log = getLog(); + if (log != null && !allowIncrementalUpdates) { + log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false."); + } + + if (log != null && !allowMinorUpdates) { + log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false."); + } + + Optional unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates + ? empty() + : allowMinorUpdates && allowIncrementalUpdates + ? of(MAJOR) + : allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL); + if (log != null && log.isDebugEnabled()) { + log.debug(unchangedSegment1 + .map(s -> Segment.of(s.value() + 1).toString()) + .orElse("ALL") + " version changes allowed"); + } + Optional unchangedSegment = unchangedSegment1; useLatestVersions( pom, diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java index 802528c548..6e68e38ce4 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java @@ -28,6 +28,7 @@ import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.repository.RepositorySystem; @@ -39,10 +40,11 @@ import org.codehaus.mojo.versions.api.recording.DependencyChangeRecord; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; -import org.codehaus.mojo.versions.utils.SegmentUtils; import static java.util.Collections.singletonList; import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.*; /** * Replaces any release versions with the next snapshot version (if it has been deployed). @@ -127,8 +129,26 @@ private void useNextSnapshots( Collection dependencies, DependencyChangeRecord.ChangeKind changeKind) throws XMLStreamException, MojoExecutionException, VersionRetrievalException { - Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( - allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); + Log log = getLog(); + if (log != null && !allowIncrementalUpdates) { + log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false."); + } + + if (log != null && !allowMinorUpdates) { + log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false."); + } + + Optional unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates + ? empty() + : allowMinorUpdates && allowIncrementalUpdates + ? of(MAJOR) + : allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL); + if (log != null && log.isDebugEnabled()) { + log.debug(unchangedSegment1 + .map(s -> Segment.of(s.value() + 1).toString()) + .orElse("ALL") + " version changes allowed"); + } + Optional unchangedSegment = unchangedSegment1; useLatestVersions( pom, diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java index 1553ccdfaa..c7ea5e8c49 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java @@ -28,11 +28,9 @@ import org.codehaus.mojo.versions.api.ArtifactVersionsCache; import org.codehaus.mojo.versions.api.Segment; +import static java.util.Optional.empty; import static java.util.Optional.of; -import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; -import static org.codehaus.mojo.versions.api.Segment.MAJOR; -import static org.codehaus.mojo.versions.api.Segment.MINOR; -import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.*; /** * Represents summary stats @@ -67,13 +65,13 @@ public static T from Collection updates, ArtifactVersionsCache cache, boolean allowSnapshots) { OverviewStats stats = new OverviewStats(); updates.forEach(details -> { - if (getNewestUpdate(cache, details, of(SUBINCREMENTAL), allowSnapshots) != null) { + if (getNewestUpdate(cache, details, of(INCREMENTAL), allowSnapshots) != null) { stats.incrementAny(); - } else if (getNewestUpdate(cache, details, of(INCREMENTAL), allowSnapshots) != null) { - stats.incrementIncremental(); } else if (getNewestUpdate(cache, details, of(MINOR), allowSnapshots) != null) { - stats.incrementMinor(); + stats.incrementIncremental(); } else if (getNewestUpdate(cache, details, of(MAJOR), allowSnapshots) != null) { + stats.incrementMinor(); + } else if (getNewestUpdate(cache, details, empty(), allowSnapshots) != null) { stats.incrementMajor(); } else { stats.incrementUpToDate(); @@ -83,10 +81,10 @@ public static T from } protected static ArtifactVersion getNewestUpdate( - ArtifactVersionsCache cache, V details, Optional segment, boolean allowSnapshots) { + ArtifactVersionsCache cache, V details, Optional unchangedSegment, boolean allowSnapshots) { return cache != null - ? cache.get(details, segment, allowSnapshots) - : details.getNewestUpdate(segment, allowSnapshots); + ? cache.get(details, unchangedSegment, allowSnapshots) + : details.getNewestUpdate(unchangedSegment, allowSnapshots); } public int getMajor() { diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java index 5fa0e56326..927a600586 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java @@ -27,11 +27,11 @@ import org.codehaus.mojo.versions.api.PluginUpdatesDetails; import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel; +import static java.util.Optional.empty; import static java.util.Optional.of; import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; import static org.codehaus.mojo.versions.api.Segment.MAJOR; import static org.codehaus.mojo.versions.api.Segment.MINOR; -import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; public class PluginOverviewStats extends OverviewStats { /** @@ -63,13 +63,13 @@ public static T from Collection updates, ArtifactVersionsCache cache, boolean allowSnapshots) { PluginOverviewStats stats = new PluginOverviewStats(); updates.forEach(details -> { - if (getNewestUpdate(cache, details, of(SUBINCREMENTAL), allowSnapshots) != null) { + if (getNewestUpdate(cache, details, of(INCREMENTAL), allowSnapshots) != null) { stats.incrementAny(); - } else if (getNewestUpdate(cache, details, of(INCREMENTAL), allowSnapshots) != null) { - stats.incrementIncremental(); } else if (getNewestUpdate(cache, details, of(MINOR), allowSnapshots) != null) { - stats.incrementMinor(); + stats.incrementIncremental(); } else if (getNewestUpdate(cache, details, of(MAJOR), allowSnapshots) != null) { + stats.incrementMinor(); + } else if (getNewestUpdate(cache, details, empty(), allowSnapshots) != null) { stats.incrementMajor(); } else { stats.incrementUpToDate();