Skip to content

Commit

Permalink
Implementing #213: new argument: allowDowngrade, only valid if allowS…
Browse files Browse the repository at this point in the history
…napshots is false, will optionally downgrade the dependency version if its current version is a snapshot
  • Loading branch information
andrzejj0 authored and slawekjaranowski committed Aug 29, 2022
1 parent 01dcc60 commit a440adc
Show file tree
Hide file tree
Showing 5 changed files with 366 additions and 91 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<version>4.7.0</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
import javax.xml.stream.XMLStreamException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Collections;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
Expand Down Expand Up @@ -75,8 +74,33 @@ public class UseLatestVersionsMojo
@Parameter( property = "allowIncrementalUpdates", defaultValue = "true" )
private boolean allowIncrementalUpdates;

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

// ------------------------------ METHODS --------------------------


/**
* {@inheritDoc}
*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
if ( allowDowngrade && allowSnapshots )
{
throw new MojoExecutionException( "allowDowngrade is only valid with allowSnapshots equal to false" );
}
super.execute();
}

/**
* @param pom the pom to update.
* @throws org.apache.maven.plugin.MojoExecutionException when things go wrong
Expand Down Expand Up @@ -109,9 +133,7 @@ protected void update( ModifiedPomXMLEventReader pom )
dependency.setGroupId( getProject().getParent().getGroupId() );
dependency.setVersion( getProject().getParent().getVersion() );
dependency.setType( "pom" );
List list = new ArrayList();
list.add( dependency );
useLatestVersions( pom, list );
useLatestVersions( pom, Collections.singletonList( dependency ) );
}
}
catch ( ArtifactMetadataRetrievalException | IOException e )
Expand Down Expand Up @@ -154,7 +176,8 @@ private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection<Depend
getLog().debug( "Looking for newer versions of " + toString( dep ) );
ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );

ArtifactVersion[] newerVersions = versions.getNewerVersions( version, segment, allowSnapshots );
ArtifactVersion[] newerVersions = versions.getNewerVersions( version, segment, allowSnapshots,
allowDowngrade );

ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( selectedVersion, newerVersions );
if ( filteredVersions.length > 0 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
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 @@ -135,30 +136,6 @@ public final ArtifactVersion[] getVersions( ArtifactVersion currentVersion, Arti
return getVersions( currentVersion, upperBound, includeSnapshots, false, false );
}

/**
* Gets newer versions of the specified artifact version.
*
* @param version The current version of the artifact.
* @param upperBoundFixedSegment Indicates the segment in the version number that cannot be changed. For example, a
* value of 0 indicates that the major version number cannot be changed. A value of -1 indicates any
* segment value can be changed.
* @param includeSnapshots Whether to include snapshot versions.
* @return Returns the newer artifact versions.
*/
private ArtifactVersion[] getNewerVersions( ArtifactVersion version, int upperBoundFixedSegment,
boolean includeSnapshots )
{
ArtifactVersion lowerBound = version;
ArtifactVersion upperBound = null;

if ( upperBoundFixedSegment != -1 )
{
upperBound = getVersionComparator().incrementSegment( lowerBound, upperBoundFixedSegment );
}

return getVersions( version, upperBound, includeSnapshots, false, false );
}

private ArtifactVersion[] getNewerVersions( ArtifactVersion version, boolean includeSnapshots )
{
return getVersions( version, null, includeSnapshots, false, true );
Expand Down Expand Up @@ -243,9 +220,43 @@ public final ArtifactVersion[] getNewerVersions( String version, boolean include
return getNewerVersions( new DefaultArtifactVersion( version ), includeSnapshots );
}

/**
* Returns an array of newer versions than the given version, given the upper bound segment and whether snapshots
* should be included.
*
* @param version current version
* @param upperBoundSegment the upper bound segment
* @param includeSnapshots whether snapshot versions should be included
* @deprecated please use {@link AbstractVersionDetails#getNewerVersions(String, int, boolean, boolean)} instead
* @return array of newer versions fulfilling the criteria
*/
@Deprecated
public final ArtifactVersion[] getNewerVersions( String version, int upperBoundSegment, boolean includeSnapshots )
{
return getNewerVersions( new DefaultArtifactVersion( version ), upperBoundSegment, includeSnapshots );
return getNewerVersions( version, upperBoundSegment, includeSnapshots, false );
}

/**
* Returns an array of newer versions than the given version, given the upper bound segment and whether snapshots
* should be included.
*
* @param versionString current version
* @param upperBoundSegment the upper bound segment
* @param includeSnapshots whether snapshot versions should be included
* @param allowDowngrade whether to allow downgrading if the current version is a snapshots and snapshots
* are disallowed
* @return array of newer versions fulfilling the criteria
*/
public final ArtifactVersion[] getNewerVersions( String versionString, int upperBoundSegment,
boolean includeSnapshots, boolean allowDowngrade )
{
ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString );
ArtifactVersion lowerBound =
allowDowngrade ? getLowerBoundArtifactVersion( currentVersion, upperBoundSegment ) : currentVersion;
ArtifactVersion upperBound = upperBoundSegment == -1 ? null
: getVersionComparator().incrementSegment( lowerBound, upperBoundSegment );

return getVersions( lowerBound, upperBound, includeSnapshots, allowDowngrade, allowDowngrade );
}

public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound )
Expand Down Expand Up @@ -483,4 +494,52 @@ public ArtifactVersion[] getAllUpdates( VersionRange versionRange, boolean inclu
{
return getVersions( versionRange, getCurrentVersion(), null, includeSnapshots, false, true );
}

protected ArtifactVersion getLowerBoundArtifactVersion( ArtifactVersion version, int segment )
{
String lowerBound = getLowerBound( version, segment );
return lowerBound != null ? new DefaultArtifactVersion( lowerBound ) : null;
}

protected String getLowerBound( ArtifactVersion version, int segment )
{
if ( segment < 0 )
{
return null;
}

int segmentCount = getVersionComparator().getSegmentCount( version );
if ( segment > segmentCount )
{
throw new InvalidSegmentException( segment, segmentCount,
version.toString() );
}

StringBuilder newVersion = new StringBuilder();
newVersion.append( version.getMajorVersion() );
if ( segmentCount > 0 )
{
newVersion.append( "." )
.append( segment >= 1 ? version.getMinorVersion() : 0 );
}
if ( segmentCount > 1 )
{
newVersion.append( "." )
.append( segment >= 2 ? version.getIncrementalVersion() : 0 );
}
if ( segmentCount > 2 )
{
if ( version.getQualifier() != null )
{
newVersion.append( "-" )
.append( segment >= 3 ? version.getQualifier() : "0" );
}
else
{
newVersion.append( "-" )
.append( segment >= 3 ? version.getBuildNumber() : "0" );
}
}
return newVersion.toString();
}
}
67 changes: 8 additions & 59 deletions src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
Expand All @@ -39,7 +38,6 @@
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.mojo.versions.Property;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.VersionComparator;

/**
Expand Down Expand Up @@ -145,12 +143,8 @@ public ArtifactAssociation[] getAssociations()

private VersionComparator[] lookupComparators()
{
Set<VersionComparator> result = new HashSet();
for ( ArtifactAssociation association : associations )
{
result.add( helper.getVersionComparator( association.getArtifact() ) );
}
return result.toArray( new VersionComparator[0] );
return associations.stream().map( association -> helper.getVersionComparator( association.getArtifact() ) )
.distinct().toArray( VersionComparator[]::new );
}

/**
Expand Down Expand Up @@ -346,19 +340,15 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert
throw new MojoExecutionException( e.getMessage(), e );
}

ArtifactVersion lowerBoundArtifactVersion = null;
ArtifactVersion lowerBoundArtifactVersion = helper.createArtifactVersion( currentVersion );
if ( allowDowngrade )
{
if ( segment != -1 )
{
lowerBoundArtifactVersion = getLowerBound( helper, currentVersion, segment );
}
helper.getLog().debug( "lowerBoundArtifactVersion is null based on allowDowngrade:" + allowDowngrade );
String updatedVersion = getLowerBound( lowerBoundArtifactVersion, segment );
lowerBoundArtifactVersion = updatedVersion != null ? helper.createArtifactVersion( updatedVersion ) : null;
}
else
if ( helper.getLog().isDebugEnabled() )
{
lowerBoundArtifactVersion = helper.createArtifactVersion( currentVersion );
helper.getLog().debug( "lowerBoundArtifactVersion: " + lowerBoundArtifactVersion.toString() );
helper.getLog().debug( "lowerBoundArtifactVersion: " + lowerBoundArtifactVersion );
}

ArtifactVersion upperBound = null;
Expand All @@ -375,7 +365,7 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert
if ( property.isSearchReactor() )
{
helper.getLog().debug( "Property ${" + property.getName() + "}: Searching reactor for a valid version..." );
Collection reactorArtifacts = helper.extractArtifacts( reactorProjects );
Set<Artifact> reactorArtifacts = helper.extractArtifacts( reactorProjects );
ArtifactVersion[] reactorVersions = getVersions( reactorArtifacts );
helper.getLog().debug( "Property ${" + property.getName()
+ "}: Set of valid available versions from the reactor is " + Arrays.asList(
Expand Down Expand Up @@ -527,45 +517,4 @@ public ArtifactVersion incrementSegment( ArtifactVersion v, int segment )
}

}


private ArtifactVersion getLowerBound( VersionsHelper helper,
String currentVersion, int segment )
{
ArtifactVersion version = helper.createArtifactVersion( currentVersion );
int segmentCount = getVersionComparator().getSegmentCount( version );
if ( segment < 0 || segment > segmentCount )
{
throw new InvalidSegmentException( segment, segmentCount,
currentVersion );
}

StringBuilder newVersion = new StringBuilder();
newVersion.append( segment >= 0 ? version.getMajorVersion() : 0 );
if ( segmentCount > 0 )
{
newVersion.append( "." )
.append( segment >= 1 ? version.getMinorVersion() : 0 );
}
if ( segmentCount > 1 )
{
newVersion.append( "." )
.append( segment >= 2 ? version.getIncrementalVersion() : 0 );
}
if ( segmentCount > 2 )
{
if ( version.getQualifier() != null )
{
newVersion.append( "-" )
.append( segment >= 3 ? version.getQualifier() : "0" );
}
else
{
newVersion.append( "-" )
.append( segment >= 3 ? version.getBuildNumber() : "0" );
}
}
return helper.createArtifactVersion( newVersion.toString() );
}

}
Loading

0 comments on commit a440adc

Please sign in to comment.