Skip to content

Commit 45615de

Browse files
committed
#454: Introducing "infinity-like" calculus for dependency range computation
1 parent e7aa406 commit 45615de

25 files changed

+614
-166
lines changed

src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public abstract class AbstractVersionsDependencyUpdaterMojo
6060

6161
/**
6262
* A comma separated list of artifact patterns to include. Follows the pattern
63-
* "groupId:artifactId:type:classifier:version". Designed to allow specifing the set of includes from the command
63+
* "groupId:artifactId:type:classifier:version". Designed to allow specifying the set of includes from the command
6464
* line. When specifying includes from the pom, use the {@link #includes} configuration instead. If this property is
6565
* specified then the {@link # include} configuration is ignored.
6666
*
@@ -71,7 +71,7 @@ public abstract class AbstractVersionsDependencyUpdaterMojo
7171

7272
/**
7373
* A comma separated list of artifact patterns to exclude. Follows the pattern
74-
* "groupId:artifactId:type:classifier:version". Designed to allow specifing the set of excludes from the command
74+
* "groupId:artifactId:type:classifier:version". Designed to allow specifying the set of excludes from the command
7575
* line. When specifying excludes from the pom, use the {@link #excludes} configuration instead. If this property is
7676
* specified then the {@link # exclude} configuration is ignored.
7777
*

src/main/java/org/codehaus/mojo/versions/AbstractVersionsReportRenderer.java

+17-34
Original file line numberDiff line numberDiff line change
@@ -780,40 +780,23 @@ private Set<String> getVersionsInRange( Property property, PropertyVersions vers
780780

781781
protected String getLabel( ArtifactVersion version, AbstractVersionDetails versions )
782782
{
783-
String label = null;
784-
if ( equals( version, versions.getNewestUpdate( of( MAJOR ) ) ) )
785-
{
786-
label = getText( "report.latestMajor" );
787-
}
788-
else if ( equals( version, versions.getOldestUpdate( of( MAJOR ) ) ) )
789-
{
790-
label = getText( "report.nextMajor" );
791-
}
792-
else if ( equals( version, versions.getNewestUpdate( of( MINOR ) ) ) )
793-
{
794-
label = getText( "report.latestMinor" );
795-
}
796-
else if ( equals( version, versions.getOldestUpdate( of( MINOR ) ) ) )
797-
{
798-
label = getText( "report.nextMinor" );
799-
}
800-
else if ( equals( version, versions.getNewestUpdate( of( INCREMENTAL ) ) ) )
801-
{
802-
label = getText( "report.latestIncremental" );
803-
}
804-
else if ( equals( version, versions.getOldestUpdate( of( INCREMENTAL ) ) ) )
805-
{
806-
label = getText( "report.nextIncremental" );
807-
}
808-
else if ( equals( version, versions.getNewestUpdate( of( SUBINCREMENTAL ) ) ) )
809-
{
810-
label = getText( "report.latestSubIncremental" );
811-
}
812-
else if ( equals( version, versions.getOldestUpdate( of( SUBINCREMENTAL ) ) ) )
813-
{
814-
label = getText( "report.nextVersion" );
815-
}
816-
return label;
783+
return equals( version, versions.getNewestUpdate( of( SUBINCREMENTAL ) ) )
784+
? getText( "report.latestSubIncremental" )
785+
: equals( version, versions.getOldestUpdate( of( SUBINCREMENTAL ) ) )
786+
? getText( "report.nextVersion" )
787+
: equals( version, versions.getOldestUpdate( of( INCREMENTAL ) ) )
788+
? getText( "report.nextIncremental" )
789+
: equals( version, versions.getNewestUpdate( of( INCREMENTAL ) ) )
790+
? getText( "report.latestIncremental" )
791+
: equals( version, versions.getOldestUpdate( of( MINOR ) ) )
792+
? getText( "report.nextMinor" )
793+
: equals( version, versions.getNewestUpdate( of( MINOR ) ) )
794+
? getText( "report.latestMinor" )
795+
: equals( version, versions.getOldestUpdate( of( MAJOR ) ) )
796+
? getText( "report.nextMajor" )
797+
: equals( version, versions.getNewestUpdate( of( MAJOR ) ) )
798+
? getText( "report.latestMajor" )
799+
: "";
817800
}
818801

819802
}

src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange ver
356356
* @param versionRange The version range.
357357
* @param allowingSnapshots <code>null</code> for no override, otherwise the local override to apply.
358358
* @param usePluginRepositories Use plugin repositories
359+
* @param allowDowngrade whether downgrades should be allowed
359360
* @return The latest version of the specified artifact that matches the specified version range or
360361
* <code>null</code> if no matching version could be found.
361362
* @throws ArtifactMetadataRetrievalException If the artifact metadata could not be found.
@@ -515,6 +516,7 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A
515516
* @param artifact The artifact.
516517
* @param currentVersion The current version of the artifact.
517518
* @param updateVersion The proposed new version of the artifact.
519+
* @param forceUpdate if true, LATEST and RELEASE versions will be overwritten with the real version
518520
* @return <code>true</code> if the update should be applied to the pom.
519521
* @since 2.9
520522
*/
@@ -575,7 +577,7 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A
575577
* @param allowMajorUpdates Allow major updates
576578
* @param allowMinorUpdates Allow minor updates
577579
* @param allowIncrementalUpdates Allow incremental updates
578-
* @return Returns the segment (0-based) that is unchangable. If any segment can change, returns -1.
580+
* @return Returns the segment (0-based) that is unchangeable. If any segment can change, returns -1.
579581
*/
580582
protected Optional<Segment> determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates,
581583
boolean allowIncrementalUpdates )

src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class DependencyUpdatesReport extends AbstractVersionsReport
6565
protected boolean processDependencyManagement;
6666

6767
/**
68-
* Whether to process the depdendencyManagement part transitive or not.
68+
* Whether to process the dependencyManagement part transitive or not.
6969
* In case of <code>&lt;type&gt;pom&lt;/type&gt;</code>and
7070
* <code>&lt;scope&gt;import&lt;/scope&gt;</code> this means
7171
* by default to report also the imported dependencies.

src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class DisplayDependencyUpdatesMojo
9494
private boolean processDependencyManagement;
9595

9696
/**
97-
* Whether to process the depdendencyManagement part transitive or not.
97+
* Whether to process the dependencyManagement part transitive or not.
9898
* In case of <code>&lt;type&gt;pom&lt;/type&gt;</code>and
9999
* <code>&lt;scope&gt;import&lt;/scope&gt;</code> this means
100100
* by default to report also the imported dependencies.
@@ -691,12 +691,19 @@ private DependencyManagement getProjectDependencyManagement( MavenProject projec
691691

692692
private Optional<Segment> calculateUpdateScope()
693693
{
694-
return !allowAnyUpdates
695-
? allowMajorUpdates ? of( MAJOR )
696-
: allowMinorUpdates ? of( MINOR )
697-
: allowIncrementalUpdates ? of( INCREMENTAL )
698-
: empty()
699-
: empty();
694+
if ( !allowIncrementalUpdates && !allowMinorUpdates && !allowMajorUpdates && !allowAnyUpdates )
695+
{
696+
throw new IllegalArgumentException( "One of: allowAnyUpdates, allowMajorUpdates, allowMinorUpdates, "
697+
+ "allowIncrementalUpdates must be true" );
698+
}
699+
700+
return allowAnyUpdates && allowMajorUpdates && allowMinorUpdates
701+
? empty()
702+
: allowMajorUpdates && allowMinorUpdates
703+
? of( MAJOR )
704+
: allowMinorUpdates
705+
? of( MINOR )
706+
: of( INCREMENTAL );
700707
}
701708

702709
private void logUpdates( Map<Dependency, ArtifactVersions> updates, String section )

src/main/java/org/codehaus/mojo/versions/SetMojo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,10 @@ public void execute() throws MojoExecutionException, MojoFailureException
402402

403403
/**
404404
* Returns the incremented version, with the nextSnapshotIndexToIncrement indicating the 1-based index,
405-
* conunting from the left, or the most major version component, of the version string.
405+
* from the left, or the most major version component, of the version string.
406406
*
407407
* @param version input version
408+
* @param nextSnapshotIndexToIncrement 1-based segment number to be incremented
408409
* @return version with the incremented index specified by nextSnapshotIndexToIncrement or last index
409410
* @throws MojoExecutionException thrown if the input parameters are invalid
410411
*/

src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java

+23-52
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
3232
import org.apache.maven.artifact.versioning.Restriction;
3333
import org.apache.maven.artifact.versioning.VersionRange;
34+
import org.codehaus.mojo.versions.ordering.BoundArtifactVersion;
3435
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
3536
import org.codehaus.mojo.versions.ordering.VersionComparator;
3637

3738
import static java.util.Optional.empty;
3839
import static java.util.Optional.of;
40+
import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL;
3941

4042
/**
4143
* Base class for {@link org.codehaus.mojo.versions.api.VersionDetails}.
@@ -171,7 +173,6 @@ private static <T> Iterable<T> reverse( T[] array )
171173
public final ArtifactVersion getNewestVersion( VersionRange versionRange, Restriction restriction,
172174
boolean includeSnapshots, boolean allowDowngrade )
173175
{
174-
final VersionComparator versionComparator = getVersionComparator();
175176
// reverse( getVersions( ... ) ) will contain versions sorted from latest to oldest,
176177
// so we only need to find the first candidate fulfilling the criteria
177178
for ( ArtifactVersion candidate : reverse( getVersions( includeSnapshots ) ) )
@@ -260,13 +261,22 @@ public final ArtifactVersion[] getNewerVersions( String versionString, Optional<
260261
throws InvalidSegmentException
261262
{
262263
ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString );
263-
ArtifactVersion lowerBound =
264-
allowDowngrade ? getLowerBoundArtifactVersion( currentVersion, upperBoundSegment ) : currentVersion;
265-
ArtifactVersion upperBound = !upperBoundSegment.isPresent() ? null
266-
: getVersionComparator().incrementSegment( lowerBound, upperBoundSegment.get() );
264+
ArtifactVersion lowerBound = allowDowngrade
265+
? getLowerBound( currentVersion, upperBoundSegment )
266+
.map( DefaultArtifactVersion::new )
267+
.orElse( null )
268+
: currentVersion;
269+
ArtifactVersion upperBound =
270+
!upperBoundSegment.isPresent()
271+
? null
272+
: upperBoundSegment
273+
.map( s -> (ArtifactVersion) new BoundArtifactVersion( currentVersion,
274+
s.isMajorTo( SUBINCREMENTAL )
275+
? Segment.of( s.value() + 1 )
276+
: s ) )
277+
.orElse( null );
267278

268279
Restriction restriction = new Restriction( lowerBound, allowDowngrade, upperBound, allowDowngrade );
269-
// TODO shouldn't allowDowngrade boolean be passed to this call ?
270280
return getVersions( restriction, includeSnapshots );
271281
}
272282

@@ -346,7 +356,8 @@ public final ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, Op
346356
{
347357
try
348358
{
349-
return getOldestVersion( restrictionFor( currentVersion, updateScope ), includeSnapshots );
359+
return getOldestVersion( getVersionComparator().restrictionFor( currentVersion, updateScope ),
360+
includeSnapshots );
350361
}
351362
catch ( InvalidSegmentException e )
352363
{
@@ -359,7 +370,8 @@ public final ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, Op
359370
{
360371
try
361372
{
362-
return getNewestVersion( restrictionFor( currentVersion, updateScope ), includeSnapshots );
373+
return getNewestVersion( getVersionComparator().restrictionFor( currentVersion, updateScope ),
374+
includeSnapshots );
363375
}
364376
catch ( InvalidSegmentException e )
365377
{
@@ -372,7 +384,8 @@ public final ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, Op
372384
{
373385
try
374386
{
375-
return getVersions( restrictionFor( currentVersion, updateScope ), includeSnapshots );
387+
return getVersions( getVersionComparator().restrictionFor( currentVersion, updateScope ),
388+
includeSnapshots );
376389
}
377390
catch ( InvalidSegmentException e )
378391
{
@@ -433,32 +446,12 @@ public ArtifactVersion[] getAllUpdates( VersionRange versionRange, boolean inclu
433446
return getVersions( versionRange, restriction, includeSnapshots );
434447
}
435448

436-
/**
437-
* Returns the lower bound version based on the given artifact version
438-
* and the lowest unchanged segment index (0-based); -1 means that the whole version string can be changed,
439-
* implying that there is also no string designation of the lower bound version.
440-
*
441-
* @param version {@link ArtifactVersion} object specyfing the verion for which the lower bound is being computed
442-
* @param unchangedSegment first segment not to be changed; empty() means any segment can change
443-
* @return {@link ArtifactVersion} the lowest artifact version with the given segment held or null if no such
444-
* version can be found
445-
* @throws InvalidSegmentException if the requested segment is outside the bounds (less than 1 or greater than
446-
* the segment count)
447-
*/
448-
protected ArtifactVersion getLowerBoundArtifactVersion( ArtifactVersion version,
449-
Optional<Segment> unchangedSegment )
450-
throws InvalidSegmentException
451-
{
452-
Optional<String> lowerBound = getLowerBound( version, unchangedSegment );
453-
return lowerBound.map( DefaultArtifactVersion::new ).orElse( null );
454-
}
455-
456449
/**
457450
* Returns the string designation of the lower bound version based on the given artifact version
458451
* and the lowest unchanged segment index (0-based); -1 means that the whole version string can be changed,
459452
* implying that there is also no string designation of the lower bound version.
460453
*
461-
* @param version {@link ArtifactVersion} object specyfing the verion for which the lower bound is being computed
454+
* @param version {@link ArtifactVersion} object specifying the version for which the lower bound is being computed
462455
* @param unchangedSegment first segment not to be changed; empty() means anything can change
463456
* @return {@link Optional} string containing the lowest artifact version with the given segment held
464457
* @throws InvalidSegmentException if the requested segment is outside of the bounds (less than 1 or greater than
@@ -527,26 +520,4 @@ private boolean isVersionInRestriction( Restriction restriction, ArtifactVersion
527520
}
528521
return ( includeLower || lower != 0 ) && ( includeUpper || upper != 0 );
529522
}
530-
531-
/**
532-
* Helper method to get the artifact boundaries for computing updates
533-
*
534-
* @param currentVersion The current version.
535-
* @param scope scope of the restriction or Optional.empty() for no restriction
536-
* @return {@linkplain Restriction} object based on the arguments
537-
* @throws InvalidSegmentException if {@code segment} ∉ [0, segmentCount)
538-
*/
539-
protected Restriction restrictionFor( ArtifactVersion currentVersion, Optional<Segment> scope )
540-
throws InvalidSegmentException
541-
{
542-
VersionComparator versionComparator = getVersionComparator();
543-
ArtifactVersion lowerBound = scope.isPresent() && scope.get().value() < Segment.SUBINCREMENTAL.value()
544-
? versionComparator.incrementSegment( currentVersion, scope.get() )
545-
: currentVersion;
546-
ArtifactVersion upperBound = scope.isPresent() && scope.get().value() > Segment.MAJOR.value()
547-
? versionComparator.incrementSegment( currentVersion, Segment.of( scope.get().value() - 1 ) )
548-
: null;
549-
return new Restriction( lowerBound, lowerBound != currentVersion,
550-
upperBound, false );
551-
}
552523
}

src/main/java/org/codehaus/mojo/versions/api/PomHelper.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1476,9 +1476,10 @@ public static String getGroupId( Model model )
14761476
* Finds the local root of the specified project.
14771477
*
14781478
* @param project The project to find the local root for.
1479+
* @param builder {@linkplain MavenProjectBuilder} object
14791480
* @param localRepository the local repo.
14801481
* @param globalProfileManager the global profile manager.
1481-
* @param logger The logger to log to.
1482+
* @param logger The logger to log tog
14821483
* @return The local root (note this may be the project passed as an argument).
14831484
*/
14841485
public static MavenProject getLocalRoot( MavenProjectBuilder builder, MavenProject project,

src/main/java/org/codehaus/mojo/versions/api/Segment.java

+12-9
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@
2626
*/
2727
public enum Segment implements Comparable<Segment>
2828
{
29-
MAJOR( 0 ), MINOR( 1 ), INCREMENTAL( 2 ), SUBINCREMENTAL( 3 );
30-
31-
private final int index;
32-
33-
Segment( int index )
34-
{
35-
this.index = index;
36-
}
29+
MAJOR, MINOR, INCREMENTAL, SUBINCREMENTAL;
3730

3831
/**
3932
* Returns the 0-based sendex index
@@ -42,7 +35,7 @@ public enum Segment implements Comparable<Segment>
4235
*/
4336
public int value()
4437
{
45-
return index;
38+
return ordinal();
4639
}
4740

4841
public static Segment of( int index )
@@ -58,6 +51,16 @@ public static Segment of( int index )
5851
}
5952
}
6053

54+
/**
55+
* Returns true if the given segment is more major than the other
56+
* @param other other segment to compare
57+
* @return true if the given segment is more major
58+
*/
59+
public boolean isMajorTo( Segment other )
60+
{
61+
return value() < other.value();
62+
}
63+
6164
@Override
6265
public String toString()
6366
{

0 commit comments

Comments
 (0)