View Javadoc
1   package org.apache.maven.execution;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.ArtifactUtils;
23  import org.apache.maven.plugin.descriptor.PluginDescriptor;
24  import org.apache.maven.project.DuplicateProjectException;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.project.ProjectSorter;
27  import org.codehaus.plexus.util.dag.CycleDetectedException;
28  
29  import java.util.ArrayList;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  @Deprecated
35  public class ReactorManager
36  {
37      public static final String FAIL_FAST = "fail-fast";
38  
39      public static final String FAIL_AT_END = "fail-at-end";
40  
41      public static final String FAIL_NEVER = "fail-never";
42  
43      public static final String MAKE_MODE = "make";
44  
45      public static final String MAKE_DEPENDENTS_MODE = "make-dependents";
46  
47      // make projects that depend on me, and projects that I depend on
48      public static final String MAKE_BOTH_MODE = "make-both";
49  
50      private List<String> blackList = new ArrayList<>();
51  
52      private Map<String, BuildFailure> buildFailuresByProject = new HashMap<>();
53  
54      private Map<String, Map<String, Map>> pluginContextsByProjectAndPluginKey = new HashMap<>();
55  
56      private String failureBehavior = FAIL_FAST;
57  
58      private final ProjectSorter sorter;
59  
60      private Map<String, BuildSuccess> buildSuccessesByProject = new HashMap<>();
61  
62      public ReactorManager( List<MavenProject> projects )
63          throws CycleDetectedException, DuplicateProjectException
64      {
65          this.sorter = new ProjectSorter( projects );
66      }
67  
68      public Map getPluginContext( PluginDescriptor plugin, MavenProject project )
69      {
70          Map<String, Map> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( project.getId() );
71  
72          if ( pluginContextsByKey == null )
73          {
74              pluginContextsByKey = new HashMap<>();
75              pluginContextsByProjectAndPluginKey.put( project.getId(), pluginContextsByKey );
76          }
77  
78          Map pluginContext = pluginContextsByKey.get( plugin.getPluginLookupKey() );
79  
80          if ( pluginContext == null )
81          {
82              pluginContext = new HashMap<>();
83              pluginContextsByKey.put( plugin.getPluginLookupKey(), pluginContext );
84          }
85  
86          return pluginContext;
87      }
88  
89      public void setFailureBehavior( String failureBehavior )
90      {
91          if ( failureBehavior == null )
92          {
93              this.failureBehavior = FAIL_FAST; // default
94              return;
95          }
96          if ( FAIL_FAST.equals( failureBehavior ) || FAIL_AT_END.equals( failureBehavior ) || FAIL_NEVER.equals(
97              failureBehavior ) )
98          {
99              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 }