Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing #231 bug/feature: added "allowDowngrade", default "false", fixed range handling wrt downgrading #662

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ public void replace( String replacement )
return;
}
pom.replace( start, end, replacement );
int delta = replacement.length() - lastEnd - lastStart;
int delta = replacement.length() - ( lastEnd - lastStart );
nextDelta += delta;
for ( int i = 0; i < MAX_MARKS; i++ )
{
Expand Down Expand Up @@ -563,7 +563,7 @@ public void replaceMark( int index, String replacement )
return;
}
pom.replace( start, end, replacement );
int delta = replacement.length() - markEnd[index] - markStart[index];
int delta = replacement.length() - ( markEnd[index] - markStart[index] );
nextDelta += delta;
if ( lastStart == markStart[index] && lastEnd == markEnd[index] )
{
Expand Down
Loading