23
23
24
24
import java .nio .file .Path ;
25
25
import java .util .ArrayList ;
26
+ import java .util .LinkedHashMap ;
26
27
import java .util .List ;
28
+ import java .util .Map ;
29
+ import java .util .function .Function ;
27
30
import java .util .stream .Collectors ;
28
31
32
+ import org .apache .maven .api .ArtifactCoordinates ;
33
+ import org .apache .maven .api .Node ;
34
+ import org .apache .maven .api .PathScope ;
29
35
import org .apache .maven .api .SessionData ;
30
36
import org .apache .maven .api .model .Dependency ;
31
37
import org .apache .maven .api .model .DependencyManagement ;
@@ -77,20 +83,100 @@ public Model build(RepositorySystemSession session, MavenProject project, Path s
77
83
78
84
protected Model buildPom (RepositorySystemSession session , MavenProject project , Path src )
79
85
throws ModelBuilderException {
80
- ModelBuilderResult result = buildModel (session , project , src );
86
+ ModelBuilderResult result = buildModel (session , src );
81
87
Model model = result .getRawModel ();
82
88
return transform (model , project );
83
89
}
84
90
85
91
protected Model buildNonPom (RepositorySystemSession session , MavenProject project , Path src )
86
92
throws ModelBuilderException {
87
- ModelBuilderResult result = buildModel (session , project , src );
88
- Model model = result .getEffectiveModel ();
93
+ Model model = buildEffectiveModel (session , src );
89
94
return transform (model , project );
90
95
}
91
96
92
- private ModelBuilderResult buildModel (RepositorySystemSession session , MavenProject project , Path src )
93
- throws ModelBuilderException {
97
+ private Model buildEffectiveModel (RepositorySystemSession session , Path src ) throws ModelBuilderException {
98
+ InternalSession iSession = InternalSession .from (session );
99
+ ModelBuilderResult result = buildModel (session , src );
100
+ Model model = result .getEffectiveModel ();
101
+
102
+ if (model .getDependencyManagement () != null
103
+ && !model .getDependencyManagement ().getDependencies ().isEmpty ()) {
104
+ ArtifactCoordinates artifact = iSession .createArtifactCoordinates (
105
+ model .getGroupId (), model .getArtifactId (), model .getVersion (), null );
106
+ Node node = iSession .collectDependencies (
107
+ iSession .createDependencyCoordinates (artifact ), PathScope .TEST_RUNTIME );
108
+
109
+ Map <String , Node > nodes = node .stream ()
110
+ .collect (Collectors .toMap (n -> getDependencyKey (n .getDependency ()), Function .identity ()));
111
+ Map <String , Dependency > directDependencies = model .getDependencies ().stream ()
112
+ .filter (dependency -> !"import" .equals (dependency .getScope ()))
113
+ .collect (Collectors .toMap (
114
+ DefaultConsumerPomBuilder ::getDependencyKey ,
115
+ Function .identity (),
116
+ this ::merge ,
117
+ LinkedHashMap ::new ));
118
+ Map <String , Dependency > managedDependencies = model .getDependencyManagement ().getDependencies ().stream ()
119
+ .filter (dependency ->
120
+ nodes .containsKey (getDependencyKey (dependency )) && !"import" .equals (dependency .getScope ()))
121
+ .collect (Collectors .toMap (
122
+ DefaultConsumerPomBuilder ::getDependencyKey ,
123
+ Function .identity (),
124
+ this ::merge ,
125
+ LinkedHashMap ::new ));
126
+
127
+ // for each managed dep in the model:
128
+ // * if there is no corresponding node in the tree, discard the managed dep
129
+ // * if there's a direct dependency, apply the managed dependency to it and discard the managed dep
130
+ // * else keep the managed dep
131
+ managedDependencies .keySet ().retainAll (nodes .keySet ());
132
+
133
+ directDependencies .replaceAll ((key , dependency ) -> {
134
+ var managedDependency = managedDependencies .get (key );
135
+ if (managedDependency != null ) {
136
+ if (dependency .getVersion () == null && managedDependency .getVersion () != null ) {
137
+ dependency = dependency .withVersion (managedDependency .getVersion ());
138
+ }
139
+ if (dependency .getScope () == null && managedDependency .getScope () != null ) {
140
+ dependency = dependency .withScope (managedDependency .getScope ());
141
+ }
142
+ if (dependency .getOptional () == null && managedDependency .getOptional () != null ) {
143
+ dependency = dependency .withOptional (managedDependency .getOptional ());
144
+ }
145
+ if (dependency .getExclusions ().isEmpty ()
146
+ && !managedDependency .getExclusions ().isEmpty ()) {
147
+ dependency = dependency .withExclusions (managedDependency .getExclusions ());
148
+ }
149
+ }
150
+ return dependency ;
151
+ });
152
+ managedDependencies .keySet ().removeAll (directDependencies .keySet ());
153
+
154
+ model = model .withDependencyManagement (
155
+ managedDependencies .isEmpty ()
156
+ ? null
157
+ : model .getDependencyManagement ().withDependencies (managedDependencies .values ()))
158
+ .withDependencies (directDependencies .isEmpty () ? null : directDependencies .values ());
159
+ }
160
+
161
+ return model ;
162
+ }
163
+
164
+ private Dependency merge (Dependency dep1 , Dependency dep2 ) {
165
+ throw new IllegalArgumentException ("Duplicate dependency: " + dep1 );
166
+ }
167
+
168
+ private static String getDependencyKey (org .apache .maven .api .Dependency dependency ) {
169
+ return dependency .getGroupId () + ":" + dependency .getArtifactId () + ":" + dependency .getType () + ":"
170
+ + dependency .getClassifier ();
171
+ }
172
+
173
+ private static String getDependencyKey (Dependency dependency ) {
174
+ return dependency .getGroupId () + ":" + dependency .getArtifactId () + ":"
175
+ + (dependency .getType () != null ? dependency .getType () : "" ) + ":"
176
+ + (dependency .getClassifier () != null ? dependency .getClassifier () : "" );
177
+ }
178
+
179
+ private ModelBuilderResult buildModel (RepositorySystemSession session , Path src ) throws ModelBuilderException {
94
180
InternalSession iSession = InternalSession .from (session );
95
181
ModelBuilderRequest .ModelBuilderRequestBuilder request = ModelBuilderRequest .builder ();
96
182
request .requestType (ModelBuilderRequest .RequestType .BUILD_CONSUMER );
@@ -102,7 +188,8 @@ private ModelBuilderResult buildModel(RepositorySystemSession session, MavenProj
102
188
request .lifecycleBindingsInjector (lifecycleBindingsInjector ::injectLifecycleBindings );
103
189
ModelBuilder .ModelBuilderSession mbSession =
104
190
iSession .getData ().get (SessionData .key (ModelBuilder .ModelBuilderSession .class ));
105
- return mbSession .build (request .build ());
191
+ ModelBuilderResult result = mbSession .build (request .build ());
192
+ return result ;
106
193
}
107
194
108
195
static Model transform (Model model , MavenProject project ) {
@@ -148,12 +235,16 @@ static Model transform(Model model, MavenProject project) {
148
235
model = model .withModelVersion (modelVersion );
149
236
} else {
150
237
Model .Builder builder = prune (
151
- Model .newBuilder (model , true )
152
- .preserveModelVersion (false )
153
- .root (false )
154
- .parent (null )
155
- .build (null ),
156
- model );
238
+ Model .newBuilder (model , true )
239
+ .preserveModelVersion (false )
240
+ .root (false )
241
+ .parent (null )
242
+ .build (null ),
243
+ model )
244
+ .developers (null )
245
+ .contributors (null )
246
+ .mailingLists (null )
247
+ .issueManagement (null );
157
248
builder .profiles (prune (model .getProfiles ()));
158
249
159
250
model = builder .build ();
@@ -210,8 +301,8 @@ private static <T extends ModelBase.Builder> T prune(T builder, ModelBase model)
210
301
.build ());
211
302
}
212
303
// only keep repositories other than 'central'
213
- builder .pluginRepositories (pruneRepositories (model .getPluginRepositories ()));
214
304
builder .repositories (pruneRepositories (model .getRepositories ()));
305
+ builder .pluginRepositories (null );
215
306
return builder ;
216
307
}
217
308
0 commit comments