Skip to content

Commit

Permalink
Fixing #231 bug/feature: added "allowDowngrade", default "false", fi…
Browse files Browse the repository at this point in the history
…xed range handling wrt downgrading
  • Loading branch information
jarmoniuk authored and slawekjaranowski committed Sep 1, 2022
1 parent 52cd445 commit b551d90
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 91 deletions.
Expand Up @@ -289,6 +289,27 @@ public void execute()
*/
protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange versionRange,
Boolean allowingSnapshots, boolean usePluginRepositories )
throws ArtifactMetadataRetrievalException, MojoExecutionException
{
return findLatestVersion( artifact, versionRange, allowingSnapshots, usePluginRepositories, false );
}

/**
* Finds the latest version of the specified artifact that matches the version range.
*
* @param artifact The artifact.
* @param versionRange The version range.
* @param allowingSnapshots <code>null</code> for no override, otherwise the local override to apply.
* @param usePluginRepositories Use plugin repositories
* @return The latest version of the specified artifact that matches the specified version range or
* <code>null</code> if no matching version could be found.
* @throws ArtifactMetadataRetrievalException If the artifact metadata could not be found.
* @throws MojoExecutionException if something goes wrong.
* @since 1.0-alpha-1
*/
protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange versionRange,
Boolean allowingSnapshots, boolean usePluginRepositories,
boolean allowDowngrade )
throws ArtifactMetadataRetrievalException, MojoExecutionException
{
boolean includeSnapshots = this.allowSnapshots;
Expand All @@ -301,7 +322,8 @@ protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange ver
includeSnapshots = false;
}
final ArtifactVersions artifactVersions = getHelper().lookupArtifactVersions( artifact, usePluginRepositories );
return artifactVersions.getNewestVersion( versionRange, includeSnapshots );
return artifactVersions.getNewestVersion( versionRange, null, null, includeSnapshots,
true, true, allowDowngrade );
}

/**
Expand Down
34 changes: 26 additions & 8 deletions src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java
Expand Up @@ -65,6 +65,17 @@ public class UpdateParentMojo extends AbstractVersionsUpdaterMojo
@Parameter( property = "forceUpdate", defaultValue = "false" )
protected boolean forceUpdate = false;

/**
* <p>Whether to downgrade a snapshot dependency if <code>allowSnapshots</code> is <code>false</code>
* and there exists a version within the range fulfilling the criteria.</p>
* <p>Default <code>false</code></p>
*
* @since 2.12.0
*/
@Parameter( property = "allowDowngrade",
defaultValue = "false" )
protected boolean allowDowngrade;

// -------------------------- OTHER METHODS --------------------------

/**
Expand Down Expand Up @@ -98,33 +109,40 @@ protected void update( ModifiedPomXMLEventReader pom )
version = parentVersion;
}

Dependency dependency = new Dependency();
dependency.setGroupId( getProject().getParent().getGroupId() );
dependency.setArtifactId( getProject().getParent().getArtifactId() );
dependency.setVersion( version );
dependency.setType( "pom" );
Artifact artifact = getHelper().createDependencyArtifact( dependency );

VersionRange versionRange;
try
{
versionRange = VersionRange.createFromVersionSpec( version );
if ( versionRange.getRecommendedVersion() != null )
{
versionRange = versionRange.restrict(
VersionRange.createFromVersionSpec( "[" + versionRange.getRecommendedVersion() + ",)" ) );
}
}
catch ( InvalidVersionSpecificationException e )
{
throw new MojoExecutionException( "Invalid version range specification: " + version, e );
}

Dependency dependency = new Dependency();
dependency.setGroupId( getProject().getParent().getGroupId() );
dependency.setArtifactId( getProject().getParent().getArtifactId() );
dependency.setVersion( version );
dependency.setType( "pom" );
Artifact artifact = getHelper().createDependencyArtifact( dependency );

ArtifactVersion artifactVersion;
try
{
artifactVersion = findLatestVersion( artifact, versionRange, null, false );
artifactVersion = findLatestVersion( artifact, versionRange, false, true,
allowDowngrade );
}
catch ( ArtifactMetadataRetrievalException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}


if ( !shouldApplyUpdate( artifact, currentVersion, artifactVersion, forceUpdate ) )
{
return;
Expand Down
Expand Up @@ -19,8 +19,11 @@
* under the License.
*/

import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.versioning.ArtifactVersion;
Expand Down Expand Up @@ -156,11 +159,26 @@ public final ArtifactVersion getNewestVersion( VersionRange versionRange, Artifa
ArtifactVersion upperBound, boolean includeSnapshots,
boolean includeLower, boolean includeUpper )
{
ArtifactVersion latest = null;
return getNewestVersion( versionRange, lowerBound, upperBound, includeSnapshots, includeLower,
includeUpper, 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 )
{
final VersionComparator versionComparator = getVersionComparator();
for ( ArtifactVersion candidate : getVersions( includeSnapshots ) )
// reverse( getVersions( ... ) ) will contain versions sorted from latest to oldest,
// so we only need to find the first candidate fulfilling the criteria
for ( ArtifactVersion candidate : reverse( getVersions( includeSnapshots ) ) )
{
if ( versionRange != null && !ArtifactVersions.isVersionInRange( candidate, versionRange ) )
if ( !allowDowngrade && versionRange != null
&& !ArtifactVersions.isVersionInRange( candidate, versionRange ) )
{
continue;
}
Expand All @@ -178,17 +196,9 @@ public final ArtifactVersion getNewestVersion( VersionRange versionRange, Artifa
{
continue;
}
if ( latest == null )
{
latest = candidate;
}
else if ( versionComparator.compare( latest, candidate ) < 0 )
{
latest = candidate;
}

return candidate;
}
return latest;
return null;
}

public final ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound,
Expand Down
26 changes: 6 additions & 20 deletions src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java
Expand Up @@ -20,7 +20,6 @@
*/

import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

Expand Down Expand Up @@ -96,7 +95,8 @@ public static boolean isVersionInRange( ArtifactVersion version, VersionRange ra
{
return false;
}
for ( Restriction r : ( (List<Restriction>) range.getRestrictions() ) )

for ( Restriction r : range.getRestrictions() )
{
if ( r.containsVersion( version ) )
{
Expand Down Expand Up @@ -157,24 +157,10 @@ public String getArtifactId()

public ArtifactVersion[] getVersions( boolean includeSnapshots )
{
Set<ArtifactVersion> result;
if ( includeSnapshots )
{
result = versions;
}
else
{
result = new TreeSet<>( versionComparator );
for ( ArtifactVersion candidate : versions )
{
if ( ArtifactUtils.isSnapshot( candidate.toString() ) )
{
continue;
}
result.add( candidate );
}
}
return result.toArray( new ArtifactVersion[0] );
return includeSnapshots
? versions.toArray( new ArtifactVersion[0] )
: versions.stream().filter( v -> !ArtifactUtils.isSnapshot( v.toString() ) )
.toArray( ArtifactVersion[]::new );
}

public VersionComparator getVersionComparator()
Expand Down

0 comments on commit b551d90

Please sign in to comment.