View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Ensure that all profiles mentioned on the commandline do exist. 
34   * 
35   * @author Robert Scholte
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  }