View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Objects;
27  
28  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * Ensure that all profiles mentioned on the commandline do exist.
35   *
36   * @author Robert Scholte
37   * @author Gabriel Belingueres
38   */
39  @Named("requireProfileIdsExist")
40  public final class RequireProfileIdsExist extends AbstractStandardEnforcerRule {
41  
42      private final MavenSession session;
43  
44      @Inject
45      public RequireProfileIdsExist(MavenSession session) {
46          this.session = Objects.requireNonNull(session);
47      }
48  
49      @Override
50      public void execute() throws EnforcerRuleException {
51  
52          List<String> profileIds = new ArrayList<>();
53          profileIds.addAll(session.getProjectBuildingRequest().getActiveProfileIds());
54          profileIds.addAll(session.getProjectBuildingRequest().getInactiveProfileIds());
55  
56          for (MavenProject project : session.getProjects()) {
57              // iterate over all parents
58              MavenProject currentProject = project;
59              do {
60                  for (org.apache.maven.model.Profile profile :
61                          currentProject.getModel().getProfiles()) {
62                      profileIds.remove(profile.getId());
63  
64                      if (profileIds.isEmpty()) {
65                          return;
66                      }
67                  }
68  
69                  currentProject = currentProject.getParent();
70              } while (currentProject != null);
71          }
72  
73          for (org.apache.maven.settings.Profile profile : session.getSettings().getProfiles()) {
74              profileIds.remove(profile.getId());
75          }
76  
77          if (profileIds.isEmpty()) {
78              return;
79          }
80  
81          StringBuilder sb = new StringBuilder();
82          if (profileIds.size() > 1) {
83              sb.append("The requested profiles don't exist: ");
84          } else {
85              sb.append("The requested profile doesn't exist: ");
86          }
87          sb.append(StringUtils.join(profileIds.iterator(), ", "));
88  
89          throw new EnforcerRuleException(sb.toString());
90      }
91  }