11package org .jboss .set .mavendependencyupdater .core .processingstrategies ;
22
3- import org .apache .commons .lang3 .builder .EqualsBuilder ;
4- import org .apache .commons .lang3 .builder .HashCodeBuilder ;
53import org .commonjava .maven .atlas .ident .ref .ArtifactRef ;
64import org .jboss .logging .Logger ;
75import org .jboss .set .mavendependencyupdater .DependencyEvaluator .ComponentUpgrade ;
8- import org .jboss .set .mavendependencyupdater .LocatedDependency ;
9- import org .jboss .set .mavendependencyupdater .PomDependencyUpdater ;
106import org .jboss .set .mavendependencyupdater .configuration .Configuration ;
7+ import org .jboss .set .mavendependencyupdater .core .aggregation .ComponentUpgradeAggregator ;
118
129import java .io .File ;
1310import java .io .FileNotFoundException ;
1411import java .io .PrintStream ;
15- import java .net .URI ;
1612import java .time .ZonedDateTime ;
1713import java .time .format .DateTimeFormatter ;
1814import java .util .Comparator ;
19- import java .util .HashSet ;
2015import java .util .List ;
2116import java .util .Map ;
22- import java .util .Optional ;
23- import java .util .Set ;
2417import java .util .stream .Collectors ;
2518
2619/**
@@ -38,7 +31,6 @@ public class TextReportProcessingStrategy implements UpgradeProcessingStrategy {
3831
3932 final protected File pomFile ;
4033 final protected Configuration configuration ;
41- final protected Set <ModifiedProperty > recordedUpdates = new HashSet <>();
4234 protected PrintStream outputStream ;
4335 protected File outputFile ;
4436
@@ -83,6 +75,7 @@ public boolean process(List<ComponentUpgrade> upgrades) {
8375 List <ComponentUpgrade > sortedUpgrades =
8476 upgrades .stream ().sorted (new ComponentUpgradeComparator ())
8577 .collect (Collectors .toList ());
78+ List <ComponentUpgrade > aggregatedUpgrades = ComponentUpgradeAggregator .aggregateComponentUpgrades (pomFile , sortedUpgrades );
8679
8780 outputStream .println ("Generated at " + DATE_TIME_FORMATTER .format (ZonedDateTime .now ()));
8881 outputStream .println ();
@@ -93,33 +86,18 @@ public boolean process(List<ComponentUpgrade> upgrades) {
9386 outputStream .println ();
9487 outputStream .println ("Possible upgrades:\n " );
9588
96- int counter = 0 ;
97- URI uri = pomFile .toURI ();
98- for (ComponentUpgrade upgrade : sortedUpgrades ) {
99- Optional <LocatedDependency > locatedDependencyOpt =
100- PomDependencyUpdater .locateDependency (pomFile , upgrade .getArtifact ());
89+ for (ComponentUpgrade upgrade : aggregatedUpgrades ) {
10190 ArtifactRef artifact = upgrade .getArtifact ();
102- String newVersion = upgrade .getNewVersion ();
103- String repoId = upgrade .getRepository ();
104-
105- if (locatedDependencyOpt .isPresent ()) {
106- LocatedDependency locatedDependency = locatedDependencyOpt .get ();
107- boolean added = recordedUpdates .add (
108- new ModifiedProperty (uri , locatedDependency .getProfile (), locatedDependency .getVersionProperty (), newVersion ));
109- if (added ) {
110- counter ++;
111- outputStream .println (
112- String .format ("%s:%s:%s -> %s (%s) - %s" ,
113- artifact .getGroupId (), artifact .getArtifactId (), artifact .getVersionString (),
114- newVersion , repoId ,
115- upgrade .getFirstSeen () == null ?
116- "new" : "since " + upgrade .getFirstSeen ().format (DATE_FORMATTER )
117- ));
118- }
119- }
91+ outputStream .println (
92+ String .format ("%s:%s:%s -> %s (%s) - %s" ,
93+ artifact .getGroupId (), artifact .getArtifactId (), artifact .getVersionString (),
94+ upgrade .getNewVersion (), upgrade .getRepository (),
95+ upgrade .getFirstSeen () == null ?
96+ "new" : "since " + upgrade .getFirstSeen ().format (DATE_FORMATTER )
97+ ));
12098 }
12199
122- outputStream .println ("\n " + counter + " items" );
100+ outputStream .println ("\n " + aggregatedUpgrades . size () + " items" );
123101 outputStream .println ("\n Report generated by Maven Dependency Updater" );
124102 outputStream .println (PROJECT_URL );
125103 return true ;
@@ -132,68 +110,24 @@ public boolean process(List<ComponentUpgrade> upgrades) {
132110 }
133111 }
134112
135- /**
136- * Identifies a property in a particular POM file and a profile.
137- *
138- * It's used to record properties that would be changed by a component upgrade, to detect that given property would
139- * have already been changed by a previous component upgrade.
140- */
141- protected static class ModifiedProperty {
142-
143- final private URI pomUri ;
144- final private String profile ;
145- final private String propertyName ;
146- final private String newValue ;
147-
148- ModifiedProperty (URI pomUri , String profile , String propertyName , String newValue ) {
149- this .pomUri = pomUri ;
150- this .profile = profile ;
151- this .propertyName = propertyName ;
152- this .newValue = newValue ;
153- }
154-
155- @ Override
156- public boolean equals (Object o ) {
157- if (this == o ) return true ;
158-
159- if (o == null || getClass () != o .getClass ()) return false ;
160-
161- ModifiedProperty that = (ModifiedProperty ) o ;
162-
163- return new EqualsBuilder ()
164- .append (pomUri , that .pomUri )
165- .append (profile , that .profile )
166- .append (propertyName , that .propertyName )
167- .append (newValue , that .newValue )
168- .isEquals ();
169- }
170-
171- @ Override
172- public int hashCode () {
173- return new HashCodeBuilder (17 , 37 )
174- .append (pomUri )
175- .append (profile )
176- .append (propertyName )
177- .append (newValue )
178- .toHashCode ();
179- }
180- }
181-
182113 /**
183114 * Comparator for sorting component upgrades. Sort primarily by first seen date, then alphabetically.
184115 */
185116 protected static class ComponentUpgradeComparator implements Comparator <ComponentUpgrade > {
186117 @ Override
187118 public int compare (ComponentUpgrade o1 , ComponentUpgrade o2 ) {
119+ // order null values first
188120 if (o1 .getFirstSeen () == null && o2 .getFirstSeen () == null ) {
189121 return o1 .getArtifact ().compareTo (o2 .getArtifact ());
190122 } else if (o1 .getFirstSeen () == null ) {
191- return 1 ;
192- } else if (o2 .getFirstSeen () == null ) {
193123 return -1 ;
124+ } else if (o2 .getFirstSeen () == null ) {
125+ return 1 ;
194126 } else {
195- int res = o1 .getFirstSeen ().toLocalDate ().compareTo (o2 .getFirstSeen ().toLocalDate ());
127+ // order by age from new to old
128+ int res = -o1 .getFirstSeen ().toLocalDate ().compareTo (o2 .getFirstSeen ().toLocalDate ());
196129 if (res == 0 ) {
130+ // when the date is the same, order alphabetically
197131 res = o1 .getArtifact ().toString ().compareTo (o2 .getArtifact ().toString ());
198132 }
199133 return res ;
0 commit comments