Skip to content

Commit

Permalink
mojohaus#283: allowMinorUpdates false should imply allowMajorUpdates …
Browse files Browse the repository at this point in the history
…false
  • Loading branch information
andrzejj0 committed Oct 8, 2022
1 parent 97c6333 commit 6cb1ecc
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -539,41 +532,6 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A
return true;
}

/**
* <p>Based on the passed flags, determines which segment (0-based), which is not to be changed.</p>
* <p>The method will return, depending on the first parameter on the list to be true:
* <ul>
* <li>{@code allowMajorUpdates}: -1</li>
* <li>{@code allowMinorUpdates}: 0</li>
* <li>{@code allowIncrementalUpdates}: 1</li>
* <li>(none): 2</li>
* </ul>
*
* 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<Segment> determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates,
boolean allowIncrementalUpdates )
{
Optional<Segment> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -158,7 +159,8 @@ public void execute()
}

Optional<Segment> unchangedSegment =
determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
SegmentUtils.determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates, getLog() );
try
{
ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -312,8 +313,8 @@ private void resolvePropertyRanges( ModifiedPomXMLEventReader pom )

property.setVersion( currentVersion );

Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates,
allowMinorUpdates, allowIncrementalUpdates, getLog() );
// TODO: Check if we could add allowDowngrade ?
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -227,8 +228,8 @@ protected ArtifactVersion resolveTargetVersion( String initialVersion )
}

final ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
Optional<Segment> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -181,8 +182,8 @@ protected void update( ModifiedPomXMLEventReader pom )

if ( canUpdateProperty )
{
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates,
allowMinorUpdates, allowIncrementalUpdates, getLog() );
try
{
ArtifactVersion targetVersion =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -166,7 +167,8 @@ protected void update( ModifiedPomXMLEventReader pom )
}

Optional<Segment> unchangedSegment =
determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
SegmentUtils.determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates, getLog() );
try
{
ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -148,8 +149,8 @@ protected void update( ModifiedPomXMLEventReader pom )
private void useLatestReleases( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates,
allowMinorUpdates, allowIncrementalUpdates, getLog() );

for ( Dependency dep : dependencies )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -148,8 +149,8 @@ protected void update( ModifiedPomXMLEventReader pom )
private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates,
allowMinorUpdates, allowIncrementalUpdates, getLog() );

for ( Dependency dep : dependencies )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -108,9 +108,6 @@ public UseLatestVersionsMojo( RepositorySystem repositorySystem,
super( repositorySystem, projectBuilder, artifactMetadataSource, wagonManager, artifactResolver );
}

/**
* {@inheritDoc}
*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
Expand Down Expand Up @@ -165,10 +162,8 @@ protected void update( ModifiedPomXMLEventReader pom )
private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
MajorMinorIncrementalFilter majorMinorIncfilter =
new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates,
allowMinorUpdates, allowIncrementalUpdates, getLog() );

for ( Dependency dep : dependencies )
{
Expand Down Expand Up @@ -199,11 +194,11 @@ private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection<Depend

try
{
// TODO consider creating a getNewestVersion method in the Details services.
ArtifactVersion[] newerVersions = versions.getNewerVersions( version, unchangedSegment, allowSnapshots,
allowDowngrade );

ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( selectedVersion, newerVersions );
//ArtifactVersion[] filteredVersions = filter( unchangedSegment, selectedVersion, newerVersions );
ArtifactVersion[] filteredVersions = newerVersions;
if ( filteredVersions.length > 0 )
{
String newVersion = filteredVersions[filteredVersions.length - 1].toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -137,8 +138,8 @@ private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection<Depende
throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
Optional<Segment>
unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
unchangedSegment = SegmentUtils.determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates, getLog() );

for ( Dependency dep : dependencies )
{
Expand Down

This file was deleted.

Loading

0 comments on commit 6cb1ecc

Please sign in to comment.