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.lifecycle;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.stream.Collectors;
26  
27  import org.apache.maven.lifecycle.mapping.LifecyclePhase;
28  
29  /**
30   * Lifecycle definition, with eventual plugin bindings (when they are not packaging-specific).
31   */
32  public class Lifecycle {
33      public Lifecycle() {}
34  
35      public Lifecycle(String id, List<String> phases, Map<String, LifecyclePhase> defaultPhases) {
36          this.id = id;
37          this.phases = phases;
38          this.defaultPhases = defaultPhases;
39      }
40  
41      public Lifecycle(
42              org.apache.maven.api.services.LifecycleRegistry registry, org.apache.maven.api.Lifecycle lifecycle) {
43          this.lifecycle = lifecycle;
44          this.id = lifecycle.id();
45          this.phases = registry.computePhases(lifecycle);
46          this.defaultPhases = getDefaultPhases(lifecycle);
47      }
48  
49      // <lifecycle>
50      //   <id>clean</id>
51      //   <phases>
52      //     <phase>pre-clean</phase>
53      //     <phase>clean</phase>
54      //     <phase>post-clean</phase>
55      //   </phases>
56      //   <default-phases>
57      //     <clean>org.apache.maven.plugins:maven-clean-plugin:clean</clean>
58      //   </default-phases>
59      // </lifecycle>
60  
61      private String id;
62  
63      private List<String> phases;
64  
65      private Map<String, LifecyclePhase> defaultPhases;
66  
67      private org.apache.maven.api.Lifecycle lifecycle;
68  
69      public String getId() {
70          return id;
71      }
72  
73      public org.apache.maven.api.Lifecycle getDelegate() {
74          return lifecycle;
75      }
76  
77      public List<String> getPhases() {
78          return phases;
79      }
80  
81      static Map<String, LifecyclePhase> getDefaultPhases(org.apache.maven.api.Lifecycle lifecycle) {
82          Map<String, List<String>> goals = new HashMap<>();
83          lifecycle.allPhases().forEach(phase -> phase.plugins()
84                  .forEach(plugin -> plugin.getExecutions().forEach(exec -> exec.getGoals()
85                          .forEach(goal -> goals.computeIfAbsent(phase.name(), n -> new ArrayList<>())
86                                  .add(plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion()
87                                          + ":" + goal)))));
88          return goals.entrySet().stream()
89                  .collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePhase(String.join(",", e.getValue()))));
90      }
91  
92      public Map<String, LifecyclePhase> getDefaultLifecyclePhases() {
93          return defaultPhases;
94      }
95  
96      @Deprecated
97      public Map<String, String> getDefaultPhases() {
98          return LifecyclePhase.toLegacyMap(getDefaultLifecyclePhases());
99      }
100 
101     @Override
102     public String toString() {
103         return id + " -> "
104                 + lifecycle
105                         .allPhases()
106                         .map(org.apache.maven.api.Lifecycle.Phase::name)
107                         .collect(Collectors.joining(", ", "[", "]"));
108     }
109 }