1717
1818import io .micrometer .release .common .ProcessRunner ;
1919import io .micrometer .release .single .ChangelogSection .Section ;
20+ import org .slf4j .Logger ;
21+ import org .slf4j .LoggerFactory ;
2022
21- import java .io .*;
23+ import java .io .BufferedWriter ;
24+ import java .io .File ;
2225import java .nio .file .Files ;
2326import java .util .*;
2427import java .util .regex .Matcher ;
2528import java .util .regex .Pattern ;
26-
27- import org .slf4j .Logger ;
28- import org .slf4j .LoggerFactory ;
29+ import java .util .stream .Collectors ;
2930
3031class ChangelogProcessor {
3132
@@ -51,7 +52,8 @@ class ChangelogProcessor {
5152 }
5253
5354 File processChangelog (File changelog , File oldChangelog ) throws Exception {
54- Set <String > testAndOptional = fetchTestAndOptionalDependencies ();
55+ Set <Dependency > dependencies = fetchAllDependencies ();
56+ Set <Dependency > testOrOptional = dependencies .stream ().filter (Dependency ::toIgnore ).collect (Collectors .toSet ());
5557
5658 Changelog currentChangelog = Changelog .parse (changelog );
5759 Changelog oldChangelogContent = oldChangelog != null ? Changelog .parse (oldChangelog ) : new Changelog ();
@@ -64,7 +66,10 @@ File processChangelog(File changelog, File oldChangelog) throws Exception {
6466
6567 // Process dependencies section specially
6668 ChangelogSection depsSection = currentChangelog .getSection (Section .UPGRADES );
67- Collection <String > processedDeps = processDependencyUpgrades (depsSection .getEntries (), testAndOptional );
69+ Collection <String > processedDeps = processDependencyUpgrades (depsSection .getEntries (),
70+ testOrOptional .stream ()
71+ .map (dependency -> dependency .group () + ":" + dependency .artifact ())
72+ .collect (Collectors .toSet ()));
6873 depsSection .clear ();
6974 processedDeps .forEach (depsSection ::addEntry );
7075
@@ -90,7 +95,7 @@ File processChangelog(File changelog, File oldChangelog) throws Exception {
9095 return outputFile ;
9196 }
9297
93- private Set <String > fetchTestAndOptionalDependencies () {
98+ private Set <Dependency > fetchAllDependencies () {
9499 log .info ("Fetching test and optional dependencies..." );
95100 List <String > projectLines = projectLines ();
96101 List <String > subprojects = projectLines .stream ()
@@ -100,8 +105,7 @@ private Set<String> fetchTestAndOptionalDependencies() {
100105
101106 log .info ("Subprojects: {}" , subprojects );
102107
103- Set <String > testOptionalDependencies = new HashSet <>();
104- Set <String > implementationDependencies = new HashSet <>();
108+ Set <Dependency > dependencies = new HashSet <>();
105109
106110 if (!subprojects .isEmpty ()) {
107111 List <String > gradleCommand = new ArrayList <>();
@@ -112,13 +116,22 @@ private Set<String> fetchTestAndOptionalDependencies() {
112116 for (String line : dependenciesLines (gradleCommand )) {
113117 if (line .startsWith ("+---" ) || line .startsWith ("\\ ---" )) {
114118 String [] parts = line .split ("[: ]" );
115- String dependency = parts [1 ] + ":" + parts [2 ];
116- if (testOrOptional ) {
117- testOptionalDependencies .add (dependency );
118- }
119- else {
120- implementationDependencies .add (dependency );
121- }
119+ String version = extractVersion (line );
120+ boolean finalTestOrOptional = testOrOptional ;
121+ dependencies .stream ()
122+ .filter (dependency -> dependency .group ().equalsIgnoreCase (parts [1 ])
123+ && dependency .artifact ().equalsIgnoreCase (parts [2 ]))
124+ .findFirst ()
125+ .ifPresentOrElse (dependency -> {
126+ log .debug ("Dependency {} is already present in compile scope" , parts [1 ] + ":" + parts [2 ]);
127+ if (dependency .toIgnore () && !finalTestOrOptional ) {
128+ log .debug (
129+ "Dependency {} was previously set in test or compile scope and will be in favour of one in compile scope" ,
130+ dependency );
131+ dependencies .remove (dependency );
132+ dependencies .add (new Dependency (parts [1 ], parts [2 ], version , finalTestOrOptional ));
133+ }
134+ }, () -> dependencies .add (new Dependency (parts [1 ], parts [2 ], version , finalTestOrOptional )));
122135 }
123136 else if (excludedDependencyScopes .stream ()
124137 .anyMatch (string -> line .toLowerCase ().contains (string .toLowerCase ()))) {
@@ -128,12 +141,30 @@ else if (line.isEmpty() || line.isBlank()) {
128141 testOrOptional = false ;
129142 }
130143 }
131-
132- testOptionalDependencies .removeAll (implementationDependencies );
133- log .info ("Excluded dependencies: {}" , testOptionalDependencies );
134144 }
135145
136- return testOptionalDependencies ;
146+ return dependencies ;
147+ }
148+
149+ static String extractVersion (String line ) {
150+ if (line == null || line .trim ().isEmpty ()) {
151+ return null ;
152+ }
153+ if (line .contains ("->" )) {
154+ String [] parts = line .split ("->" );
155+ if (parts .length > 1 ) {
156+ return parts [1 ].trim ().split ("\\ s+" )[0 ];
157+ }
158+ return null ;
159+ }
160+ String [] parts = line .split (":" );
161+ if (parts .length < 2 ) {
162+ return null ;
163+ }
164+ if (parts .length >= 3 ) {
165+ return parts [2 ].trim ().split ("\\ s+" )[0 ];
166+ }
167+ return null ;
137168 }
138169
139170 List <String > dependenciesLines (List <String > gradleCommand ) {
0 commit comments