22
22
import javax .inject .Named ;
23
23
import javax .inject .Singleton ;
24
24
25
+ import java .io .File ;
25
26
import java .util .ArrayList ;
26
27
import java .util .Collection ;
28
+ import java .util .Collections ;
27
29
import java .util .HashSet ;
28
30
import java .util .List ;
31
+ import java .util .Map ;
32
+ import java .util .Properties ;
33
+ import java .util .function .UnaryOperator ;
34
+ import java .util .stream .Collectors ;
29
35
30
36
import org .apache .maven .model .Activation ;
37
+ import org .apache .maven .model .Model ;
31
38
import org .apache .maven .model .Profile ;
39
+ import org .apache .maven .model .building .DefaultModelBuildingRequest ;
40
+ import org .apache .maven .model .building .ModelBuildingRequest ;
32
41
import org .apache .maven .model .building .ModelProblem .Severity ;
33
42
import org .apache .maven .model .building .ModelProblem .Version ;
34
43
import org .apache .maven .model .building .ModelProblemCollector ;
35
44
import org .apache .maven .model .building .ModelProblemCollectorRequest ;
45
+ import org .apache .maven .model .interpolation .ModelInterpolator ;
36
46
import org .apache .maven .model .profile .activation .ProfileActivator ;
37
47
38
48
/**
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
@ Inject
48
63
private List <ProfileActivator > activators = new ArrayList <>();
49
64
65
+ @ Inject
66
+ private ModelInterpolator interpolator = new ModelInterpolator () {
67
+
68
+ @ Override
69
+ public Model interpolateModel (
70
+ Model model , File projectDir , ModelBuildingRequest request , ModelProblemCollector problems ) {
71
+ return model ;
72
+ }
73
+ };
74
+
50
75
public DefaultProfileSelector addProfileActivator (ProfileActivator profileActivator ) {
51
76
if (profileActivator != null ) {
52
77
activators .add (profileActivator );
53
78
}
54
79
return this ;
55
80
}
56
81
82
+ public void setInterpolator (ModelInterpolator interpolator ) {
83
+ this .interpolator = interpolator ;
84
+ }
85
+
57
86
@ Override
58
87
public List <Profile > getActiveProfiles (
59
88
Collection <Profile > profiles , ProfileActivationContext context , ModelProblemCollector problems ) {
@@ -64,9 +93,12 @@ public List<Profile> getActiveProfiles(
64
93
List <Profile > activePomProfilesByDefault = new ArrayList <>();
65
94
boolean activatedPomProfileNotByDefault = false ;
66
95
96
+ Map <String , Profile > activation = earlyInterpolateProfileActivations (profiles , context );
97
+
67
98
for (Profile profile : profiles ) {
68
99
if (!deactivatedIds .contains (profile .getId ())) {
69
- if (activatedIds .contains (profile .getId ()) || isActive (profile , context , problems )) {
100
+ if (activatedIds .contains (profile .getId ())
101
+ || isActive (activation .get (profile .getId ()), context , problems )) {
70
102
activeProfiles .add (profile );
71
103
72
104
if (Profile .SOURCE_POM .equals (profile .getSource ())) {
@@ -89,6 +121,39 @@ public List<Profile> getActiveProfiles(
89
121
return activeProfiles ;
90
122
}
91
123
124
+ private Map <String , Profile > earlyInterpolateProfileActivations (
125
+ Collection <Profile > original , ProfileActivationContext context ) {
126
+
127
+ if (original .stream ().map (Profile ::getId ).distinct ().count () < original .size ()) {
128
+ // invalid profile specification
129
+ return Collections .emptyMap ();
130
+ }
131
+ Model model = new Model ();
132
+
133
+ UnaryOperator <Profile > activatableProfile = p -> {
134
+ Profile result = new Profile ();
135
+ result .setId (p .getId ());
136
+ result .setActivation (p .getActivation ());
137
+ return result ;
138
+ };
139
+ model .setProfiles (original .stream ().map (activatableProfile ).collect (Collectors .toList ()));
140
+
141
+ ModelBuildingRequest mbr = new DefaultModelBuildingRequest ()
142
+ .setActiveProfileIds (context .getActiveProfileIds ())
143
+ .setInactiveProfileIds (context .getInactiveProfileIds ())
144
+ .setRawModel (model )
145
+ .setSystemProperties (asProperties (context .getSystemProperties ()))
146
+ .setUserProperties (asProperties (context .getUserProperties ()))
147
+ .setTwoPhaseBuilding (true )
148
+ .setValidationLevel (ModelBuildingRequest .VALIDATION_LEVEL_MINIMAL );
149
+
150
+ interpolator
151
+ .interpolateModel (model , context .getProjectDirectory (), mbr , problem -> {})
152
+ .getProfiles ();
153
+
154
+ return model .getProfiles ().stream ().collect (Collectors .toMap (Profile ::getId , UnaryOperator .identity ()));
155
+ }
156
+
92
157
private boolean isActive (Profile profile , ProfileActivationContext context , ModelProblemCollector problems ) {
93
158
boolean isActive = false ;
94
159
for (ProfileActivator activator : activators ) {
0 commit comments