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.execution.MavenSession;
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 RequireProfileIdsExist extends AbstractNonCacheableEnforcerRule
38 {
39 @Override
40 public void execute( EnforcerRuleHelper helper )
41 throws EnforcerRuleException
42 {
43 try
44 {
45 MavenSession session = (MavenSession) helper.evaluate( "${session}" );
46
47 List<String> profileIds = new ArrayList<String>();
48 profileIds.addAll( session.getProjectBuildingRequest().getActiveProfileIds() );
49 profileIds.addAll( session.getProjectBuildingRequest().getInactiveProfileIds() );
50
51 for ( MavenProject project : session.getProjects() )
52 {
53 for ( org.apache.maven.model.Profile profile : project.getModel().getProfiles() )
54 {
55 profileIds.remove( profile.getId() );
56
57 if ( profileIds.isEmpty() )
58 {
59 return;
60 }
61 }
62 }
63
64 for ( org.apache.maven.settings.Profile profile : session.getSettings().getProfiles() )
65 {
66 profileIds.remove( profile.getId() );
67
68 if ( profileIds.isEmpty() )
69 {
70 return;
71 }
72 }
73
74 StringBuilder sb = new StringBuilder();
75 if ( profileIds.size() > 1 )
76 {
77 sb.append( "The requested profiles don't exist: " );
78 }
79 else
80 {
81 sb.append( "The requested profile doesn't exist: " );
82 }
83 sb.append( StringUtils.join( profileIds.iterator(), ", " ) );
84
85 throw new EnforcerRuleException( sb.toString() );
86 }
87 catch ( ExpressionEvaluationException e )
88 {
89 throw new EnforcerRuleException( e.getMessage() );
90 }
91 }
92 }