22
22
import javax .inject .Named ;
23
23
import javax .inject .Singleton ;
24
24
25
+ import java .io .File ;
26
+ import java .nio .file .Path ;
25
27
import java .util .ArrayList ;
26
28
import java .util .Collection ;
29
+ import java .util .Collections ;
27
30
import java .util .HashSet ;
28
31
import java .util .List ;
32
+ import java .util .Map ;
33
+ import java .util .Optional ;
34
+ import java .util .Properties ;
29
35
import java .util .stream .Collectors ;
30
36
31
37
import org .apache .maven .model .Activation ;
38
+ import org .apache .maven .model .Model ;
32
39
import org .apache .maven .model .Profile ;
40
+ import org .apache .maven .model .building .DefaultModelBuildingRequest ;
41
+ import org .apache .maven .model .building .ModelBuildingRequest ;
33
42
import org .apache .maven .model .building .ModelProblem .Severity ;
34
43
import org .apache .maven .model .building .ModelProblem .Version ;
35
44
import org .apache .maven .model .building .ModelProblemCollector ;
36
45
import org .apache .maven .model .building .ModelProblemCollectorRequest ;
46
+ import org .apache .maven .model .interpolation .ModelInterpolator ;
37
47
import org .apache .maven .model .profile .activation .ProfileActivator ;
38
48
39
49
/**
44
54
@ Singleton
45
55
public class DefaultProfileSelector implements ProfileSelector {
46
56
57
+ private static Properties asProperties (Map <String , String > m ) {
58
+ return m .entrySet ().stream ()
59
+ .collect (Collectors .toMap (e -> e .getKey (), e -> e .getValue (), (l , r ) -> r , Properties ::new ));
60
+ }
61
+
47
62
private final List <ProfileActivator > activators ;
63
+ private ModelInterpolator interpolator ;
48
64
49
65
public DefaultProfileSelector () {
50
- this .activators = new ArrayList <>();
66
+ this (new ArrayList <>(), new ModelInterpolator () {
67
+
68
+ @ Override
69
+ public Model interpolateModel (
70
+ Model model , File projectDir , ModelBuildingRequest request , ModelProblemCollector problems ) {
71
+ return model ;
72
+ }
73
+
74
+ @ Override
75
+ public org .apache .maven .api .model .Model interpolateModel (
76
+ org .apache .maven .api .model .Model model ,
77
+ File projectDir ,
78
+ ModelBuildingRequest request ,
79
+ ModelProblemCollector problems ) {
80
+ return model ;
81
+ }
82
+
83
+ @ Override
84
+ public Model interpolateModel (
85
+ Model model , Path projectDir , ModelBuildingRequest request , ModelProblemCollector problems ) {
86
+ return model ;
87
+ }
88
+
89
+ @ Override
90
+ public org .apache .maven .api .model .Model interpolateModel (
91
+ org .apache .maven .api .model .Model model ,
92
+ Path projectDir ,
93
+ ModelBuildingRequest request ,
94
+ ModelProblemCollector problems ) {
95
+ return model ;
96
+ }
97
+ });
51
98
}
52
99
53
100
@ Inject
54
- public DefaultProfileSelector (List <ProfileActivator > activators ) {
101
+ public DefaultProfileSelector (List <ProfileActivator > activators , ModelInterpolator interpolator ) {
55
102
this .activators = new ArrayList <>(activators );
103
+ this .interpolator = interpolator ;
56
104
}
57
105
58
106
public DefaultProfileSelector addProfileActivator (ProfileActivator profileActivator ) {
@@ -62,6 +110,10 @@ public DefaultProfileSelector addProfileActivator(ProfileActivator profileActiva
62
110
return this ;
63
111
}
64
112
113
+ public void setInterpolator (ModelInterpolator interpolator ) {
114
+ this .interpolator = interpolator ;
115
+ }
116
+
65
117
@ Override
66
118
public List <org .apache .maven .api .model .Profile > getActiveProfilesV4 (
67
119
Collection <org .apache .maven .api .model .Profile > profiles ,
@@ -76,16 +128,24 @@ public List<org.apache.maven.api.model.Profile> getActiveProfilesV4(
76
128
@ Override
77
129
public List <Profile > getActiveProfiles (
78
130
Collection <Profile > profiles , ProfileActivationContext context , ModelProblemCollector problems ) {
131
+
132
+ if (profiles .stream ().map (Profile ::getId ).distinct ().count () < profiles .size ()) {
133
+ // invalid profile specification
134
+ return Collections .emptyList ();
135
+ }
79
136
Collection <String > activatedIds = new HashSet <>(context .getActiveProfileIds ());
80
137
Collection <String > deactivatedIds = new HashSet <>(context .getInactiveProfileIds ());
81
138
82
139
List <Profile > activeProfiles = new ArrayList <>(profiles .size ());
83
140
List <Profile > activePomProfilesByDefault = new ArrayList <>();
84
141
boolean activatedPomProfileNotByDefault = false ;
85
142
143
+ Map <String , Profile > activation = earlyInterpolateProfileActivations (profiles , context );
144
+
86
145
for (Profile profile : profiles ) {
87
146
if (!deactivatedIds .contains (profile .getId ())) {
88
- if (activatedIds .contains (profile .getId ()) || isActive (profile , context , problems )) {
147
+ if (activatedIds .contains (profile .getId ())
148
+ || isActive (activation .get (profile .getId ()), context , problems )) {
89
149
activeProfiles .add (profile );
90
150
91
151
if (Profile .SOURCE_POM .equals (profile .getSource ())) {
@@ -108,6 +168,40 @@ public List<Profile> getActiveProfiles(
108
168
return activeProfiles ;
109
169
}
110
170
171
+ private Map <String , Profile > earlyInterpolateProfileActivations (
172
+ Collection <Profile > original , ProfileActivationContext context ) {
173
+
174
+ org .apache .maven .api .model .Model model = org .apache .maven .api .model .Model .newBuilder ()
175
+ .profiles (original .stream ()
176
+ .map (p -> org .apache .maven .api .model .Profile .newBuilder ()
177
+ .id (p .getId ())
178
+ .activation (Optional .ofNullable (p .getActivation ())
179
+ .map (Activation ::getDelegate )
180
+ .orElse (null ))
181
+ .build ())
182
+ .collect (Collectors .toList ()))
183
+ .build ();
184
+
185
+ ModelBuildingRequest mbr = new DefaultModelBuildingRequest ()
186
+ .setActiveProfileIds (context .getActiveProfileIds ())
187
+ .setInactiveProfileIds (context .getInactiveProfileIds ())
188
+ .setSystemProperties (asProperties (context .getSystemProperties ()))
189
+ .setUserProperties (asProperties (context .getUserProperties ()))
190
+ .setTwoPhaseBuilding (true )
191
+ .setValidationLevel (ModelBuildingRequest .VALIDATION_LEVEL_MINIMAL );
192
+
193
+ model = interpolator .interpolateModel (
194
+ model ,
195
+ Optional .ofNullable (context .getProjectDirectory ())
196
+ .map (File ::toPath )
197
+ .orElse (null ),
198
+ mbr ,
199
+ problem -> {});
200
+
201
+ return model .getProfiles ().stream ()
202
+ .collect (Collectors .toMap (org .apache .maven .api .model .Profile ::getId , Profile ::new ));
203
+ }
204
+
111
205
private boolean isActive (Profile profile , ProfileActivationContext context , ModelProblemCollector problems ) {
112
206
boolean isActive = false ;
113
207
for (ProfileActivator activator : activators ) {
0 commit comments