Skip to content

Commit

Permalink
mojohaus#632 Fixed lower and upper bounds to keep milestones and rcs …
Browse files Browse the repository at this point in the history
…in the right majors.
  • Loading branch information
sultan committed Sep 16, 2022
1 parent b1fde0a commit 01fd67d
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,10 @@ protected abstract void doGenerateReport( Locale locale, Sink sink )
* @since 1.0-alpha-1
*/
protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange versionRange,
boolean allowingSnapshots, boolean usePluginRepositories )
Boolean allowingSnapshots, boolean usePluginRepositories )
throws MavenReportException
{
boolean includeSnapshots = this.allowSnapshots;
if ( allowingSnapshots )
{
includeSnapshots = true;
}
if ( allowingSnapshots )
{
includeSnapshots = false;
}
boolean includeSnapshots = allowingSnapshots != null ? allowingSnapshots : this.allowSnapshots;
try
{
final ArtifactVersions artifactVersions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,9 @@ protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange ver
boolean allowDowngrade )
throws ArtifactMetadataRetrievalException, MojoExecutionException
{
boolean includeSnapshots = this.allowSnapshots;
if ( Boolean.TRUE.equals( allowingSnapshots ) )
{
includeSnapshots = true;
}
if ( Boolean.FALSE.equals( allowingSnapshots ) )
{
includeSnapshots = false;
}
boolean includeSnapshots = allowingSnapshots != null ? allowingSnapshots : this.allowSnapshots;
final ArtifactVersions artifactVersions = getHelper().lookupArtifactVersions( artifact, usePluginRepositories );
return artifactVersions.getNewestVersion( versionRange, null, null, includeSnapshots,
true, true, allowDowngrade );
return artifactVersions.getNewestVersion( versionRange, null, includeSnapshots, allowDowngrade );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -170,11 +171,12 @@ private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection<Depen
}
try
{
// TODO check whether true can be passed as forceSnapshot parameter.
// TODO check whether true should be passed as forceSnapshot parameter in incrementSegment call.
ArtifactVersion upperBound =
segment >= 0 ? versionComparator.incrementSegment( lowerBound, segment, false ) : null;
getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) );
ArtifactVersion[] newer = versions.getVersions( lowerBound, upperBound, true, false, false );
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );
ArtifactVersion[] newer = versions.getVersions( restriction, true );
getLog().debug( "Candidate versions " + Arrays.asList( newer ) );

String latestVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -152,11 +153,12 @@ private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection<Depende
}
try
{
// TODO check whether true can be passed as forceSnapshot parameter.
// TODO check whether true should be passed as forceSnapshot parameter in incrementSegment call.
ArtifactVersion upperBound =
segment >= 0 ? versionComparator.incrementSegment( lowerBound, segment, false ) : null;
getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) );
ArtifactVersion[] newer = versions.getVersions( lowerBound, upperBound, true, false, false );
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );
ArtifactVersion[] newer = versions.getVersions( restriction, true );
getLog().debug( "Candidate versions " + Arrays.asList( newer ) );
for ( ArtifactVersion artifactVersion : newer )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.artifact.versioning.VersionRange;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.VersionComparator;
Expand Down Expand Up @@ -129,23 +130,25 @@ public final ArtifactVersion[] getVersions()

public final ArtifactVersion[] getVersions( VersionRange versionRange, boolean includeSnapshots )
{
return getVersions( versionRange, null, null, includeSnapshots, true, true );
return getVersions( versionRange, null, includeSnapshots );
}

public final ArtifactVersion[] getVersions( ArtifactVersion currentVersion, ArtifactVersion upperBound )
public final ArtifactVersion[] getVersions( ArtifactVersion lowerBound, ArtifactVersion upperBound )
{
return getVersions( currentVersion, upperBound, isIncludeSnapshots() );
return getVersions( lowerBound, upperBound, isIncludeSnapshots() );
}

public final ArtifactVersion[] getVersions( ArtifactVersion currentVersion, ArtifactVersion upperBound,
public final ArtifactVersion[] getVersions( ArtifactVersion lowerBound, ArtifactVersion upperBound,
boolean includeSnapshots )
{
return getVersions( currentVersion, upperBound, includeSnapshots, false, false );
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );
return getVersions( restriction, includeSnapshots );
}

private ArtifactVersion[] getNewerVersions( ArtifactVersion version, boolean includeSnapshots )
{
return getVersions( version, null, includeSnapshots, false, true );
Restriction restriction = new Restriction( version, false, null, false );
return getVersions( restriction, includeSnapshots );
}

public final ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound )
Expand All @@ -156,25 +159,23 @@ public final ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, Artif
public final ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound,
boolean includeSnapshots )
{
return getNewestVersion( lowerBound, upperBound, includeSnapshots, false, false );
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );
return getNewestVersion( restriction, includeSnapshots );
}

public final ArtifactVersion getNewestVersion( VersionRange versionRange, ArtifactVersion lowerBound,
ArtifactVersion upperBound, boolean includeSnapshots,
boolean includeLower, boolean includeUpper )
public final ArtifactVersion getNewestVersion( VersionRange versionRange, Restriction restriction,
boolean includeSnapshots )
{
return getNewestVersion( versionRange, lowerBound, upperBound, includeSnapshots, includeLower,
includeUpper, false );
return getNewestVersion( versionRange, restriction, includeSnapshots, false );
}

private static <T> Iterable<T> reverse( T[] array )
{
return Arrays.stream( array ).sorted( Collections.reverseOrder() ).collect( Collectors.toList() );
}

public final ArtifactVersion getNewestVersion( VersionRange versionRange, ArtifactVersion lowerBound,
ArtifactVersion upperBound, boolean includeSnapshots,
boolean includeLower, boolean includeUpper, boolean allowDowngrade )
public final ArtifactVersion getNewestVersion( VersionRange versionRange, Restriction restriction,
boolean includeSnapshots, boolean allowDowngrade )
{
final VersionComparator versionComparator = getVersionComparator();
// reverse( getVersions( ... ) ) will contain versions sorted from latest to oldest,
Expand All @@ -186,13 +187,7 @@ public final ArtifactVersion getNewestVersion( VersionRange versionRange, Artifa
{
continue;
}
int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate );
int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate );
if ( lower > 0 || upper < 0 )
{
continue;
}
if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) )
if ( restriction != null && !isVersionInRestriction( restriction, candidate ) )
{
continue;
}
Expand All @@ -205,16 +200,14 @@ public final ArtifactVersion getNewestVersion( VersionRange versionRange, Artifa
return null;
}

public final ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound,
boolean includeSnapshots, boolean includeLower,
boolean includeUpper )
public final ArtifactVersion getNewestVersion( Restriction restriction, boolean includeSnapshots )
{
return getNewestVersion( null, lowerBound, upperBound, includeSnapshots, includeLower, includeUpper );
return getNewestVersion( null, restriction, includeSnapshots );
}

public final ArtifactVersion getNewestVersion( VersionRange versionRange, boolean includeSnapshots )
{
return getNewestVersion( versionRange, null, null, includeSnapshots, true, true );
return getNewestVersion( versionRange, null, includeSnapshots );
}

public final boolean containsVersion( String version )
Expand Down Expand Up @@ -276,7 +269,8 @@ public final ArtifactVersion[] getNewerVersions( String versionString, int upper
ArtifactVersion upperBound = upperBoundSegment == -1 ? null
: getVersionComparator().incrementSegment( lowerBound, upperBoundSegment, true );

return getVersions( lowerBound, upperBound, includeSnapshots, allowDowngrade, allowDowngrade );
Restriction restriction = new Restriction( lowerBound, allowDowngrade, upperBound, allowDowngrade );
return getVersions( restriction, includeSnapshots );
}

public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound )
Expand All @@ -286,25 +280,23 @@ public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, Artif

public final ArtifactVersion getOldestVersion( VersionRange versionRange, boolean includeSnapshots )
{
return getOldestVersion( versionRange, null, null, includeSnapshots, true, true );
return getOldestVersion( versionRange, null, includeSnapshots );
}

public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound,
boolean includeSnapshots )
{
return getOldestVersion( lowerBound, upperBound, includeSnapshots, false, false );
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );
return getOldestVersion( restriction, includeSnapshots );
}

public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound,
boolean includeSnapshots, boolean includeLower,
boolean includeUpper )
public final ArtifactVersion getOldestVersion( Restriction restriction, boolean includeSnapshots )
{
return getOldestVersion( null, lowerBound, upperBound, includeSnapshots, includeLower, includeUpper );
return getOldestVersion( null, restriction, includeSnapshots );
}

public final ArtifactVersion getOldestVersion( VersionRange versionRange, ArtifactVersion lowerBound,
ArtifactVersion upperBound, boolean includeSnapshots,
boolean includeLower, boolean includeUpper )
public final ArtifactVersion getOldestVersion( VersionRange versionRange, Restriction restriction,
boolean includeSnapshots )
{
ArtifactVersion oldest = null;
final VersionComparator versionComparator = getVersionComparator();
Expand All @@ -314,13 +306,7 @@ public final ArtifactVersion getOldestVersion( VersionRange versionRange, Artifa
{
continue;
}
int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate );
int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate );
if ( lower > 0 || upper < 0 )
{
continue;
}
if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) )
if ( restriction != null && !isVersionInRestriction( restriction, candidate ) )
{
continue;
}
Expand All @@ -340,15 +326,13 @@ else if ( versionComparator.compare( oldest, candidate ) > 0 )
return oldest;
}

public final ArtifactVersion[] getVersions( ArtifactVersion lowerBound, ArtifactVersion upperBound,
boolean includeSnapshots, boolean includeLower, boolean includeUpper )
public final ArtifactVersion[] getVersions( Restriction restriction, boolean includeSnapshots )
{
return getVersions( null, lowerBound, upperBound, includeSnapshots, includeLower, includeUpper );
return getVersions( null, restriction, includeSnapshots );
}

public final ArtifactVersion[] getVersions( VersionRange versionRange, ArtifactVersion lowerBound,
ArtifactVersion upperBound, boolean includeSnapshots,
boolean includeLower, boolean includeUpper )
public final ArtifactVersion[] getVersions( VersionRange versionRange, Restriction restriction,
boolean includeSnapshots )
{
final VersionComparator versionComparator = getVersionComparator();
Set<ArtifactVersion> result = new TreeSet<>( versionComparator );
Expand All @@ -358,13 +342,7 @@ public final ArtifactVersion[] getVersions( VersionRange versionRange, ArtifactV
{
continue;
}
int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate );
int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate );
if ( lower > 0 || upper < 0 )
{
continue;
}
if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) )
if ( restriction != null && !isVersionInRestriction( restriction, candidate ) )
{
continue;
}
Expand Down Expand Up @@ -505,17 +483,20 @@ public final ArtifactVersion[] getAllUpdates( VersionRange versionRange )

public ArtifactVersion getOldestUpdate( VersionRange versionRange, boolean includeSnapshots )
{
return getOldestVersion( versionRange, getCurrentVersion(), null, includeSnapshots, false, true );
Restriction restriction = new Restriction( getCurrentVersion(), false, null, false );
return getOldestVersion( versionRange, restriction, includeSnapshots );
}

public ArtifactVersion getNewestUpdate( VersionRange versionRange, boolean includeSnapshots )
{
return getNewestVersion( versionRange, getCurrentVersion(), null, includeSnapshots, false, true );
Restriction restriction = new Restriction( getCurrentVersion(), false, null, false );
return getNewestVersion( versionRange, restriction, includeSnapshots );
}

public ArtifactVersion[] getAllUpdates( VersionRange versionRange, boolean includeSnapshots )
{
return getVersions( versionRange, getCurrentVersion(), null, includeSnapshots, false, true );
Restriction restriction = new Restriction( getCurrentVersion(), false, null, false );
return getVersions( versionRange, restriction, includeSnapshots );
}

/**
Expand Down Expand Up @@ -589,4 +570,33 @@ protected Optional<String> getLowerBound( ArtifactVersion version, int unchanged
}
return of( newVersion.toString() );
}

/**
* Checks if the candidate version is in the range of the restriction.
* a custom comparator is/can be used to have milestones and rcs before final releases,
* which is not yet possible with {@link Restriction#containsVersion(ArtifactVersion)}.
* @param restriction the range to check against.
* @param candidate the version to check.
* @return true if the candidate version is within the range of the restriction parameter.
*/
private boolean isVersionInRestriction( Restriction restriction, ArtifactVersion candidate )
{
ArtifactVersion lowerBound = restriction.getLowerBound();
ArtifactVersion upperBound = restriction.getUpperBound();
boolean includeLower = restriction.isLowerBoundInclusive();
boolean includeUpper = restriction.isUpperBoundInclusive();
final VersionComparator versionComparator = getVersionComparator();
int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate );
int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate );
if ( lower > 0 || upper < 0 )
{
return false;
}
if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) )
{
return false;
}
return true;
}

}
Loading

0 comments on commit 01fd67d

Please sign in to comment.