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