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