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

Correcting BoundArtifact comparison and eliminating incrementSegment #771

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/it/it-use-latest-versions-011/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
<version>@project.version@</version>
<configuration>
<processParent>true</processParent>
<allowMajorUpdates>false</allowMajorUpdates>
<allowMinorUpdates>true</allowMinorUpdates>
<allowIncrementalUpdates>false</allowIncrementalUpdates>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ protected AbstractVersionsDisplayMojo( RepositorySystem repositorySystem,
super( repositorySystem, projectBuilder, artifactMetadataSource, wagonManager, artifactResolver );
}

@SuppressWarnings( "unchecked" )
protected void logInit()
{
if ( outputFile != null && !outputFileError )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@
import org.codehaus.mojo.versions.api.ArtifactVersions;
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.BoundArtifactVersion;
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;

/**
* Replaces any release versions with the latest snapshot version (if it has been deployed).
Expand Down Expand Up @@ -191,65 +190,58 @@ private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection<Depen
getLog().info( "Ignoring " + toString( dep ) + " as the version number is too short" );
continue;
}
try
{
ArtifactVersion upperBound = unchangedSegment.isPresent()
&& unchangedSegment.get().value() >= MAJOR.value()
? versionComparator.incrementSegment( lowerBound, unchangedSegment.get() )
: null;
getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) );
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );
ArtifactVersion[] newer = versions.getVersions( restriction, true );
getLog().debug( "Candidate versions " + Arrays.asList( newer ) );
ArtifactVersion upperBound = unchangedSegment
.map( s ->
(ArtifactVersion) new BoundArtifactVersion( lowerBound, Segment.of( s.value() + 1 ) ) )
.orElse( null );

getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) );
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );
ArtifactVersion[] newer = versions.getVersions( restriction, true );
getLog().debug( "Candidate versions " + Arrays.asList( newer ) );

// TODO consider creating a search + filter in the Details services to get latest snapshot.
String latestVersion;
ArrayList<ArtifactVersion> snapshotsOnly = new ArrayList<>();
// TODO consider creating a search + filter in the Details services to get latest snapshot.
String latestVersion;
ArrayList<ArtifactVersion> snapshotsOnly = new ArrayList<>();

for ( ArtifactVersion artifactVersion : newer )
for ( ArtifactVersion artifactVersion : newer )
{
String newVersion = artifactVersion.toString();
if ( matchSnapshotRegex.matcher( newVersion ).matches() )
{
String newVersion = artifactVersion.toString();
if ( matchSnapshotRegex.matcher( newVersion ).matches() )
{
snapshotsOnly.add( artifactVersion );
}
snapshotsOnly.add( artifactVersion );
}
ArtifactVersion[] filteredVersions = snapshotsOnly.toArray(
new ArtifactVersion[snapshotsOnly.size()] );
if ( filteredVersions.length > 0 )
}
ArtifactVersion[] filteredVersions = snapshotsOnly.toArray(
new ArtifactVersion[snapshotsOnly.size()] );
if ( filteredVersions.length > 0 )
{
latestVersion = filteredVersions[filteredVersions.length - 1].toString();
if ( getProject().getParent() != null )
{
latestVersion = filteredVersions[filteredVersions.length - 1].toString();
if ( getProject().getParent() != null )
final Artifact parentArtifact = getProject().getParentArtifact();
if ( artifact.getId().equals( parentArtifact.getId() ) && isProcessingParent() )
{
final Artifact parentArtifact = getProject().getParentArtifact();
if ( artifact.getId().equals( parentArtifact.getId() ) && isProcessingParent() )
if ( PomHelper.setProjectParentVersion( pom, latestVersion ) )
{
if ( PomHelper.setProjectParentVersion( pom, latestVersion ) )
{
getLog().debug( "Made parent update from " + version + " to " + latestVersion );
getLog().debug( "Made parent update from " + version + " to " + latestVersion );

this.getChangeRecorder()
.recordUpdate( "useLatestSnapshots", parentArtifact.getGroupId(),
parentArtifact.getArtifactId(), version, latestVersion );
}
this.getChangeRecorder()
.recordUpdate( "useLatestSnapshots", parentArtifact.getGroupId(),
parentArtifact.getArtifactId(), version, latestVersion );
}
}
}

if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
latestVersion, getProject().getModel() ) )
{
getLog().info( "Updated " + toString( dep ) + " to version " + latestVersion );
if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
latestVersion, getProject().getModel() ) )
{
getLog().info( "Updated " + toString( dep ) + " to version " + latestVersion );

this.getChangeRecorder().recordUpdate( "useLatestSnapshots", dep.getGroupId(),
dep.getArtifactId(), version, latestVersion );
}
this.getChangeRecorder().recordUpdate( "useLatestSnapshots", dep.getGroupId(),
dep.getArtifactId(), version, latestVersion );
}
}
catch ( InvalidSegmentException e )
{
getLog().warn( String.format( "Skipping the processing of %s:%s:%s due to: %s", dep.getGroupId(),
dep.getArtifactId(), dep.getVersion(), e.getMessage() ) );
}
}
}
}
Expand Down
50 changes: 21 additions & 29 deletions src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@
import org.codehaus.mojo.versions.api.ArtifactVersions;
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.BoundArtifactVersion;
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;

/**
* Replaces any release versions with the next snapshot version (if it has been deployed).
*
Expand Down Expand Up @@ -178,38 +176,32 @@ private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection<Depende
getLog().info( "Ignoring " + toString( dep ) + " as the version number is too short" );
continue;
}
try

ArtifactVersion upperBound = unchangedSegment
.map( s ->
(ArtifactVersion) new BoundArtifactVersion( lowerBound, Segment.of( s.value() + 1 ) ) )
.orElse( null );

getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) );
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 )
{
ArtifactVersion upperBound = unchangedSegment.isPresent()
&& unchangedSegment.get().value() >= MAJOR.value()
? versionComparator.incrementSegment( lowerBound, unchangedSegment.get() )
: null;
getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) );
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 )
String newVersion = artifactVersion.toString();
if ( matchSnapshotRegex.matcher( newVersion ).matches() )
{
String newVersion = artifactVersion.toString();
if ( matchSnapshotRegex.matcher( newVersion ).matches() )
if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
newVersion, getProject().getModel() ) )
{
if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
newVersion, getProject().getModel() ) )
{
getLog().info( "Updated " + toString( dep ) + " to version " + newVersion );

this.getChangeRecorder().recordUpdate( "useNextSnapshots", dep.getGroupId(),
dep.getArtifactId(), version, newVersion );
}
break;
getLog().info( "Updated " + toString( dep ) + " to version " + newVersion );

this.getChangeRecorder().recordUpdate( "useNextSnapshots", dep.getGroupId(),
dep.getArtifactId(), version, newVersion );
}
break;
}
}
catch ( InvalidSegmentException e )
{
getLog().warn( String.format( "Skipping the processing of %s:%s:%s due to: %s", dep.getGroupId(),
dep.getArtifactId(), dep.getVersion(), e.getMessage() ) );
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,18 @@ public final ArtifactVersion[] getNewerVersions( String version, Optional<Segmen
}

@Override
public final ArtifactVersion[] getNewerVersions( String versionString, Optional<Segment> upperBoundSegment,
public final ArtifactVersion[] getNewerVersions( String versionString, Optional<Segment> unchangedSegment,
boolean includeSnapshots, boolean allowDowngrade )
throws InvalidSegmentException
{
ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString );
ArtifactVersion lowerBound = allowDowngrade
? getLowerBound( currentVersion, upperBoundSegment )
? getLowerBound( currentVersion, unchangedSegment )
.map( DefaultArtifactVersion::new )
.orElse( null )
: currentVersion;
ArtifactVersion upperBound =
!upperBoundSegment.isPresent()
? null
: upperBoundSegment
unchangedSegment
.map( s -> (ArtifactVersion) new BoundArtifactVersion( currentVersion,
s.isMajorTo( SUBINCREMENTAL )
? Segment.of( s.value() + 1 )
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/org/codehaus/mojo/versions/api/ArtifactVersions.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.versioning.ArtifactVersion;
Expand Down Expand Up @@ -112,6 +114,38 @@ public int compareTo( ArtifactVersions that )
: compare( getVersion(), that.getVersion() );
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}

if ( !( o instanceof ArtifactVersions ) )
{
return false;
}

ArtifactVersions that = (ArtifactVersions) o;

return new EqualsBuilder()
.append( getArtifact(), that.getArtifact() )
.append( getVersions(), that.getVersions() )
.append( getVersionComparator(), that.getVersionComparator() )
.isEquals();
}

@Override
public int hashCode()
{
return new HashCodeBuilder( 17, 37 )
.append( getArtifact() )
.append( getVersions() )
.append( getVersionComparator() )
.toHashCode();
}

/**
* Checks if the version is in the range (and ensures that the range respects the <code>-!</code> syntax to rule out
* any qualifiers from range boundaries).
Expand Down
43 changes: 0 additions & 43 deletions src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,21 +432,6 @@ else if ( getVersionComparator().compare( result, fromReactor ) < 0 )
return result;
}

private ArtifactVersion getNewestVersion( String currentVersion, VersionsHelper helper,
Optional<Segment> unchangedSegment,
boolean includeSnapshots, VersionRange range )
throws InvalidSegmentException
{
ArtifactVersion lowerBound = helper.createArtifactVersion( currentVersion );
ArtifactVersion upperBound = null;
if ( unchangedSegment.isPresent() )
{
upperBound = getVersionComparator().incrementSegment( lowerBound, unchangedSegment.get() );
}
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );
return getNewestVersion( range, restriction, includeSnapshots );
}

private final class PropertyVersionComparator implements VersionComparator
{
public int compare( ArtifactVersion v1, ArtifactVersion v2 )
Expand Down Expand Up @@ -507,33 +492,5 @@ public int getSegmentCount( ArtifactVersion v )
}
return result;
}

public ArtifactVersion incrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException
{
if ( !isAssociated() )
{
throw new IllegalStateException( "Cannot compare versions for a property with no associations" );
}
VersionComparator[] comparators = lookupComparators();
assert comparators.length >= 1 : "we have at least one association => at least one comparator";
ArtifactVersion result = comparators[0].incrementSegment( v, segment );
for ( int i = 1; i < comparators.length; i++ )
{
ArtifactVersion alt = comparators[i].incrementSegment( v, segment );
if ( !result.toString().equals( alt.toString() ) )
{
throw new IllegalStateException(
"Property " + name + " is associated with multiple artifacts"
+ " and these artifacts use different version sorting rules and these rules are effectively"
+ " incompatible for the two of versions being compared.\n"
+ "First rule says incrementSegment(\""
+ v + "\", " + segment + ") = " + result
+ "\nSecond rule says incrementSegment(\"" + v + "\", "
+ segment + ") = " + alt );
}
}
return result;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.codehaus.mojo.versions.api.Segment;

/**
* Base class for version comparators.
Expand Down Expand Up @@ -54,18 +53,6 @@ public final int getSegmentCount( ArtifactVersion v )

protected abstract int innerGetSegmentCount( ArtifactVersion v );

/**
* {@inheritDoc}
*/
public final ArtifactVersion incrementSegment( ArtifactVersion v, Segment segment ) throws InvalidSegmentException
{
return VersionComparators.copySnapshot( v, innerIncrementSegment( VersionComparators.stripSnapshot( v ),
segment ) );
}

protected abstract ArtifactVersion innerIncrementSegment( ArtifactVersion v, Segment segment )
throws InvalidSegmentException;

/**
* Returns a hash code value for the comparator class.
*
Expand Down