1 package org.apache.maven.plugins.enforcer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
26 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
27 import org.apache.maven.model.Profile;
28 import org.apache.maven.project.MavenProject;
29 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
30 import org.codehaus.plexus.util.StringUtils;
31
32
33
34
35
36
37 public class RequireActiveProfile
38 extends AbstractNonCacheableEnforcerRule
39 {
40
41
42 public String profiles = null;
43
44
45 public boolean all = true;
46
47
48
49
50
51
52 public void execute( EnforcerRuleHelper theHelper )
53 throws EnforcerRuleException
54 {
55 List<String> missingProfiles = new ArrayList<String>();
56 try
57 {
58 MavenProject project = (MavenProject) theHelper.evaluate( "${project}" );
59 if ( StringUtils.isNotEmpty( profiles ) )
60 {
61 String[] profs = profiles.split( "," );
62 for ( String profile : profs )
63 {
64 if ( !isProfileActive( project, profile ) )
65 {
66 missingProfiles.add( profile );
67 }
68 }
69
70 boolean fail = false;
71 if ( !missingProfiles.isEmpty() )
72 {
73 fail = true;
74
75
76
77
78
79
80
81
82
83
84
85 }
86
87 if ( fail )
88 {
89 StringBuilder buf = new StringBuilder();
90 if ( message != null )
91 {
92 buf.append( message + "\n" );
93 }
94
95 for ( String profile : missingProfiles )
96 {
97 buf.append( "Profile \"" + profile + "\" is not activated.\n" );
98 }
99
100 throw new EnforcerRuleException( buf.toString() );
101 }
102
103 }
104
105 }
106 catch ( ExpressionEvaluationException e )
107 {
108 throw new EnforcerRuleException( "Unable to retrieve the project.", e );
109 }
110
111 }
112
113
114
115
116
117
118
119
120 protected boolean isProfileActive( MavenProject project, String profileName )
121 {
122 @SuppressWarnings( "unchecked" )
123 List<Profile> activeProfiles = project.getActiveProfiles();
124 if ( activeProfiles != null && !activeProfiles.isEmpty() )
125 {
126 for ( Profile profile : activeProfiles )
127 {
128 if ( profile.getId().equals( profileName ) )
129 {
130 return true;
131 }
132 }
133 }
134
135 return false;
136 }
137 }