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  
23  import org.apache.maven.artifact.ArtifactUtils;
24  import org.apache.maven.plugin.descriptor.PluginDescriptor;
25  import org.apache.maven.project.DuplicateProjectException;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.project.ProjectSorter;
28  import org.codehaus.plexus.util.dag.CycleDetectedException;
29  
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Map;
35  
36  public class ReactorManager
37  {
38      public static final String FAIL_FAST = "fail-fast";
39  
40      public static final String FAIL_AT_END = "fail-at-end";
41  
42      public static final String FAIL_NEVER = "fail-never";
43  
44      private List blackList = new ArrayList();
45  
46      private Map buildFailuresByProject = new HashMap();
47  
48      private Map pluginContextsByProjectAndPluginKey = new HashMap();
49  
50      private String failureBehavior = FAIL_FAST;
51  
52      private final ProjectSorter sorter;
53  
54      private Map buildSuccessesByProject = new HashMap();
55  
56      public ReactorManager( List projects )
57          throws CycleDetectedException, DuplicateProjectException
58      {
59          this.sorter = new ProjectSorter( projects );
60      }
61  
62      public Map getPluginContext( PluginDescriptor plugin, MavenProject project )
63      {
64          Map pluginContextsByKey = (Map) pluginContextsByProjectAndPluginKey.get( project.getId() );
65  
66          if ( pluginContextsByKey == null )
67          {
68              pluginContextsByKey = new HashMap();
69              pluginContextsByProjectAndPluginKey.put( project.getId(), pluginContextsByKey );
70          }
71  
72          Map pluginContext = (Map) pluginContextsByKey.get( plugin.getPluginLookupKey() );
73  
74          if ( pluginContext == null )
75          {
76              pluginContext = new HashMap();
77              pluginContextsByKey.put( plugin.getPluginLookupKey(), pluginContext );
78          }
79  
80          return pluginContext;
81      }
82  
83      public void setFailureBehavior( String failureBehavior )
84      {
85          if ( FAIL_FAST.equals( failureBehavior ) || FAIL_AT_END.equals( failureBehavior ) ||
86              FAIL_NEVER.equals( failureBehavior ) )
87          {
88              this.failureBehavior = failureBehavior;
89          }
90          else
91          {
92              throw new IllegalArgumentException( "Invalid failure behavior (must be one of: \'" + FAIL_FAST + "\', \'" +
93                  FAIL_AT_END + "\', \'" + FAIL_NEVER + "\')." );
94          }
95      }
96  
97      public String getFailureBehavior()
98      {
99          return failureBehavior;
100     }
101 
102     public void blackList( MavenProject project )
103     {
104         blackList( getProjectKey( project ) );
105     }
106 
107     private void blackList( String id )
108     {
109         if ( !blackList.contains( id ) )
110         {
111             blackList.add( id );
112 
113             List dependents = sorter.getDependents( id );
114 
115             if ( dependents != null && !dependents.isEmpty() )
116             {
117                 for ( Iterator it = dependents.iterator(); it.hasNext(); )
118                 {
119                     String dependentId = (String) it.next();
120 
121                     if ( !buildSuccessesByProject.containsKey( dependentId ) &&
122                         !buildFailuresByProject.containsKey( dependentId ) )
123                     {
124                         blackList( dependentId );
125                     }
126                 }
127             }
128         }
129     }
130 
131     public boolean isBlackListed( MavenProject project )
132     {
133         return blackList.contains( getProjectKey( project ) );
134     }
135 
136     private static String getProjectKey( MavenProject project )
137     {
138         return ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
139     }
140 
141     public void registerBuildFailure( MavenProject project, Exception error, String task, long time )
142     {
143         buildFailuresByProject.put( getProjectKey( project ), new BuildFailure( error, task, time ) );
144     }
145 
146     public boolean hasBuildFailures()
147     {
148         return !buildFailuresByProject.isEmpty();
149     }
150 
151     public boolean hasBuildFailure( MavenProject project )
152     {
153         return buildFailuresByProject.containsKey( getProjectKey( project ) );
154     }
155 
156     public boolean hasMultipleProjects()
157     {
158         return sorter.hasMultipleProjects();
159     }
160 
161     public List getSortedProjects()
162     {
163         return sorter.getSortedProjects();
164     }
165 
166     public MavenProject getTopLevelProject()
167     {
168         return sorter.getTopLevelProject();
169     }
170 
171     public boolean hasBuildSuccess( MavenProject project )
172     {
173         return buildSuccessesByProject.containsKey( getProjectKey( project ) );
174     }
175 
176     public void registerBuildSuccess( MavenProject project, long time )
177     {
178         buildSuccessesByProject.put( getProjectKey( project ), new BuildSuccess( project, time ) );
179     }
180 
181     public BuildFailure getBuildFailure( MavenProject project )
182     {
183         return (BuildFailure) buildFailuresByProject.get( getProjectKey( project ) );
184     }
185 
186     public BuildSuccess getBuildSuccess( MavenProject project )
187     {
188         return (BuildSuccess) buildSuccessesByProject.get( getProjectKey( project ) );
189     }
190 
191     public boolean executedMultipleProjects()
192     {
193         return buildFailuresByProject.size() + buildSuccessesByProject.size() > 1;
194     }
195 }