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