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.mapping;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import static java.util.function.Function.identity;
27  import static java.util.stream.Collectors.toMap;
28  
29  /**
30   * DefaultLifecycleMapping
31   */
32  public class DefaultLifecycleMapping implements LifecycleMapping {
33  
34      private List<Lifecycle> lifecycles;
35  
36      private Map<String, Lifecycle> lifecycleMap;
37  
38      /** @deprecated use lifecycles instead */
39      private Map<String, LifecyclePhase> phases;
40  
41      /**
42       * Default ctor for plexus compatibility: lifecycles are most commonly defined in Plexus XML, that does field
43       * injection. Still, for Plexus to be able to instantiate this class, default ctor is needed.
44       *
45       * @deprecated Should not be used in Java code.
46       */
47      @Deprecated
48      public DefaultLifecycleMapping() {}
49  
50      /**
51       * Ctor to be used in Java code/providers.
52       */
53      public DefaultLifecycleMapping(final List<Lifecycle> lifecycles) {
54          this.lifecycleMap =
55                  Collections.unmodifiableMap(lifecycles.stream().collect(toMap(Lifecycle::getId, identity())));
56      }
57  
58      /**
59       * Plexus: Populates the lifecycle map from the injected list of lifecycle mappings (if not already done).
60       */
61      private void initLifecycleMap() {
62          if (lifecycleMap == null) {
63              lifecycleMap = new HashMap<>();
64  
65              if (lifecycles != null) {
66                  for (Lifecycle lifecycle : lifecycles) {
67                      lifecycleMap.put(lifecycle.getId(), lifecycle);
68                  }
69              } else {
70                  /*
71                   * NOTE: This is to provide a migration path for implementors of the legacy API which did not know about
72                   * getLifecycles().
73                   */
74  
75                  String[] lifecycleIds = {"default", "clean", "site"};
76  
77                  for (String lifecycleId : lifecycleIds) {
78                      Map<String, LifecyclePhase> phases = getLifecyclePhases(lifecycleId);
79                      if (phases != null) {
80                          Lifecycle lifecycle = new Lifecycle();
81  
82                          lifecycle.setId(lifecycleId);
83                          lifecycle.setLifecyclePhases(phases);
84  
85                          lifecycleMap.put(lifecycleId, lifecycle);
86                      }
87                  }
88              }
89          }
90      }
91  
92      @Override
93      public Map<String, Lifecycle> getLifecycles() {
94          initLifecycleMap();
95  
96          return lifecycleMap;
97      }
98  
99      @Deprecated
100     @Override
101     public List<String> getOptionalMojos(String lifecycle) {
102         return null;
103     }
104 
105     private Map<String, LifecyclePhase> getLifecyclePhases(String lifecycle) {
106         initLifecycleMap();
107 
108         Lifecycle lifecycleMapping = lifecycleMap.get(lifecycle);
109 
110         if (lifecycleMapping != null) {
111             return lifecycleMapping.getLifecyclePhases();
112         } else if ("default".equals(lifecycle)) {
113             return phases;
114         } else {
115             return null;
116         }
117     }
118 
119     @Deprecated
120     @Override
121     public Map<String, String> getPhases(String lifecycle) {
122         return LifecyclePhase.toLegacyMap(getLifecyclePhases(lifecycle));
123     }
124 }