38
38
import java .util .Set ;
39
39
import java .util .TreeMap ;
40
40
import java .util .TreeSet ;
41
- import java .util .concurrent .Callable ;
42
41
import java .util .concurrent .ExecutionException ;
43
42
import java .util .concurrent .ExecutorService ;
44
43
import java .util .concurrent .Executors ;
45
44
import java .util .concurrent .Future ;
46
45
import java .util .regex .Pattern ;
47
46
import java .util .stream .Collectors ;
48
47
48
+ import org .apache .commons .lang3 .tuple .ImmutablePair ;
49
+ import org .apache .commons .lang3 .tuple .Pair ;
49
50
import org .apache .maven .artifact .Artifact ;
50
51
import org .apache .maven .artifact .ArtifactUtils ;
51
52
import org .apache .maven .artifact .manager .WagonManager ;
@@ -668,109 +669,95 @@ public ArtifactVersion createArtifactVersion( String version )
668
669
return new DefaultArtifactVersion ( version );
669
670
}
670
671
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
+ */
671
681
@ Override
672
682
public Map <Dependency , ArtifactVersions > lookupDependenciesUpdates ( Set <Dependency > dependencies ,
673
683
boolean usePluginRepositories )
674
684
throws ArtifactMetadataRetrievalException
675
685
{
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 );
688
687
try
689
688
{
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 )
695
695
{
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 () );
698
698
}
699
+
700
+ return dependencyUpdates ;
699
701
}
700
702
catch ( ExecutionException | InterruptedException ie )
701
703
{
702
704
throw new ArtifactMetadataRetrievalException ( "Unable to acquire metadata for dependencies " + dependencies
703
- + ": " + ie .getMessage (), ie , null );
705
+ + ": " + ie .getMessage (), ie , null );
704
706
}
705
707
finally
706
708
{
707
- executor .shutdownNow ();
709
+ executor .shutdown ();
708
710
}
709
- return dependencyUpdates ;
710
711
}
711
712
712
713
@ Override
713
714
public ArtifactVersions lookupDependencyUpdates ( Dependency dependency , boolean usePluginRepositories )
714
715
throws ArtifactMetadataRetrievalException
715
716
{
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 () );
721
721
}
722
722
723
723
@ Override
724
724
public Map <Plugin , PluginUpdatesDetails > lookupPluginsUpdates ( Set <Plugin > plugins , boolean allowSnapshots )
725
725
throws ArtifactMetadataRetrievalException
726
726
{
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...
737
727
ExecutorService executor = Executors .newFixedThreadPool ( LOOKUP_PARALLEL_THREADS );
738
728
try
739
729
{
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 )
745
736
{
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 () );
748
739
}
740
+
741
+ return pluginUpdates ;
749
742
}
750
743
catch ( ExecutionException | InterruptedException ie )
751
744
{
752
745
throw new ArtifactMetadataRetrievalException ( "Unable to acquire metadata for plugins " + plugins + ": "
753
- + ie .getMessage (), ie , null );
746
+ + ie .getMessage (), ie , null );
754
747
}
755
748
finally
756
749
{
757
- executor .shutdownNow ();
750
+ executor .shutdown ();
758
751
}
759
- return pluginUpdates ;
760
752
}
761
753
762
754
@ Override
763
755
public PluginUpdatesDetails lookupPluginUpdates ( Plugin plugin , boolean allowSnapshots )
764
756
throws ArtifactMetadataRetrievalException
765
757
{
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" ;
774
761
775
762
Set <Dependency > pluginDependencies = new TreeSet <>( DependencyComparator .INSTANCE );
776
763
if ( plugin .getDependencies () != null )
@@ -780,7 +767,13 @@ public PluginUpdatesDetails lookupPluginUpdates( Plugin plugin, boolean allowSna
780
767
Map <Dependency , ArtifactVersions > pluginDependencyDetails =
781
768
lookupDependenciesUpdates ( pluginDependencies , false );
782
769
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 );
784
777
}
785
778
786
779
@ Override
@@ -832,8 +825,8 @@ public Map<Property, PropertyVersions> getVersionPropertiesMap( MavenProject pro
832
825
}
833
826
}
834
827
835
- List <String > includePropertiesList = getSplittedProperties ( includeProperties );
836
- List <String > excludePropertiesList = getSplittedProperties ( excludeProperties );
828
+ List <String > includePropertiesList = getSplitProperties ( includeProperties );
829
+ List <String > excludePropertiesList = getSplitProperties ( excludeProperties );
837
830
838
831
getLog ().debug ( "Searching for properties associated with builders" );
839
832
Iterator <Property > i = properties .values ().iterator ();
@@ -902,7 +895,7 @@ else if ( !excludePropertiesList.isEmpty() && excludePropertiesList.contains( pr
902
895
return propertyVersions ;
903
896
}
904
897
905
- private List <String > getSplittedProperties ( String commaSeparatedProperties )
898
+ private List <String > getSplitProperties ( String commaSeparatedProperties )
906
899
{
907
900
List <String > propertiesList = Collections .emptyList ();
908
901
if ( StringUtils .isNotEmpty ( commaSeparatedProperties ) )
@@ -913,97 +906,6 @@ private List<String> getSplittedProperties( String commaSeparatedProperties )
913
906
return propertiesList ;
914
907
}
915
908
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
-
1007
909
/**
1008
910
* Builder class for {@linkplain DefaultVersionsHelper}
1009
911
*/
0 commit comments