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;
031
032/**
033 * Ensure that all profiles mentioned on the commandline do exist.
034 *
035 * @author Robert Scholte
036 * @author Gabriel Belingueres
037 */
038@Named("requireProfileIdsExist")
039public final class RequireProfileIdsExist extends AbstractStandardEnforcerRule {
040
041    private final MavenSession session;
042
043    @Inject
044    public RequireProfileIdsExist(MavenSession session) {
045        this.session = Objects.requireNonNull(session);
046    }
047
048    @Override
049    public void execute() throws EnforcerRuleException {
050
051        List<String> profileIds = new ArrayList<>();
052        profileIds.addAll(session.getProjectBuildingRequest().getActiveProfileIds());
053        profileIds.addAll(session.getProjectBuildingRequest().getInactiveProfileIds());
054
055        for (MavenProject project : session.getProjects()) {
056            // iterate over all parents
057            MavenProject currentProject = project;
058            do {
059                for (org.apache.maven.model.Profile profile :
060                        currentProject.getModel().getProfiles()) {
061                    profileIds.remove(profile.getId());
062
063                    if (profileIds.isEmpty()) {
064                        return;
065                    }
066                }
067
068                currentProject = currentProject.getParent();
069            } while (currentProject != null);
070        }
071
072        for (org.apache.maven.settings.Profile profile : session.getSettings().getProfiles()) {
073            profileIds.remove(profile.getId());
074        }
075
076        if (profileIds.isEmpty()) {
077            return;
078        }
079
080        StringBuilder sb = new StringBuilder();
081        if (getMessage() != null) {
082            sb.append(getMessage()).append("\n");
083        }
084        if (profileIds.size() > 1) {
085            sb.append("The requested profiles don't exist: ");
086        } else {
087            sb.append("The requested profile doesn't exist: ");
088        }
089        sb.append(String.join(", ", profileIds));
090
091        throw new EnforcerRuleException(sb.toString());
092    }
093}