From 0623e868ad9d244d72614b388f5851f884c59aaa Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Sat, 8 Oct 2022 10:30:05 +0200 Subject: [PATCH] #283: allowMinorUpdates false should imply allowMajorUpdates false --- .../versions/AbstractVersionsUpdaterMojo.java | 42 ------ .../DisplayDependencyUpdatesMojo.java | 50 +++---- .../versions/DisplayPropertyUpdatesMojo.java | 13 +- .../mojo/versions/ResolveRangesMojo.java | 14 +- .../mojo/versions/UpdateParentMojo.java | 14 +- .../mojo/versions/UpdatePropertiesMojo.java | 17 ++- .../mojo/versions/UpdatePropertyMojo.java | 13 +- .../mojo/versions/UseLatestReleasesMojo.java | 14 +- .../mojo/versions/UseLatestSnapshotsMojo.java | 8 +- .../mojo/versions/UseLatestVersionsMojo.java | 18 ++- .../mojo/versions/UseNextSnapshotsMojo.java | 8 +- .../ordering/MajorMinorIncrementalFilter.java | 122 ------------------ .../mojo/versions/utils/SegmentUtils.java | 95 ++++++++++++++ .../mojo/versions/UpdateParentMojoTest.java | 2 +- .../versions/UseLatestVersionsMojoTest.java | 22 ++-- .../MajorMinorIncrementalFilterTest.java | 102 --------------- 16 files changed, 209 insertions(+), 345 deletions(-) delete mode 100644 src/main/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilter.java create mode 100644 src/main/java/org/codehaus/mojo/versions/utils/SegmentUtils.java delete mode 100644 src/test/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilterTest.java diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java index d374a9cc1d..ca59f3755f 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java @@ -31,7 +31,6 @@ import java.util.Optional; import java.util.Set; -import org.apache.commons.text.CaseUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; @@ -70,12 +69,6 @@ import org.codehaus.plexus.util.WriterFactory; import org.codehaus.stax2.XMLInputFactory2; -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; - /** * Abstract base class for Versions Mojos. * @@ -539,41 +532,6 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A return true; } - /** - *

Based on the passed flags, determines which segment (0-based), which is not to be changed.

- *

The method will return, depending on the first parameter on the list to be true: - *

- * - * This can be used when determining an upper - * bound for the "latest" version. - * - * @param allowMajorUpdates Allow major updates - * @param allowMinorUpdates Allow minor updates - * @param allowIncrementalUpdates Allow incremental updates - * @return Returns the segment (0-based) that is unchangeable. If any segment can change, returns -1. - */ - protected Optional determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates, - boolean allowIncrementalUpdates ) - { - Optional unchangedSegment = allowMajorUpdates ? empty() - : allowMinorUpdates ? of( MAJOR ) - : allowIncrementalUpdates ? of( MINOR ) - : of( INCREMENTAL ); - if ( getLog().isInfoEnabled() ) - { - getLog().info( - unchangedSegment.map( s -> - CaseUtils.toCamelCase( Segment.of( s.value() + 1 ).toString(), true ) ) - .orElse( "All" ) + " version changes allowed" ); - } - return unchangedSegment; - } - protected ArtifactVersion updatePropertyToNewestVersion( ModifiedPomXMLEventReader pom, Property property, PropertyVersions version, String currentVersion, boolean allowDowngrade, diff --git a/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java b/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java index a34235cfa5..55aba317e8 100644 --- a/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java @@ -54,14 +54,13 @@ import org.codehaus.mojo.versions.filtering.WildcardMatcher; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DependencyComparator; +import org.codehaus.mojo.versions.utils.SegmentUtils; import org.codehaus.plexus.util.StringUtils; 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.INCREMENTAL; import static org.codehaus.mojo.versions.api.Segment.MAJOR; -import static org.codehaus.mojo.versions.api.Segment.MINOR; /** * Displays all dependencies that have newer versions available. @@ -220,8 +219,9 @@ public class DisplayDependencyUpdatesMojo /** * Whether to allow the major version number to be changed. - * You need to set {@link #allowAnyUpdates} to false to - * get this configuration gets control. + * + *

Note: {@code false} also implies {@linkplain #allowAnyUpdates} + * to be {@code false}

* * @since 2.5 */ @@ -229,9 +229,10 @@ public class DisplayDependencyUpdatesMojo private boolean allowMajorUpdates; /** - * Whether to allow the minor version number to be changed. - * You need to set {@link #allowMajorUpdates} to false to - * get this configuration gets control. + *

Whether to allow the minor version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowAnyUpdates} + * and {@linkplain #allowMajorUpdates} to be {@code false}

* * @since 2.5 */ @@ -239,9 +240,11 @@ public class DisplayDependencyUpdatesMojo private boolean allowMinorUpdates; /** - * Whether to allow the incremental version number to be changed. - * You need to set {@link #allowMinorUpdates} to false to - * get this configuration gets control. + *

Whether to allow the incremental version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowAnyUpdates}, + * {@linkplain #allowMajorUpdates}, and {@linkplain #allowMinorUpdates} + * to be {@code false}

* * @since 2.5 */ @@ -691,28 +694,11 @@ private DependencyManagement getProjectDependencyManagement( MavenProject projec private Optional calculateUpdateScope() { - if ( !allowIncrementalUpdates && !allowMinorUpdates && !allowMajorUpdates && !allowAnyUpdates ) - { - throw new IllegalArgumentException( "One of: allowAnyUpdates, allowMajorUpdates, allowMinorUpdates, " - + "allowIncrementalUpdates must be true" ); - } - - if ( allowAnyUpdates && allowMajorUpdates && allowMinorUpdates ) - { - return empty(); - } - - if ( allowMajorUpdates && allowMinorUpdates ) - { - return of( MAJOR ); - } - - if ( allowMinorUpdates ) - { - return of( MINOR ); - } - - return of( INCREMENTAL ); + return allowAnyUpdates + ? empty() + : of( SegmentUtils.determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + allowIncrementalUpdates, getLog() ).map( s -> Segment.of( s.value() - 1 ) ) + .orElse( MAJOR ) ); } private void logUpdates( Map updates, String section ) diff --git a/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java b/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java index 97c15dc585..2120aac1cb 100644 --- a/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java @@ -44,6 +44,7 @@ import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.codehaus.mojo.versions.utils.SegmentUtils; /** * Displays properties that are linked to artifact versions and have updates available. @@ -107,7 +108,9 @@ public class DisplayPropertyUpdatesMojo private boolean allowMajorUpdates; /** - * Whether to allow the minor version number to be changed. + *

Whether to allow the minor version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

* * @since 2.5 */ @@ -115,7 +118,10 @@ public class DisplayPropertyUpdatesMojo private boolean allowMinorUpdates; /** - * Whether to allow the incremental version number to be changed. + *

Whether to allow the incremental version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} {@code false}

* * @since 2.5 */ @@ -158,7 +164,8 @@ public void execute() } Optional unchangedSegment = - determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); + SegmentUtils.determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + allowIncrementalUpdates, getLog() ); try { ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots, diff --git a/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java b/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java index 0ed8fe5b68..db997e1b09 100644 --- a/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java @@ -48,6 +48,7 @@ import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.codehaus.mojo.versions.utils.SegmentUtils; /** * Attempts to resolve dependency version ranges to the specific version being used in the build. For example a version @@ -94,7 +95,9 @@ public class ResolveRangesMojo private boolean allowMajorUpdates; /** - * Whether to allow the minor version number to be changed. + *

Whether to allow the minor version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

* * @since 2.5 */ @@ -102,7 +105,10 @@ public class ResolveRangesMojo private boolean allowMinorUpdates; /** - * Whether to allow the incremental version number to be changed. + *

Whether to allow the incremental version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} {@code false}

* * @since 2.5 */ @@ -312,8 +318,8 @@ private void resolvePropertyRanges( ModifiedPomXMLEventReader pom ) property.setVersion( currentVersion ); - Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, - allowIncrementalUpdates ); + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates, + allowMinorUpdates, allowIncrementalUpdates, getLog() ); // TODO: Check if we could add allowDowngrade ? try { diff --git a/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java index bece00a032..caa60b4ce8 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java @@ -48,6 +48,7 @@ import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DependencyBuilder; +import org.codehaus.mojo.versions.utils.SegmentUtils; import static org.apache.maven.shared.utils.StringUtils.isBlank; @@ -112,7 +113,9 @@ public class UpdateParentMojo extends AbstractVersionsUpdaterMojo protected boolean allowMajorUpdates = true; /** - * Whether to allow the minor version number to be changed. + *

Whether to allow the minor version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

* * @since 2.13.0 */ @@ -120,7 +123,10 @@ public class UpdateParentMojo extends AbstractVersionsUpdaterMojo protected boolean allowMinorUpdates = true; /** - * Whether to allow the incremental version number to be changed. + *

Whether to allow the incremental version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} {@code false}

* * @since 2.13.0 */ @@ -227,8 +233,8 @@ protected ArtifactVersion resolveTargetVersion( String initialVersion ) } final ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false ); - Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, - allowIncrementalUpdates ); + 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/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java index 9cb0e812e0..fda3617312 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java @@ -41,6 +41,7 @@ import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.codehaus.mojo.versions.utils.SegmentUtils; /** * Sets properties to the latest versions of specific artifacts. @@ -107,16 +108,20 @@ public class UpdatePropertiesMojo extends AbstractVersionsDependencyUpdaterMojo protected boolean allowMajorUpdates; /** - * Whether to allow the minor version number to be changed. + *

Whether to allow the minor version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

* * @since 2.4 */ - @Parameter( property = "allowMinorUpdates", - defaultValue = "true" ) + @Parameter( property = "allowMinorUpdates", defaultValue = "true" ) protected boolean allowMinorUpdates; /** - * Whether to allow the incremental version number to be changed. + *

Whether to allow the incremental version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} {@code false}

* * @since 2.4 */ @@ -181,8 +186,8 @@ protected void update( ModifiedPomXMLEventReader pom ) if ( canUpdateProperty ) { - Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, - allowIncrementalUpdates ); + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates, + allowMinorUpdates, allowIncrementalUpdates, getLog() ); try { ArtifactVersion targetVersion = diff --git a/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java index ea7e4c002c..dcc1d5d5c9 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java @@ -41,6 +41,7 @@ import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.codehaus.mojo.versions.utils.SegmentUtils; /** * Sets a property to the latest version in a given range of associated artifacts. @@ -109,7 +110,9 @@ public class UpdatePropertyMojo protected boolean allowMajorUpdates; /** - * Whether to allow the minor version number to be changed. + *

Whether to allow the minor version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

* * @since 2.4 */ @@ -117,7 +120,10 @@ public class UpdatePropertyMojo protected boolean allowMinorUpdates; /** - * Whether to allow the incremental version number to be changed. + *

Whether to allow the incremental version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} {@code false}

* * @since 2.4 */ @@ -166,7 +172,8 @@ protected void update( ModifiedPomXMLEventReader pom ) } Optional unchangedSegment = - determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); + SegmentUtils.determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + allowIncrementalUpdates, getLog() ); try { ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion, diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java index 3613f131dd..d0464d1f40 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java @@ -52,6 +52,7 @@ import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DependencyBuilder; +import org.codehaus.mojo.versions.utils.SegmentUtils; import static java.util.Collections.singletonList; @@ -82,7 +83,9 @@ public class UseLatestReleasesMojo protected boolean allowMajorUpdates; /** - * Whether to allow the minor version number to be changed. + *

Whether to allow the minor version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

* * @since 1.2 */ @@ -90,7 +93,10 @@ public class UseLatestReleasesMojo protected boolean allowMinorUpdates; /** - * Whether to allow the incremental version number to be changed. + *

Whether to allow the incremental version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} {@code false}

* * @since 1.2 */ @@ -148,8 +154,8 @@ protected void update( ModifiedPomXMLEventReader pom ) private void useLatestReleases( ModifiedPomXMLEventReader pom, Collection dependencies ) throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException { - Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, - allowIncrementalUpdates ); + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates, + allowMinorUpdates, allowIncrementalUpdates, getLog() ); for ( Dependency dep : dependencies ) { diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java index 50ee454d9f..b593d5263e 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java @@ -51,6 +51,7 @@ import org.codehaus.mojo.versions.ordering.VersionComparator; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DependencyBuilder; +import org.codehaus.mojo.versions.utils.SegmentUtils; import static java.util.Collections.singletonList; import static org.codehaus.mojo.versions.api.Segment.MAJOR; @@ -77,6 +78,7 @@ public class UseLatestSnapshotsMojo /** * Whether to allow the minor version number to be changed. * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

* @since 1.0-beta-1 */ @Parameter( property = "allowMinorUpdates", defaultValue = "false" ) @@ -85,6 +87,8 @@ public class UseLatestSnapshotsMojo /** * Whether to allow the incremental version number to be changed. * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} {@code false}

* @since 1.0-beta-1 */ @Parameter( property = "allowIncrementalUpdates", defaultValue = "true" ) @@ -148,8 +152,8 @@ protected void update( ModifiedPomXMLEventReader pom ) private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection dependencies ) throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException { - Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, - allowIncrementalUpdates ); + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates, + allowMinorUpdates, allowIncrementalUpdates, getLog() ); for ( Dependency dep : dependencies ) { diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java index da31fd138d..8b9b234301 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java @@ -45,9 +45,9 @@ import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; -import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DependencyBuilder; +import org.codehaus.mojo.versions.utils.SegmentUtils; import static java.util.Collections.singletonList; @@ -72,6 +72,7 @@ public class UseLatestVersionsMojo /** * Whether to allow the minor version number to be changed. * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

* @since 1.2 */ @Parameter( property = "allowMinorUpdates", defaultValue = "true" ) @@ -80,6 +81,8 @@ public class UseLatestVersionsMojo /** * Whether to allow the incremental version number to be changed. * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} {@code false}

* @since 1.2 */ @Parameter( property = "allowIncrementalUpdates", defaultValue = "true" ) @@ -108,9 +111,6 @@ public UseLatestVersionsMojo( RepositorySystem repositorySystem, super( repositorySystem, projectBuilder, artifactMetadataSource, wagonManager, artifactResolver ); } - /** - * {@inheritDoc} - */ @Override public void execute() throws MojoExecutionException, MojoFailureException { @@ -165,10 +165,8 @@ protected void update( ModifiedPomXMLEventReader pom ) private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection dependencies ) throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException { - Optional unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, - allowIncrementalUpdates ); - MajorMinorIncrementalFilter majorMinorIncfilter = - new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates, + allowMinorUpdates, allowIncrementalUpdates, getLog() ); for ( Dependency dep : dependencies ) { @@ -199,11 +197,11 @@ private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection 0 ) { String newVersion = filteredVersions[filteredVersions.length - 1].toString(); diff --git a/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java index 3050b2ce9a..a9e693a5bb 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java @@ -49,6 +49,7 @@ import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.codehaus.mojo.versions.utils.SegmentUtils; import static org.codehaus.mojo.versions.api.Segment.MAJOR; @@ -74,6 +75,7 @@ public class UseNextSnapshotsMojo /** * Whether to allow the minor version number to be changed. * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

* @since 1.0-beta-1 */ @Parameter( property = "allowMinorUpdates", defaultValue = "false" ) @@ -82,6 +84,8 @@ public class UseNextSnapshotsMojo /** * Whether to allow the incremental version number to be changed. * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} {@code false}

* @since 1.0-beta-1 */ @Parameter( property = "allowIncrementalUpdates", defaultValue = "true" ) @@ -137,8 +141,8 @@ private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection - unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, - allowIncrementalUpdates ); + unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + allowIncrementalUpdates, getLog() ); for ( Dependency dep : dependencies ) { diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilter.java b/src/main/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilter.java deleted file mode 100644 index 39b7cba69d..0000000000 --- a/src/main/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilter.java +++ /dev/null @@ -1,122 +0,0 @@ -package org.codehaus.mojo.versions.ordering; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - */ - -import java.util.LinkedList; -import java.util.List; -import java.util.Optional; - -import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.codehaus.mojo.versions.api.AbstractVersionDetails; - -/** - * This class will handle the edge cases where a version update would have happened based on the usage of version ranges - * to limit the replacement of the versions. We have currently the following scenario: The user defined - * allowMajorUpdates=false and allowMinorUpdates=true. An given artifact - * groupId:artifactId:2.0.8 and a repository which contains the following versions of this artifact: - *
    - *
  • 2.0.11
  • - *
  • 2.1.0-M1
  • - *
  • 2.2.1
  • - *
  • 3.0-beta-3
  • - *
  • 3.0
  • - *
  • 3.1.0
  • - *
  • 3.3.0
  • - *
- * The {@link AbstractVersionDetails#getNewerVersions(String, Optional, boolean, boolean)} will use an upper version of - * 2.1.0 to limit the versions to use. The result of this would be using 2.1.0-M1 which - * contradicts the wish of the user of not updating the minor version. The root cause of this is the comparison of Maven - * versions which will defined 2.1.0-M1 as less than 2.1.0. The method - * {@link #filter(ArtifactVersion, ArtifactVersion[])} will filter out those versions which violate the configuration - * {@link #allowMajorUpdates}, {@link #allowMinorUpdates} {@link #allowIncrementalUpdates}. - * - * @author Karl Heinz Marbaise - */ -public class MajorMinorIncrementalFilter -{ - - private final boolean allowMajorUpdates; - - private final boolean allowMinorUpdates; - - private final boolean allowIncrementalUpdates; - - public MajorMinorIncrementalFilter( boolean allowMajorUpdates, boolean allowMinorUpdates, - boolean allowIncrementalUpdates ) - { - this.allowMajorUpdates = allowMajorUpdates; - this.allowMinorUpdates = allowMinorUpdates; - this.allowIncrementalUpdates = allowIncrementalUpdates; - } - - /** - * @param selectedVersion The version which will be checked. - * @param newerVersions The list of identified versions which are greater or equal than the selectedVersion. - * @return The cleaned up list which obeys usage of {@link #allowMajorUpdates}, {@link #allowMinorUpdates}, - * {@link #allowIncrementalUpdates}. - */ - public ArtifactVersion[] filter( ArtifactVersion selectedVersion, ArtifactVersion[] newerVersions ) - { - List versionsToUse = new LinkedList<>(); - for ( ArtifactVersion artifactVersion : newerVersions ) - { - if ( artifactVersion.getMajorVersion() != selectedVersion.getMajorVersion() ) - { - if ( allowMajorUpdates ) - { - if ( !versionsToUse.contains( artifactVersion ) ) - { - versionsToUse.add( artifactVersion ); - } - } - } - else if ( artifactVersion.getMinorVersion() != selectedVersion.getMinorVersion() ) - { - if ( allowMinorUpdates ) - { - if ( !versionsToUse.contains( artifactVersion ) ) - { - versionsToUse.add( artifactVersion ); - } - } - } - else if ( artifactVersion.getIncrementalVersion() != selectedVersion.getIncrementalVersion() ) - { - if ( allowIncrementalUpdates ) - { - if ( !versionsToUse.contains( artifactVersion ) ) - { - versionsToUse.add( artifactVersion ); - } - } - } - else - { - // build number or qualifier. Will already be sorted and higher - if ( !versionsToUse.contains( artifactVersion ) ) - { - versionsToUse.add( artifactVersion ); - } - } - } - return versionsToUse.toArray( new ArtifactVersion[0] ); - - } -} diff --git a/src/main/java/org/codehaus/mojo/versions/utils/SegmentUtils.java b/src/main/java/org/codehaus/mojo/versions/utils/SegmentUtils.java new file mode 100644 index 0000000000..6d4d21c46d --- /dev/null +++ b/src/main/java/org/codehaus/mojo/versions/utils/SegmentUtils.java @@ -0,0 +1,95 @@ +package org.codehaus.mojo.versions.utils; + + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +import java.util.Optional; + +import org.apache.maven.plugin.logging.Log; +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; + +/** + * Utility class for manipulating with {@link Segment} objects + */ +public class SegmentUtils +{ + /** + *

Based on the passed flags, determines which segment which is not to be changed.

+ * + *

Also, logs the enriched values of the {@code allowMajorUpdates}, {@code allowMinorUpdates}, + * and {@code allowIncrementalUpdates} options so that {@code allowMinorUpdates} equal to {@code false} + * implies that {@code allowMajorUpdates} is also {@code false}.

+ *

Also, {@code allowIncrementalUpdates} equal to {@code false} + * implies that both {@code allowMajorUpdates} and {@code allowMinorUpdates} are also {@code false}.

+ * + * + * + * + * + * + * + * + * + * + * + * + *
Effective values for update options
allowMajorUpdatesallowMinorUpdatesallowIncrementalUpdates
truetruetrue
truetrue
true
+ * + * @param allowMajorUpdates whether all updates should be allowed + * @param allowMinorUpdates if major updates are disallowed, minor, incremental updates should be allowed + * @param allowIncrementalUpdates if major and minor updates are disallowed, incremental updates are allowed + * @param log If not null, the {@linkplain Log} object to log the selected scope + * @return Returns the segment (0-based) that is unchangeable. If any segment can change, returns -1. + */ + public static Optional determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates, + boolean allowIncrementalUpdates, Log log ) + { + 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 unchangedSegment = allowMajorUpdates && allowMinorUpdates + ? empty() + : allowMinorUpdates && allowIncrementalUpdates + ? of( MAJOR ) + : allowIncrementalUpdates + ? of( MINOR ) + : of( INCREMENTAL ); + if ( log != null && log.isInfoEnabled() ) + { + log.info( + unchangedSegment.map( s -> Segment.of( s.value() + 1 ).toString() ) + .orElse( "ALL" ) + " version changes allowed" ); + } + return unchangedSegment; + } +} diff --git a/src/test/java/org/codehaus/mojo/versions/UpdateParentMojoTest.java b/src/test/java/org/codehaus/mojo/versions/UpdateParentMojoTest.java index 43ca78b36f..0350f8c684 100644 --- a/src/test/java/org/codehaus/mojo/versions/UpdateParentMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/UpdateParentMojoTest.java @@ -337,7 +337,7 @@ public void testAllowMinorUpdates() }} ); mojo.allowMajorUpdates = false; mojo.allowMinorUpdates = true; - mojo.allowIncrementalUpdates = false; + mojo.allowIncrementalUpdates = true; ArtifactVersion newVersion = mojo.resolveTargetVersion( "0.8.0" ); diff --git a/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java b/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java index 47e0ed3393..e5b0ac4e1d 100644 --- a/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java @@ -122,9 +122,13 @@ public void testDependenciesDowngradeMinor() setVariableValueToObject( mojo, "allowSnapshots", false ); setVariableValueToObject( mojo, "allowMajorUpdates", false ); setVariableValueToObject( mojo, "allowMinorUpdates", true ); - setVariableValueToObject( mojo, "allowIncrementalUpdates", false ); + setVariableValueToObject( mojo, "allowIncrementalUpdates", true ); setVariableValueToObject( mojo, "allowDowngrade", true ); + mojo.getProject().getModel().setDependencies( Collections.singletonList( + DependencyBuilder.dependencyWith( "default-group", "dependency-artifact", "1.1.0-SNAPSHOT", + "default", "pom", SCOPE_COMPILE ) ) ); + try ( MockedStatic pomHelper = mockStatic( PomHelper.class ) ) { pomHelper.when( () -> PomHelper.setDependencyVersion( any(), any(), any(), any(), any(), any() ) ) @@ -132,7 +136,8 @@ public void testDependenciesDowngradeMinor() mojo.update( null ); } assertThat( changeRecorder.getChanges(), - hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", "1.0.0" ) ) ); + hasItem( new VersionChange( "default-group", "dependency-artifact", + "1.1.0-SNAPSHOT", "1.1.0" ) ) ); } @Test @@ -142,8 +147,8 @@ public void testDependenciesDowngradeMajor() setVariableValueToObject( mojo, "processDependencies", true ); setVariableValueToObject( mojo, "allowSnapshots", false ); setVariableValueToObject( mojo, "allowMajorUpdates", true ); - setVariableValueToObject( mojo, "allowMinorUpdates", false ); - setVariableValueToObject( mojo, "allowIncrementalUpdates", false ); + setVariableValueToObject( mojo, "allowMinorUpdates", true ); + setVariableValueToObject( mojo, "allowIncrementalUpdates", true ); setVariableValueToObject( mojo, "allowDowngrade", true ); try ( MockedStatic pomHelper = mockStatic( PomHelper.class ) ) @@ -153,7 +158,8 @@ public void testDependenciesDowngradeMajor() mojo.update( null ); } assertThat( changeRecorder.getChanges(), - hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", "0.9.0" ) ) ); + hasItem( new VersionChange( "default-group", "dependency-artifact", + "1.1.1-SNAPSHOT", "1.1.0" ) ) ); } @Test @@ -220,13 +226,13 @@ public void testPoisonDependencyVersion() "default", "pom", SCOPE_COMPILE ), DependencyBuilder.dependencyWith( "default-group", "poison-artifact", "1.1.1.1-SNAPSHOT", "default", "pom", SCOPE_COMPILE ) - ) ); + ) ); setVariableValueToObject( mojo, "processDependencies", true ); setVariableValueToObject( mojo, "allowSnapshots", false ); setVariableValueToObject( mojo, "allowMajorUpdates", false ); setVariableValueToObject( mojo, "allowMinorUpdates", true ); - setVariableValueToObject( mojo, "allowIncrementalUpdates", false ); + setVariableValueToObject( mojo, "allowIncrementalUpdates", true ); setVariableValueToObject( mojo, "allowDowngrade", true ); try ( MockedStatic pomHelper = mockStatic( PomHelper.class ) ) @@ -239,7 +245,7 @@ public void testPoisonDependencyVersion() // being present in the dependency list assertThat( changeRecorder.getChanges(), hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", - "1.0.0" ) ) ); + "1.1.0" ) ) ); } @Test diff --git a/src/test/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilterTest.java b/src/test/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilterTest.java deleted file mode 100644 index 4c5436c926..0000000000 --- a/src/test/java/org/codehaus/mojo/versions/ordering/MajorMinorIncrementalFilterTest.java +++ /dev/null @@ -1,102 +0,0 @@ -package org.codehaus.mojo.versions.ordering; - -import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; -import org.junit.Test; - -import static org.hamcrest.Matchers.arrayContaining; -import static org.junit.Assert.assertThat; - -public class MajorMinorIncrementalFilterTest -{ - - private ArtifactVersion[] newerVersions = new ArtifactVersion[] {version( "1.1.1-sp1" ), - version( "1.1.1-1" ), - version( "1.1.2" ), - version( "1.1.3" ), - version( "1.2.0" ), - version( "2.0.0-SNAPSHOT" )}; - - @Test - public void checkFilter() - { - ArtifactVersion selectedVersion = version( "1.1.1" ); - MajorMinorIncrementalFilter filter = new MajorMinorIncrementalFilter( true, true, true ); - ArtifactVersion[] filteredVersions = filter.filter( selectedVersion, newerVersions ); - assertThat( filteredVersions, arrayContaining( - version( "1.1.1-sp1" ), version( "1.1.1-1" ), - version( "1.1.2" ), version( "1.1.3" ), - version( "1.2.0" ), version( "2.0.0-SNAPSHOT" ) ) ); - } - - @Test - public void checkFilterWithNoMajorUpdates() - { - ArtifactVersion selectedVersion = version( "1.1.1" ); - MajorMinorIncrementalFilter filter = new MajorMinorIncrementalFilter( false, true, true ); - ArtifactVersion[] filteredVersions = filter.filter( selectedVersion, newerVersions ); - assertThat( filteredVersions, arrayContaining( version( "1.1.1-sp1" ), version( "1.1.1-1" ), - version( "1.1.2" ), version( "1.1.3" ), version( "1.2.0" ) ) ); - } - - @Test - public void checkFilterWithNoMajorOrMinorUpdates() - { - ArtifactVersion selectedVersion = version( "1.1.1" ); - MajorMinorIncrementalFilter filter = new MajorMinorIncrementalFilter( false, false, true ); - ArtifactVersion[] filteredVersions = filter.filter( selectedVersion, newerVersions ); - assertThat( filteredVersions, arrayContaining( version( "1.1.1-sp1" ), version( "1.1.1-1" ), - version( "1.1.2" ), version( "1.1.3" ) ) ); - } - - @Test - public void checkFilterWithNoMajorOrMinorOrIncrementalUpdates() - { - ArtifactVersion selectedVersion = version( "1.1.1" ); - MajorMinorIncrementalFilter filter = new MajorMinorIncrementalFilter( false, false, false ); - ArtifactVersion[] filteredVersions = filter.filter( selectedVersion, newerVersions ); - assertThat( filteredVersions, arrayContaining( version( "1.1.1-sp1" ), version( "1.1.1-1" ) ) ); - } - - @Test - public void checkFilterWithSnapshotAtSameVersion() - { - ArtifactVersion selectedVersion = version( "1.1.1-SNAPSHOT" ); - MajorMinorIncrementalFilter filter = new MajorMinorIncrementalFilter( false, false, false ); - ArtifactVersion[] filteredVersions = filter.filter( selectedVersion, - new ArtifactVersion[] {version( "1.1.1" )} ); - assertThat( filteredVersions, arrayContaining( version( "1.1.1" ) ) ); - } - - - @Test - public void checkFilterWithNonStandardVersions() - { - ArtifactVersion selectedVersion = version( "1.1.1.1" ); - MajorMinorIncrementalFilter filter = new MajorMinorIncrementalFilter( true, true, true ); - - ArtifactVersion[] newerVersions = new ArtifactVersion[] {version( "1.1.1.1-sp1" ), - version( "1.1.1.2" ), - version( "1.1.2.21" ), - version( "1.1.3.0" ), - version( "1.2.0" ), - version( "1.2.0.1" ), - version( "2.0.0-SNAPSHOT" )}; - - - ArtifactVersion[] filteredVersions = filter.filter( selectedVersion, newerVersions ); - assertThat( filteredVersions, - arrayContaining( version( "1.1.1.1-sp1" ), - version( "1.1.1.2" ), - version( "1.1.2.21" ), - version( "1.1.3.0" ), - version( "1.2.0" ), - version( "1.2.0.1" ), - version( "2.0.0-SNAPSHOT" ) ) ); - } - - private ArtifactVersion version( String versionString ) - { - return new DefaultArtifactVersion( versionString ); - } -}