View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.execution;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.artifact.ArtifactUtils;
27  import org.apache.maven.plugin.descriptor.PluginDescriptor;
28  import org.apache.maven.project.DuplicateProjectException;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.project.ProjectSorter;
31  import org.codehaus.plexus.util.dag.CycleDetectedException;
32  
33  /**
34   * ReactorManager - unused
35   */
36  @Deprecated
37  public class ReactorManager {
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      public static final String MAKE_MODE = "make";
45  
46      public static final String MAKE_DEPENDENTS_MODE = "make-dependents";
47  
48      // make projects that depend on me, and projects that I depend on
49      public static final String MAKE_BOTH_MODE = "make-both";
50  
51      private List<String> blackList = new ArrayList<>();
52  
53      private Map<String, BuildFailure> buildFailuresByProject = new HashMap<>();
54  
55      private Map<String, Map<String, Map>> pluginContextsByProjectAndPluginKey = new HashMap<>();
56  
57      private String failureBehavior = FAIL_FAST;
58  
59      private final ProjectSorter sorter;
60  
61      private Map<String, BuildSuccess> buildSuccessesByProject = new HashMap<>();
62  
63      public ReactorManager(List<MavenProject> projects) throws CycleDetectedException, DuplicateProjectException {
64          this.sorter = new ProjectSorter(projects);
65      }
66  
67      public Map getPluginContext(PluginDescriptor plugin, MavenProject project) {
68          Map<String, Map> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get(project.getId());
69  
70          if (pluginContextsByKey == null) {
71              pluginContextsByKey = new HashMap<>();
72              pluginContextsByProjectAndPluginKey.put(project.getId(), pluginContextsByKey);
73          }
74  
75          Map pluginContext = pluginContextsByKey.get(plugin.getPluginLookupKey());
76  
77          if (pluginContext == null) {
78              pluginContext = new HashMap<>();
79              pluginContextsByKey.put(plugin.getPluginLookupKey(), pluginContext);
80          }
81  
82          return pluginContext;
83      }
84  
85      public void setFailureBehavior(String failureBehavior) {
86          if (failureBehavior == null) {
87              this.failureBehavior = FAIL_FAST; // default
88              return;
89          }
90          if (FAIL_FAST.equals(failureBehavior)
91                  || FAIL_AT_END.equals(failureBehavior)
92                  || FAIL_NEVER.equals(failureBehavior)) {
93              this.failureBehavior = failureBehavior;
94          } else {
95              throw new IllegalArgumentException("Invalid failure behavior (must be one of: \'" + FAIL_FAST + "\', \'"
96                      + FAIL_AT_END + "\', \'" + FAIL_NEVER + "\').");
97          }
98      }
99  
100     public String getFailureBehavior() {
101         return failureBehavior;
102     }
103 
104     public void blackList(MavenProject project) {
105         blackList(getProjectKey(project));
106     }
107 
108     private void blackList(String id) {
109         if (!blackList.contains(id)) {
110             blackList.add(id);
111 
112             List<String> dependents = sorter.getDependents(id);
113 
114             if (dependents != null && !dependents.isEmpty()) {
115                 for (String dependentId : dependents) {
116                     if (!buildSuccessesByProject.containsKey(dependentId)
117                             && !buildFailuresByProject.containsKey(dependentId)) {
118                         blackList(dependentId);
119                     }
120                 }
121             }
122         }
123     }
124 
125     public boolean isBlackListed(MavenProject project) {
126         return blackList.contains(getProjectKey(project));
127     }
128 
129     private static String getProjectKey(MavenProject project) {
130         return ArtifactUtils.versionlessKey(project.getGroupId(), project.getArtifactId());
131     }
132 
133     public void registerBuildFailure(MavenProject project, Exception error, String task, long time) {
134         buildFailuresByProject.put(getProjectKey(project), new BuildFailure(project, time, error));
135     }
136 
137     public boolean hasBuildFailures() {
138         return !buildFailuresByProject.isEmpty();
139     }
140 
141     public boolean hasBuildFailure(MavenProject project) {
142         return buildFailuresByProject.containsKey(getProjectKey(project));
143     }
144 
145     public boolean hasMultipleProjects() {
146         return sorter.hasMultipleProjects();
147     }
148 
149     public List<MavenProject> getSortedProjects() {
150         return sorter.getSortedProjects();
151     }
152 
153     public boolean hasBuildSuccess(MavenProject project) {
154         return buildSuccessesByProject.containsKey(getProjectKey(project));
155     }
156 
157     public void registerBuildSuccess(MavenProject project, long time) {
158         buildSuccessesByProject.put(getProjectKey(project), new BuildSuccess(project, time));
159     }
160 
161     public BuildFailure getBuildFailure(MavenProject project) {
162         return buildFailuresByProject.get(getProjectKey(project));
163     }
164 
165     public BuildSuccess getBuildSuccess(MavenProject project) {
166         return buildSuccessesByProject.get(getProjectKey(project));
167     }
168 
169     public boolean executedMultipleProjects() {
170         return buildFailuresByProject.size() + buildSuccessesByProject.size() > 1;
171     }
172 }