View Javadoc
1   /*
2    * Copyright (C) 2012 Jean-Christophe Gay (contact@jeanchristophegay.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.buildplan;
17  
18  import static org.codehaus.mojo.buildplan.util.Objects.firstNonNull;
19  import static org.codehaus.plexus.util.StringUtils.isEmpty;
20  
21  import java.util.Collections;
22  import java.util.List;
23  import org.apache.maven.lifecycle.DefaultLifecycles;
24  import org.apache.maven.lifecycle.Lifecycle;
25  import org.apache.maven.plugin.MojoExecution;
26  import org.codehaus.mojo.buildplan.util.LinkedMultimap;
27  import org.codehaus.mojo.buildplan.util.Multimap;
28  import org.codehaus.plexus.logging.console.ConsoleLogger;
29  
30  public class Groups {
31  
32      public static class ByPlugin {
33  
34          public static Multimap<String, MojoExecution> of(List<MojoExecution> executions) {
35              return of(executions, null);
36          }
37  
38          public static Multimap<String, MojoExecution> of(List<MojoExecution> executions, String artifactIdFilter) {
39              Multimap<String, MojoExecution> result = new LinkedMultimap<>();
40              boolean notFiltering = isEmpty(artifactIdFilter);
41              for (MojoExecution execution : executions) {
42                  if (notFiltering || execution.getArtifactId().equalsIgnoreCase(artifactIdFilter)) {
43                      result.put(execution.getArtifactId(), execution);
44                  }
45              }
46              return result;
47          }
48      }
49  
50      public static class ByPhase {
51  
52          public static Multimap<String, MojoExecution> of(List<MojoExecution> plan) {
53              return of(plan, new Options(defaultLifecycles()));
54          }
55  
56          public static Multimap<String, MojoExecution> of(List<MojoExecution> executions, String phaseFilter) {
57              return of(executions, new Options(defaultLifecycles()).forPhase(phaseFilter));
58          }
59  
60          public static Multimap<String, MojoExecution> of(List<MojoExecution> executions, Options options) {
61              Multimap<String, MojoExecution> result = new LinkedMultimap<>();
62              boolean notFiltering = isEmpty(options.phase);
63              for (MojoExecution execution : executions) {
64                  String phase = firstNonNull(execution.getLifecyclePhase(), "<no phase>");
65                  if (options.showingAllPhases) {
66                      Lifecycle lifecycle = options.defaultLifecycles.get(phase);
67                      if (lifecycle != null) {
68                          lifecycle
69                                  .getPhases()
70                                  .forEach(defaultPhase -> result.put(defaultPhase, NoMojoExecution.INSTANCE));
71                      }
72                  }
73                  if (notFiltering || phase.equalsIgnoreCase(options.phase)) {
74                      result.put(phase, execution);
75                  }
76              }
77              return result;
78          }
79  
80          private static DefaultLifecycles defaultLifecycles() {
81              return new DefaultLifecycles(Collections.emptyMap(), new ConsoleLogger());
82          }
83      }
84  
85      public static class Options {
86  
87          private final DefaultLifecycles defaultLifecycles;
88  
89          private String phase;
90          private boolean showingAllPhases;
91          private boolean showingLifecycles;
92  
93          public Options(DefaultLifecycles defaultLifecycles) {
94              this.defaultLifecycles = defaultLifecycles;
95          }
96  
97          public Options forPhase(String phase) {
98              this.phase = phase;
99              return this;
100         }
101 
102         public Options showingAllPhases() {
103             this.showingAllPhases = true;
104             return this;
105         }
106 
107         public Options showingLifecycles() {
108             this.showingLifecycles = true;
109             return this;
110         }
111     }
112 }