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