16
16
17
17
package org .springframework .boot .build ;
18
18
19
+ import java .net .URI ;
20
+ import java .util .function .BiFunction ;
21
+
19
22
import javax .inject .Inject ;
20
23
21
24
import org .gradle .api .Project ;
29
32
*/
30
33
public class RepositoryTransformersExtension {
31
34
32
- private static final String MARKER = "{spring.mavenRepositories}" ;
35
+ private static final String REPOSITORIES_MARKER = "{spring.mavenRepositories}" ;
33
36
34
- private static final String MARKER_PLUGIN = "{spring.mavenPluginRepositories}" ;
37
+ private static final String PLUGIN_REPOSITORIES_MARKER = "{spring.mavenPluginRepositories}" ;
35
38
36
39
private final Project project ;
37
40
@@ -45,18 +48,12 @@ public Transformer<String, String> ant() {
45
48
}
46
49
47
50
private String transformAnt (String line ) {
48
- if (line .contains (MARKER )) {
49
- StringBuilder result = new StringBuilder ();
50
- String indent = getIndent (line );
51
- this .project .getRepositories ().withType (MavenArtifactRepository .class , (repository ) -> {
51
+ if (line .contains (REPOSITORIES_MARKER )) {
52
+ return transform (line , (repository , indent ) -> {
52
53
String name = repository .getName ();
53
- if (name .startsWith ("spring-" )) {
54
- result .append (!result .isEmpty () ? "\n " : "" );
55
- result .append ("%s<ibiblio name=\" %s\" m2compatible=\" true\" root=\" %s\" />" .formatted (indent , name ,
56
- repository .getUrl ()));
57
- }
54
+ URI url = repository .getUrl ();
55
+ return "%s<ibiblio name=\" %s\" m2compatible=\" true\" root=\" %s\" />" .formatted (indent , name , url );
58
56
});
59
- return result .toString ();
60
57
}
61
58
return line ;
62
59
}
@@ -66,26 +63,17 @@ public Transformer<String, String> mavenSettings() {
66
63
}
67
64
68
65
private String transformMavenSettings (String line ) {
69
- if (line .contains (MARKER )) {
70
- return transformMarker (line , false );
66
+ if (line .contains (REPOSITORIES_MARKER )) {
67
+ return transformMavenRepositories (line , false );
71
68
}
72
- if (line .contains (MARKER_PLUGIN )) {
73
- return transformMarker (line , true );
69
+ if (line .contains (PLUGIN_REPOSITORIES_MARKER )) {
70
+ return transformMavenRepositories (line , true );
74
71
}
75
72
return line ;
76
73
}
77
74
78
- private String transformMarker (String line , boolean pluginRepository ) {
79
- StringBuilder result = new StringBuilder ();
80
- String indent = getIndent (line );
81
- this .project .getRepositories ().withType (MavenArtifactRepository .class , (repository ) -> {
82
- String name = repository .getName ();
83
- if (name .startsWith ("spring-" )) {
84
- result .append (!result .isEmpty () ? "\n " : "" );
85
- result .append (mavenRepositoryXml (indent , repository , pluginRepository ));
86
- }
87
- });
88
- return result .toString ();
75
+ private String transformMavenRepositories (String line , boolean pluginRepository ) {
76
+ return transform (line , (repository , indent ) -> mavenRepositoryXml (indent , repository , pluginRepository ));
89
77
}
90
78
91
79
private String mavenRepositoryXml (String indent , MavenArtifactRepository repository , boolean pluginRepository ) {
@@ -105,6 +93,22 @@ private String mavenRepositoryXml(String indent, MavenArtifactRepository reposit
105
93
return xml .toString ();
106
94
}
107
95
96
+ private String transform (String line , BiFunction <MavenArtifactRepository , String , String > generator ) {
97
+ StringBuilder result = new StringBuilder ();
98
+ String indent = getIndent (line );
99
+ this .project .getRepositories ().withType (MavenArtifactRepository .class , (repository ) -> {
100
+ String name = repository .getName ();
101
+ if (name .startsWith ("spring-" )) {
102
+ String fragment = generator .apply (repository , indent );
103
+ if (fragment != null ) {
104
+ result .append (!result .isEmpty () ? "\n " : "" );
105
+ result .append (fragment );
106
+ }
107
+ }
108
+ });
109
+ return result .toString ();
110
+ }
111
+
108
112
private String getIndent (String line ) {
109
113
return line .substring (0 , line .length () - line .stripLeading ().length ());
110
114
}
0 commit comments