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 javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.HashMap;
28  import java.util.LinkedHashMap;
29  import java.util.LinkedHashSet;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Set;
33  import java.util.stream.Collectors;
34  
35  import org.apache.maven.lifecycle.providers.lifecycle.CleanLifecycleProvider;
36  import org.apache.maven.lifecycle.providers.lifecycle.DefaultLifecycleProvider;
37  import org.apache.maven.lifecycle.providers.lifecycle.SiteLifecycleProvider;
38  import org.codehaus.plexus.logging.Logger;
39  import org.codehaus.plexus.util.StringUtils;
40  
41  /**
42   * @since 3.0
43   * @author Jason van Zyl
44   * @author Kristian Rosenvold
45   */
46  // TODO The configuration for the lifecycle needs to be externalized so that I can use the annotations properly for the
47  // wiring and reference and external source for the lifecycle configuration.
48  @Singleton
49  @Named
50  public class DefaultLifecycles {
51      private static final List<String> BUILT_IN_LIFECYCLES =
52              Arrays.asList(CleanLifecycleProvider.NAME, DefaultLifecycleProvider.NAME, SiteLifecycleProvider.NAME);
53  
54      /**
55       * Java System Property, as it must be "early discovered", that disables site lifecycle.
56       */
57      private static final String CONFIG_PROP_SITE_DISABLED = "maven.site.lifecycle.disabled";
58  
59      /**
60       * @deprecated Use {@link #getStandardLifecycles()} instead.
61       */
62      @Deprecated
63      public static final String[] STANDARD_LIFECYCLES = {
64          CleanLifecycleProvider.NAME, DefaultLifecycleProvider.NAME, SiteLifecycleProvider.NAME
65      };
66  
67      @Inject
68      private Map<String, Lifecycle> lifecycles;
69  
70      @Inject
71      private Logger logger;
72  
73      public DefaultLifecycles() {}
74  
75      public DefaultLifecycles(Map<String, Lifecycle> lifecycles, Logger logger) {
76          this.lifecycles = lifecycles;
77          this.logger = logger;
78      }
79  
80      private boolean isEnabled(String lifecycleId) {
81          if (SiteLifecycleProvider.NAME.equals(lifecycleId)) {
82              return !Boolean.getBoolean(CONFIG_PROP_SITE_DISABLED);
83          } else {
84              return true;
85          }
86      }
87  
88      public List<String> getStandardLifecycles() {
89          return BUILT_IN_LIFECYCLES.stream().filter(this::isEnabled).collect(Collectors.toList());
90      }
91  
92      public Lifecycle get(String key) {
93          return getPhaseToLifecycleMap().get(key);
94      }
95  
96      /**
97       * We use this to map all phases to the lifecycle that contains it. This is used so that a user can specify the
98       * phase they want to execute and we can easily determine what lifecycle we need to run.
99       *
100      * @return A map of lifecycles, indexed on id
101      */
102     public Map<String, Lifecycle> getPhaseToLifecycleMap() {
103         // If people are going to make their own lifecycles then we need to tell people how to namespace them correctly
104         // so that they don't interfere with internally defined lifecycles.
105 
106         HashMap<String, Lifecycle> phaseToLifecycleMap = new HashMap<>();
107 
108         for (Lifecycle lifecycle : getLifeCycles()) {
109             if (logger.isDebugEnabled()) {
110                 logger.debug("Lifecycle " + lifecycle);
111             }
112 
113             for (String phase : lifecycle.getPhases()) {
114                 // The first definition wins.
115                 if (!phaseToLifecycleMap.containsKey(phase)) {
116                     phaseToLifecycleMap.put(phase, lifecycle);
117                 } else {
118                     Lifecycle original = phaseToLifecycleMap.get(phase);
119                     logger.warn("Duplicated lifecycle phase " + phase + ". Defined in " + original.getId()
120                             + " but also in " + lifecycle.getId());
121                 }
122             }
123         }
124 
125         return phaseToLifecycleMap;
126     }
127 
128     /**
129      * Returns an ordered list of lifecycles
130      */
131     public List<Lifecycle> getLifeCycles() {
132         // ensure canonical order of standard lifecycles
133 
134         // take "snapshot": will filter by visibility
135         Map<String, Lifecycle> lifecycles = new LinkedHashMap<>(this.lifecycles);
136         // apply enabled
137         lifecycles.entrySet().removeIf(e -> !isEnabled(e.getKey()));
138 
139         LinkedHashSet<String> lifecycleNames = new LinkedHashSet<>(getStandardLifecycles());
140         lifecycleNames.addAll(lifecycles.keySet());
141 
142         ArrayList<Lifecycle> result = new ArrayList<>();
143         for (String name : lifecycleNames) {
144             Lifecycle lifecycle = lifecycles.get(name);
145             // defensive check lifecycleNames is subset (or equal) of lifecycles.keySet
146             if (lifecycle == null || lifecycle.getId() == null) {
147                 throw new NullPointerException("A lifecycle must be non-null and must have an id.");
148             }
149             result.add(lifecycle);
150         }
151 
152         return result;
153     }
154 
155     public String getLifecyclePhaseList() {
156         Set<String> phases = new LinkedHashSet<>();
157 
158         for (Lifecycle lifecycle : getLifeCycles()) {
159             phases.addAll(lifecycle.getPhases());
160         }
161 
162         return StringUtils.join(phases.iterator(), ", ");
163     }
164 }