Skip to content

Commit d421b45

Browse files
andrzejj0slawekjaranowski
authored andcommitted
Resolves #776: onlyUpgradable change the filter to versions where the current version is not the latest one
1 parent 8ee15b7 commit d421b45

File tree

7 files changed

+143
-170
lines changed

7 files changed

+143
-170
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@
2525
import java.nio.file.Files;
2626
import java.nio.file.Path;
2727
import java.nio.file.Paths;
28+
import java.util.Arrays;
2829
import java.util.Locale;
2930
import java.util.Map;
3031
import java.util.Set;
3132
import java.util.TreeSet;
33+
import java.util.stream.Collectors;
3234

3335
import org.apache.maven.artifact.manager.WagonManager;
3436
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
3537
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
3638
import org.apache.maven.artifact.resolver.ArtifactResolver;
39+
import org.apache.maven.artifact.versioning.ArtifactVersion;
3740
import org.apache.maven.doxia.sink.Sink;
3841
import org.apache.maven.model.Dependency;
3942
import org.apache.maven.plugins.annotations.Mojo;
@@ -202,8 +205,21 @@ && getProject().getOriginalModel().getDependencyManagement().getDependencies() !
202205

203206
if ( onlyUpgradable )
204207
{
205-
dependencyUpdates = filter( dependencyUpdates, e -> e.getVersions().length > 1 );
206-
dependencyManagementUpdates = filter( dependencyManagementUpdates, e -> e.getVersions().length > 1 );
208+
dependencyUpdates = filter( dependencyUpdates, e -> e.getVersions().length > 0 );
209+
dependencyManagementUpdates = filter( dependencyManagementUpdates, e -> e.getVersions().length > 0 );
210+
}
211+
212+
if ( getLog().isDebugEnabled() )
213+
{
214+
getLog().debug( "Dependency versions:" );
215+
dependencyUpdates.forEach( ( key, value ) -> getLog().debug( key.toString() + ": "
216+
+ Arrays.stream( value.getVersions() ).map( ArtifactVersion::toString )
217+
.collect( Collectors.joining( ", " ) ) ) );
218+
219+
getLog().debug( "Dependency management versions:" );
220+
dependencyManagementUpdates.forEach( ( key, value ) -> getLog().debug( key.toString() + ": "
221+
+ Arrays.stream( value.getVersions() ).map( ArtifactVersion::toString )
222+
.collect( Collectors.joining( ", " ) ) ) );
207223
}
208224

209225
for ( String format : formats )

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,8 @@ protected void doGenerateReport( Locale locale, Sink sink ) throws MavenReportEx
163163

164164
if ( onlyUpgradable )
165165
{
166-
pluginUpdates =
167-
filter( pluginUpdates, plugin -> plugin.getVersions().length > 1 );
168-
pluginManagementUpdates = filter( pluginManagementUpdates,
169-
plugin -> plugin.getVersions().length > 1 );
166+
pluginUpdates = filter( pluginUpdates, p -> p.getVersions().length > 0 );
167+
pluginManagementUpdates = filter( pluginManagementUpdates, p -> p.getVersions().length > 0 );
170168
}
171169

172170
PluginUpdatesModel model = new PluginUpdatesModel( pluginUpdates, pluginManagementUpdates );

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ public final ArtifactVersion[] getAllUpdates( Optional<Segment> updateScope, boo
339339
return null;
340340
}
341341

342+
@Override
343+
public final ArtifactVersion[] getAllUpdates()
344+
{
345+
return getAllUpdates( (VersionRange) null, isIncludeSnapshots() );
346+
}
347+
342348
@Override
343349
public final ArtifactVersion[] getAllUpdates( VersionRange versionRange )
344350
{

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

Lines changed: 53 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@
3838
import java.util.Set;
3939
import java.util.TreeMap;
4040
import java.util.TreeSet;
41-
import java.util.concurrent.Callable;
4241
import java.util.concurrent.ExecutionException;
4342
import java.util.concurrent.ExecutorService;
4443
import java.util.concurrent.Executors;
4544
import java.util.concurrent.Future;
4645
import java.util.regex.Pattern;
4746
import java.util.stream.Collectors;
4847

48+
import org.apache.commons.lang3.tuple.ImmutablePair;
49+
import org.apache.commons.lang3.tuple.Pair;
4950
import org.apache.maven.artifact.Artifact;
5051
import org.apache.maven.artifact.ArtifactUtils;
5152
import org.apache.maven.artifact.manager.WagonManager;
@@ -668,109 +669,95 @@ public ArtifactVersion createArtifactVersion( String version )
668669
return new DefaultArtifactVersion( version );
669670
}
670671

672+
/**
673+
* Returns a map of all possible updates per dependency. The lookup is done in parallel using
674+
* {@code LOOKUP_PARALLEL_THREADS} threads.
675+
*
676+
* @param dependencies The set of {@link Dependency} instances to look up.
677+
* @param usePluginRepositories Search the plugin repositories.
678+
* @return map containing the ArtifactVersions object per dependency
679+
* @throws ArtifactMetadataRetrievalException if the lookup does not succeed
680+
*/
671681
@Override
672682
public Map<Dependency, ArtifactVersions> lookupDependenciesUpdates( Set<Dependency> dependencies,
673683
boolean usePluginRepositories )
674684
throws ArtifactMetadataRetrievalException
675685
{
676-
// Create the request for details collection for parallel lookup...
677-
final List<Callable<DependencyArtifactVersions>> requestsForDetails =
678-
new ArrayList<>( dependencies.size() );
679-
for ( final Dependency dependency : dependencies )
680-
{
681-
requestsForDetails.add( new DependencyLookup( dependency, usePluginRepositories ) );
682-
}
683-
684-
final Map<Dependency, ArtifactVersions> dependencyUpdates = new TreeMap<>( DependencyComparator.INSTANCE );
685-
686-
// Lookup details in parallel...
687-
final ExecutorService executor = Executors.newFixedThreadPool( LOOKUP_PARALLEL_THREADS );
686+
ExecutorService executor = Executors.newFixedThreadPool( LOOKUP_PARALLEL_THREADS );
688687
try
689688
{
690-
final List<Future<DependencyArtifactVersions>> responseForDetails =
691-
executor.invokeAll( requestsForDetails );
692-
693-
// Construct the final results...
694-
for ( final Future<DependencyArtifactVersions> details : responseForDetails )
689+
Map<Dependency, ArtifactVersions> dependencyUpdates = new TreeMap<>( DependencyComparator.INSTANCE );
690+
List<Future<? extends Pair<Dependency, ArtifactVersions>>> futures = dependencies.stream()
691+
.map( dependency -> executor.submit( () -> new ImmutablePair<>
692+
( dependency, lookupDependencyUpdates( dependency, usePluginRepositories ) ) ) )
693+
.collect( Collectors.toList() );
694+
for ( Future<? extends Pair<Dependency, ArtifactVersions>> details : futures )
695695
{
696-
final DependencyArtifactVersions dav = details.get();
697-
dependencyUpdates.put( dav.getDependency(), dav.getArtifactVersions() );
696+
Pair<Dependency, ArtifactVersions> pair = details.get();
697+
dependencyUpdates.put( pair.getKey(), pair.getValue() );
698698
}
699+
700+
return dependencyUpdates;
699701
}
700702
catch ( ExecutionException | InterruptedException ie )
701703
{
702704
throw new ArtifactMetadataRetrievalException( "Unable to acquire metadata for dependencies " + dependencies
703-
+ ": " + ie.getMessage(), ie, null );
705+
+ ": " + ie.getMessage(), ie, null );
704706
}
705707
finally
706708
{
707-
executor.shutdownNow();
709+
executor.shutdown();
708710
}
709-
return dependencyUpdates;
710711
}
711712

712713
@Override
713714
public ArtifactVersions lookupDependencyUpdates( Dependency dependency, boolean usePluginRepositories )
714715
throws ArtifactMetadataRetrievalException
715716
{
716-
getLog().debug( "Checking "
717-
+ ArtifactUtils.versionlessKey( dependency.getGroupId(), dependency.getArtifactId() )
718-
+ " for updates newer than " + dependency.getVersion() );
719-
720-
return lookupArtifactVersions( createDependencyArtifact( dependency ), usePluginRepositories );
717+
ArtifactVersions allVersions = lookupArtifactVersions( createDependencyArtifact( dependency ),
718+
usePluginRepositories );
719+
return new ArtifactVersions( allVersions.getArtifact(), Arrays.stream( allVersions.getAllUpdates() )
720+
.collect( Collectors.toList() ), allVersions.getVersionComparator() );
721721
}
722722

723723
@Override
724724
public Map<Plugin, PluginUpdatesDetails> lookupPluginsUpdates( Set<Plugin> plugins, boolean allowSnapshots )
725725
throws ArtifactMetadataRetrievalException
726726
{
727-
// Create the request for details collection for parallel lookup...
728-
List<Callable<PluginPluginUpdatesDetails>> requestsForDetails = new ArrayList<>( plugins.size() );
729-
for ( final Plugin plugin : plugins )
730-
{
731-
requestsForDetails.add( new PluginLookup( plugin, allowSnapshots ) );
732-
}
733-
734-
Map<Plugin, PluginUpdatesDetails> pluginUpdates = new TreeMap<>( PluginComparator.INSTANCE );
735-
736-
// Lookup details in parallel...
737727
ExecutorService executor = Executors.newFixedThreadPool( LOOKUP_PARALLEL_THREADS );
738728
try
739729
{
740-
final List<Future<PluginPluginUpdatesDetails>> responseForDetails =
741-
executor.invokeAll( requestsForDetails );
742-
743-
// Construct the final results...
744-
for ( final Future<PluginPluginUpdatesDetails> details : responseForDetails )
730+
Map<Plugin, PluginUpdatesDetails> pluginUpdates = new TreeMap<>( PluginComparator.INSTANCE );
731+
List<Future<? extends Pair<Plugin, PluginUpdatesDetails>>> futures = plugins.stream()
732+
.map( p -> executor.submit( () -> new ImmutablePair<>
733+
( p, lookupPluginUpdates( p, allowSnapshots ) ) ) )
734+
.collect( Collectors.toList() );
735+
for ( Future<? extends Pair<Plugin, PluginUpdatesDetails>> details : futures )
745736
{
746-
final PluginPluginUpdatesDetails pud = details.get();
747-
pluginUpdates.put( pud.getPlugin(), pud.getPluginUpdatesDetails() );
737+
Pair<Plugin, PluginUpdatesDetails> pair = details.get();
738+
pluginUpdates.put( pair.getKey(), pair.getValue() );
748739
}
740+
741+
return pluginUpdates;
749742
}
750743
catch ( ExecutionException | InterruptedException ie )
751744
{
752745
throw new ArtifactMetadataRetrievalException( "Unable to acquire metadata for plugins " + plugins + ": "
753-
+ ie.getMessage(), ie, null );
746+
+ ie.getMessage(), ie, null );
754747
}
755748
finally
756749
{
757-
executor.shutdownNow();
750+
executor.shutdown();
758751
}
759-
return pluginUpdates;
760752
}
761753

762754
@Override
763755
public PluginUpdatesDetails lookupPluginUpdates( Plugin plugin, boolean allowSnapshots )
764756
throws ArtifactMetadataRetrievalException
765757
{
766-
String version = plugin.getVersion();
767-
version = version == null ? "LATEST" : version;
768-
getLog().debug( "Checking " + ArtifactUtils.versionlessKey( plugin.getGroupId(), plugin.getArtifactId() )
769-
+ " for updates newer than " + version );
770-
771-
final ArtifactVersions pluginArtifactVersions =
772-
lookupArtifactVersions( createPluginArtifact( plugin.getGroupId(), plugin.getArtifactId(), version ),
773-
true );
758+
String version = plugin.getVersion() != null
759+
? plugin.getVersion()
760+
: "LATEST";
774761

775762
Set<Dependency> pluginDependencies = new TreeSet<>( DependencyComparator.INSTANCE );
776763
if ( plugin.getDependencies() != null )
@@ -780,7 +767,13 @@ public PluginUpdatesDetails lookupPluginUpdates( Plugin plugin, boolean allowSna
780767
Map<Dependency, ArtifactVersions> pluginDependencyDetails =
781768
lookupDependenciesUpdates( pluginDependencies, false );
782769

783-
return new PluginUpdatesDetails( pluginArtifactVersions, pluginDependencyDetails, allowSnapshots );
770+
ArtifactVersions allVersions =
771+
lookupArtifactVersions( createPluginArtifact( plugin.getGroupId(), plugin.getArtifactId(), version ),
772+
true );
773+
ArtifactVersions updatedVersions = new ArtifactVersions( allVersions.getArtifact(),
774+
Arrays.stream( allVersions.getAllUpdates() ).collect( Collectors.toList() ),
775+
allVersions.getVersionComparator() );
776+
return new PluginUpdatesDetails( updatedVersions, pluginDependencyDetails, allowSnapshots );
784777
}
785778

786779
@Override
@@ -832,8 +825,8 @@ public Map<Property, PropertyVersions> getVersionPropertiesMap( MavenProject pro
832825
}
833826
}
834827

835-
List<String> includePropertiesList = getSplittedProperties( includeProperties );
836-
List<String> excludePropertiesList = getSplittedProperties( excludeProperties );
828+
List<String> includePropertiesList = getSplitProperties( includeProperties );
829+
List<String> excludePropertiesList = getSplitProperties( excludeProperties );
837830

838831
getLog().debug( "Searching for properties associated with builders" );
839832
Iterator<Property> i = properties.values().iterator();
@@ -902,7 +895,7 @@ else if ( !excludePropertiesList.isEmpty() && excludePropertiesList.contains( pr
902895
return propertyVersions;
903896
}
904897

905-
private List<String> getSplittedProperties( String commaSeparatedProperties )
898+
private List<String> getSplitProperties( String commaSeparatedProperties )
906899
{
907900
List<String> propertiesList = Collections.emptyList();
908901
if ( StringUtils.isNotEmpty( commaSeparatedProperties ) )
@@ -913,97 +906,6 @@ private List<String> getSplittedProperties( String commaSeparatedProperties )
913906
return propertiesList;
914907
}
915908

916-
// This is a data container to hold the result of a Dependency lookup to its ArtifactVersions.
917-
private static class DependencyArtifactVersions
918-
{
919-
private final Dependency dependency;
920-
921-
private final ArtifactVersions artifactVersions;
922-
923-
DependencyArtifactVersions( final Dependency dependency, final ArtifactVersions artifactVersions )
924-
{
925-
this.dependency = dependency;
926-
this.artifactVersions = artifactVersions;
927-
}
928-
929-
public Dependency getDependency()
930-
{
931-
return dependency;
932-
}
933-
934-
public ArtifactVersions getArtifactVersions()
935-
{
936-
return artifactVersions;
937-
}
938-
}
939-
940-
// This is a data container to hold the result of a Dependency lookup to its ArtifactVersions.
941-
private static class PluginPluginUpdatesDetails
942-
{
943-
private final Plugin plugin;
944-
945-
private final PluginUpdatesDetails pluginUpdatesDetails;
946-
947-
PluginPluginUpdatesDetails( final Plugin plugin, final PluginUpdatesDetails pluginUpdatesDetails )
948-
{
949-
this.plugin = plugin;
950-
this.pluginUpdatesDetails = pluginUpdatesDetails;
951-
}
952-
953-
public Plugin getPlugin()
954-
{
955-
return plugin;
956-
}
957-
958-
public PluginUpdatesDetails getPluginUpdatesDetails()
959-
{
960-
return pluginUpdatesDetails;
961-
}
962-
}
963-
964-
// This Callable wraps lookupDependencyUpdates so that it can be run in parallel.
965-
private class DependencyLookup
966-
implements Callable<DependencyArtifactVersions>
967-
{
968-
private final Dependency dependency;
969-
970-
private final boolean usePluginRepositories;
971-
972-
DependencyLookup( final Dependency dependency, final boolean usePluginRepositories )
973-
{
974-
this.dependency = dependency;
975-
this.usePluginRepositories = usePluginRepositories;
976-
}
977-
978-
public DependencyArtifactVersions call()
979-
throws Exception
980-
{
981-
return new DependencyArtifactVersions( dependency,
982-
lookupDependencyUpdates( dependency, usePluginRepositories ) );
983-
}
984-
}
985-
986-
// This Callable wraps lookupPluginUpdates so that it can be run in parallel.
987-
private class PluginLookup
988-
implements Callable<PluginPluginUpdatesDetails>
989-
{
990-
private final Plugin plugin;
991-
992-
private final boolean allowSnapshots;
993-
994-
PluginLookup( final Plugin plugin, final Boolean allowSnapshots )
995-
{
996-
this.plugin = plugin;
997-
this.allowSnapshots = allowSnapshots;
998-
}
999-
1000-
public PluginPluginUpdatesDetails call()
1001-
throws Exception
1002-
{
1003-
return new PluginPluginUpdatesDetails( plugin, lookupPluginUpdates( plugin, allowSnapshots ) );
1004-
}
1005-
}
1006-
1007909
/**
1008910
* Builder class for {@linkplain DefaultVersionsHelper}
1009911
*/

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ ArtifactVersion getNewestUpdate( Optional<Segment> updateScope, boolean includeS
348348
ArtifactVersion[] getAllUpdates( Optional<Segment> updateScope, boolean includeSnapshots )
349349
throws InvalidSegmentException;
350350

351+
/**
352+
* Returns the all versions newer than the specified current version
353+
*
354+
* @return the all versions after currentVersion
355+
* @since 2.13.0
356+
*/
357+
ArtifactVersion[] getAllUpdates();
358+
351359
/**
352360
* Returns the all versions newer than the specified current version, but within the specified update scope.
353361
*

0 commit comments

Comments
 (0)