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 ) {
89
+
90
+ if (profiles .stream ().map (Profile ::getId ).distinct ().count () < profiles .size ()) {
91
+ // invalid profile specification
92
+ return Collections .emptyList ();
93
+ }
60
94
Collection <String > activatedIds = new HashSet <>(context .getActiveProfileIds ());
61
95
Collection <String > deactivatedIds = new HashSet <>(context .getInactiveProfileIds ());
62
96
63
97
List <Profile > activeProfiles = new ArrayList <>(profiles .size ());
64
98
List <Profile > activePomProfilesByDefault = new ArrayList <>();
65
99
boolean activatedPomProfileNotByDefault = false ;
66
100
101
+ Map <String , Profile > activation = earlyInterpolateProfileActivations (profiles , context );
102
+
67
103
for (Profile profile : profiles ) {
68
104
if (!deactivatedIds .contains (profile .getId ())) {
69
- if (activatedIds .contains (profile .getId ()) || isActive (profile , context , problems )) {
105
+ if (activatedIds .contains (profile .getId ())
106
+ || isActive (activation .get (profile .getId ()), context , problems )) {
70
107
activeProfiles .add (profile );
71
108
72
109
if (Profile .SOURCE_POM .equals (profile .getSource ())) {
@@ -89,6 +126,35 @@ public List<Profile> getActiveProfiles(
89
126
return activeProfiles ;
90
127
}
91
128
129
+ private Map <String , Profile > earlyInterpolateProfileActivations (
130
+ Collection <Profile > original , ProfileActivationContext context ) {
131
+
132
+ Model model = new Model ();
133
+
134
+ UnaryOperator <Profile > activatableProfile = p -> {
135
+ Profile result = new Profile ();
136
+ result .setId (p .getId ());
137
+ result .setActivation (p .getActivation ());
138
+ return result ;
139
+ };
140
+ model .setProfiles (original .stream ().map (activatableProfile ).collect (Collectors .toList ()));
141
+
142
+ ModelBuildingRequest mbr = new DefaultModelBuildingRequest ()
143
+ .setActiveProfileIds (context .getActiveProfileIds ())
144
+ .setInactiveProfileIds (context .getInactiveProfileIds ())
145
+ .setRawModel (model )
146
+ .setSystemProperties (asProperties (context .getSystemProperties ()))
147
+ .setUserProperties (asProperties (context .getUserProperties ()))
148
+ .setTwoPhaseBuilding (true )
149
+ .setValidationLevel (ModelBuildingRequest .VALIDATION_LEVEL_MINIMAL );
150
+
151
+ interpolator
152
+ .interpolateModel (model , context .getProjectDirectory (), mbr , problem -> {})
153
+ .getProfiles ();
154
+
155
+ return model .getProfiles ().stream ().collect (Collectors .toMap (Profile ::getId , UnaryOperator .identity ()));
156
+ }
157
+
92
158
private boolean isActive (Profile profile , ProfileActivationContext context , ModelProblemCollector problems ) {
93
159
boolean isActive = false ;
94
160
for (ProfileActivator activator : activators ) {
0 commit comments