001package org.apache.maven.execution;
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 org.apache.maven.artifact.ArtifactUtils;
023import org.apache.maven.plugin.descriptor.PluginDescriptor;
024import org.apache.maven.project.DuplicateProjectException;
025import org.apache.maven.project.MavenProject;
026import org.apache.maven.project.ProjectSorter;
027import org.codehaus.plexus.util.dag.CycleDetectedException;
028
029import java.util.ArrayList;
030import java.util.HashMap;
031import java.util.List;
032import java.util.Map;
033
034@Deprecated
035public class ReactorManager
036{
037    public static final String FAIL_FAST = "fail-fast";
038
039    public static final String FAIL_AT_END = "fail-at-end";
040
041    public static final String FAIL_NEVER = "fail-never";
042
043    public static final String MAKE_MODE = "make";
044
045    public static final String MAKE_DEPENDENTS_MODE = "make-dependents";
046
047    // make projects that depend on me, and projects that I depend on
048    public static final String MAKE_BOTH_MODE = "make-both";
049
050    private List<String> blackList = new ArrayList<>();
051
052    private Map<String, BuildFailure> buildFailuresByProject = new HashMap<>();
053
054    private Map<String, Map<String, Map>> pluginContextsByProjectAndPluginKey = new HashMap<>();
055
056    private String failureBehavior = FAIL_FAST;
057
058    private final ProjectSorter sorter;
059
060    private Map<String, BuildSuccess> buildSuccessesByProject = new HashMap<>();
061
062    public ReactorManager( List<MavenProject> projects )
063        throws CycleDetectedException, DuplicateProjectException
064    {
065        this.sorter = new ProjectSorter( projects );
066    }
067
068    public Map getPluginContext( PluginDescriptor plugin, MavenProject project )
069    {
070        Map<String, Map> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( project.getId() );
071
072        if ( pluginContextsByKey == null )
073        {
074            pluginContextsByKey = new HashMap<>();
075            pluginContextsByProjectAndPluginKey.put( project.getId(), pluginContextsByKey );
076        }
077
078        Map pluginContext = pluginContextsByKey.get( plugin.getPluginLookupKey() );
079
080        if ( pluginContext == null )
081        {
082            pluginContext = new HashMap<>();
083            pluginContextsByKey.put( plugin.getPluginLookupKey(), pluginContext );
084        }
085
086        return pluginContext;
087    }
088
089    public void setFailureBehavior( String failureBehavior )
090    {
091        if ( failureBehavior == null )
092        {
093            this.failureBehavior = FAIL_FAST; // default
094            return;
095        }
096        if ( FAIL_FAST.equals( failureBehavior ) || FAIL_AT_END.equals( failureBehavior ) || FAIL_NEVER.equals(
097            failureBehavior ) )
098        {
099            this.failureBehavior = failureBehavior;
100        }
101        else
102        {
103            throw new IllegalArgumentException(
104                "Invalid failure behavior (must be one of: \'" + FAIL_FAST + "\', \'" + FAIL_AT_END + "\', \'"
105                    + FAIL_NEVER + "\')." );
106        }
107    }
108
109    public String getFailureBehavior()
110    {
111        return failureBehavior;
112    }
113
114    public void blackList( MavenProject project )
115    {
116        blackList( getProjectKey( project ) );
117    }
118
119    private void blackList( String id )
120    {
121        if ( !blackList.contains( id ) )
122        {
123            blackList.add( id );
124
125            List<String> dependents = sorter.getDependents( id );
126
127            if ( dependents != null && !dependents.isEmpty() )
128            {
129                for ( String dependentId : dependents )
130                {
131                    if ( !buildSuccessesByProject.containsKey( dependentId ) && !buildFailuresByProject.containsKey(
132                        dependentId ) )
133                    {
134                        blackList( dependentId );
135                    }
136                }
137            }
138        }
139    }
140
141    public boolean isBlackListed( MavenProject project )
142    {
143        return blackList.contains( getProjectKey( project ) );
144    }
145
146    private static String getProjectKey( MavenProject project )
147    {
148        return ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
149    }
150
151    public void registerBuildFailure( MavenProject project, Exception error, String task, long time )
152    {
153        buildFailuresByProject.put( getProjectKey( project ), new BuildFailure( project, time, error ) );
154    }
155
156    public boolean hasBuildFailures()
157    {
158        return !buildFailuresByProject.isEmpty();
159    }
160
161    public boolean hasBuildFailure( MavenProject project )
162    {
163        return buildFailuresByProject.containsKey( getProjectKey( project ) );
164    }
165
166    public boolean hasMultipleProjects()
167    {
168        return sorter.hasMultipleProjects();
169    }
170
171    public List<MavenProject> getSortedProjects()
172    {
173        return sorter.getSortedProjects();
174    }
175
176    public boolean hasBuildSuccess( MavenProject project )
177    {
178        return buildSuccessesByProject.containsKey( getProjectKey( project ) );
179    }
180
181    public void registerBuildSuccess( MavenProject project, long time )
182    {
183        buildSuccessesByProject.put( getProjectKey( project ), new BuildSuccess( project, time ) );
184    }
185
186    public BuildFailure getBuildFailure( MavenProject project )
187    {
188        return buildFailuresByProject.get( getProjectKey( project ) );
189    }
190
191    public BuildSuccess getBuildSuccess( MavenProject project )
192    {
193        return buildSuccessesByProject.get( getProjectKey( project ) );
194    }
195
196    public boolean executedMultipleProjects()
197    {
198        return buildFailuresByProject.size() + buildSuccessesByProject.size() > 1;
199    }
200}