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