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.Arrays;
26  import java.util.Comparator;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Objects;
31  import java.util.stream.Collectors;
32  
33  import org.apache.maven.api.services.LifecycleRegistry;
34  import org.apache.maven.api.services.Lookup;
35  import org.apache.maven.api.services.LookupException;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * @since 3.0
41   */
42  // TODO The configuration for the lifecycle needs to be externalized so that I can use the annotations properly for the
43  // wiring and reference and external source for the lifecycle configuration.
44  @Named
45  @Singleton
46  public class DefaultLifecycles {
47      public static final String[] STANDARD_LIFECYCLES = {"clean", "default", "site", "wrapper"};
48  
49      private final Logger logger = LoggerFactory.getLogger(getClass());
50  
51      // @Configuration(source="org/apache/maven/lifecycle/lifecycles.xml")
52  
53      private final Lookup lookup;
54  
55      private final LifecycleRegistry registry;
56  
57      private Map<String, Lifecycle> customLifecycles;
58  
59      public DefaultLifecycles() {
60          this.lookup = null;
61          this.registry = null;
62      }
63  
64      /**
65       * @deprecated Use {@link #DefaultLifecycles(LifecycleRegistry,Lookup)} instead
66       */
67      @Deprecated
68      public DefaultLifecycles(Map<String, Lifecycle> lifecycles, org.codehaus.plexus.logging.Logger logger) {
69          this.customLifecycles = lifecycles;
70          this.lookup = null;
71          this.registry = null;
72      }
73  
74      @Inject
75      public DefaultLifecycles(LifecycleRegistry registry, Lookup lookup) {
76          this.lookup = lookup;
77          this.registry = registry;
78      }
79  
80      /**
81       * Get lifecycle based on phase
82       *
83       * @param phase
84       * @return
85       */
86      public Lifecycle get(String phase) {
87          return getPhaseToLifecycleMap().get(phase);
88      }
89  
90      /**
91       * We use this to map all phases to the lifecycle that contains it. This is used so that a user can specify the
92       * phase they want to execute and we can easily determine what lifecycle we need to run.
93       *
94       * @return A map of lifecycles, indexed on id
95       */
96      public Map<String, Lifecycle> getPhaseToLifecycleMap() {
97          // If people are going to make their own lifecycles then we need to tell people how to namespace them correctly
98          // so that they don't interfere with internally defined lifecycles.
99  
100         Map<String, Lifecycle> phaseToLifecycleMap = new HashMap<>();
101 
102         for (Lifecycle lifecycle : getLifeCycles()) {
103             logger.debug("Lifecycle {}", lifecycle);
104 
105             for (String phase : lifecycle.getPhases()) {
106                 // The first definition wins.
107                 if (!phaseToLifecycleMap.containsKey(phase)) {
108                     phaseToLifecycleMap.put(phase, lifecycle);
109                 } else if (logger.isWarnEnabled()) {
110                     Lifecycle original = phaseToLifecycleMap.get(phase);
111                     logger.warn(
112                             "Duplicated lifecycle phase {}. Defined in {} but also in {}",
113                             phase,
114                             original.getId(),
115                             lifecycle.getId());
116                 }
117             }
118         }
119 
120         return phaseToLifecycleMap;
121     }
122 
123     /**
124      * Returns an ordered list of lifecycles
125      */
126     public List<Lifecycle> getLifeCycles() {
127         List<String> lifecycleIds = Arrays.asList(STANDARD_LIFECYCLES);
128 
129         Comparator<String> comparator = (l, r) -> {
130             int lx = lifecycleIds.indexOf(l);
131             int rx = lifecycleIds.indexOf(r);
132 
133             if (lx < 0 || rx < 0) {
134                 return rx - lx;
135             } else {
136                 return lx - rx;
137             }
138         };
139 
140         Map<String, Lifecycle> lifecyclesMap = lookupLifecycles();
141 
142         // ensure canonical order of standard lifecycles
143         return lifecyclesMap.values().stream()
144                 .peek(l -> Objects.requireNonNull(l.getId(), "A lifecycle must have an id."))
145                 .sorted(Comparator.comparing(Lifecycle::getId, comparator))
146                 .collect(Collectors.toList());
147     }
148 
149     private Map<String, Lifecycle> lookupLifecycles() {
150         // TODO: Remove the following code when maven-compat is gone
151         // This code is here to ensure maven-compat's EmptyLifecycleExecutor keeps on working.
152         if (lookup == null) {
153             return customLifecycles != null ? customLifecycles : new HashMap<>();
154         }
155 
156         // Lifecycles cannot be cached as extensions might add custom lifecycles later in the execution.
157         try {
158             return registry != null
159                     ? registry.stream().collect(Collectors.toMap(lf -> lf.id(), lf -> new Lifecycle(lf)))
160                     : Map.of();
161         } catch (LookupException e) {
162             throw new IllegalStateException("Unable to lookup lifecycles from the plexus container", e);
163         }
164     }
165 
166     public String getLifecyclePhaseList() {
167         return getLifeCycles().stream().flatMap(l -> l.getPhases().stream()).collect(Collectors.joining(", "));
168     }
169 }