001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.enforcer.rules;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Objects;
027
028import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
029import org.apache.maven.execution.MavenSession;
030import org.apache.maven.project.MavenProject;
031import org.codehaus.plexus.util.StringUtils;
032
033/**
034 * Ensure that all profiles mentioned on the commandline do exist.
035 *
036 * @author Robert Scholte
037 * @author Gabriel Belingueres
038 */
039@Named("requireProfileIdsExist")
040public final class RequireProfileIdsExist extends AbstractStandardEnforcerRule {
041
042    private final MavenSession session;
043
044    @Inject
045    public RequireProfileIdsExist(MavenSession session) {
046        this.session = Objects.requireNonNull(session);
047    }
048
049    @Override
050    public void execute() throws EnforcerRuleException {
051
052        List<String> profileIds = new ArrayList<>();
053        profileIds.addAll(session.getProjectBuildingRequest().getActiveProfileIds());
054        profileIds.addAll(session.getProjectBuildingRequest().getInactiveProfileIds());
055
056        for (MavenProject project : session.getProjects()) {
057            // iterate over all parents
058            MavenProject currentProject = project;
059            do {
060                for (org.apache.maven.model.Profile profile :
061                        currentProject.getModel().getProfiles()) {
062                    profileIds.remove(profile.getId());
063
064                    if (profileIds.isEmpty()) {
065                        return;
066                    }
067                }
068
069                currentProject = currentProject.getParent();
070            } while (currentProject != null);
071        }
072
073        for (org.apache.maven.settings.Profile profile : session.getSettings().getProfiles()) {
074            profileIds.remove(profile.getId());
075        }
076
077        if (profileIds.isEmpty()) {
078            return;
079        }
080
081        StringBuilder sb = new StringBuilder();
082        if (profileIds.size() > 1) {
083            sb.append("The requested profiles don't exist: ");
084        } else {
085            sb.append("The requested profile doesn't exist: ");
086        }
087        sb.append(StringUtils.join(profileIds.iterator(), ", "));
088
089        throw new EnforcerRuleException(sb.toString());
090    }
091}