View Javadoc
1   package org.apache.maven.lifecycle.mapping;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import static java.util.function.Function.identity;
28  import static java.util.stream.Collectors.toMap;
29  
30  /**
31   * DefaultLifecycleMapping
32   */
33  public class DefaultLifecycleMapping
34      implements LifecycleMapping
35  {
36  
37      private List<Lifecycle> lifecycles;
38  
39      private Map<String, Lifecycle> lifecycleMap;
40  
41      /** @deprecated use lifecycles instead */
42      private Map<String, LifecyclePhase> phases;
43  
44      /**
45       * Default ctor for plexus compatibility: lifecycles are most commonly defined in Plexus XML, that does field
46       * injection. Still, for Plexus to be able to instantiate this class, default ctor is needed.
47       *
48       * @deprecated Should not be used in Java code.
49       */
50      @Deprecated
51      public DefaultLifecycleMapping()
52      {
53      }
54  
55      /**
56       * Ctor to be used in Java code/providers.
57       */
58      public DefaultLifecycleMapping( final List<Lifecycle> lifecycles )
59      {
60          this.lifecycleMap = Collections.unmodifiableMap(
61                  lifecycles.stream().collect( toMap( Lifecycle::getId, identity() ) )
62          );
63      }
64  
65      /**
66       * Plexus: Populates the lifecycle map from the injected list of lifecycle mappings (if not already done).
67       */
68      private void initLifecycleMap()
69      {
70          if ( lifecycleMap == null )
71          {
72              lifecycleMap = new HashMap<>();
73  
74              if ( lifecycles != null )
75              {
76                  for ( Lifecycle lifecycle : lifecycles )
77                  {
78                      lifecycleMap.put( lifecycle.getId(), lifecycle );
79                  }
80              }
81              else
82              {
83                  /*
84                   * NOTE: This is to provide a migration path for implementors of the legacy API which did not know about
85                   * getLifecycles().
86                   */
87  
88                  String[] lifecycleIds = { "default", "clean", "site" };
89  
90                  for ( String lifecycleId : lifecycleIds )
91                  {
92                      Map<String, LifecyclePhase> phases = getLifecyclePhases( lifecycleId );
93                      if ( phases != null )
94                      {
95                          Lifecycle lifecycle = new Lifecycle();
96  
97                          lifecycle.setId( lifecycleId );
98                          lifecycle.setLifecyclePhases( phases );
99  
100                         lifecycleMap.put( lifecycleId, lifecycle );
101                     }
102                 }
103             }
104         }
105     }
106 
107     @Override
108     public Map<String, Lifecycle> getLifecycles()
109     {
110         initLifecycleMap();
111 
112         return lifecycleMap;
113     }
114 
115     @Deprecated
116     @Override
117     public List<String> getOptionalMojos( String lifecycle )
118     {
119         return null;
120     }
121 
122     private Map<String, LifecyclePhase> getLifecyclePhases( String lifecycle )
123     {
124         initLifecycleMap();
125 
126         Lifecycle lifecycleMapping = lifecycleMap.get( lifecycle );
127 
128         if ( lifecycleMapping != null )
129         {
130             return lifecycleMapping.getLifecyclePhases();
131         }
132         else if ( "default".equals( lifecycle ) )
133         {
134             return phases;
135         }
136         else
137         {
138             return null;
139         }
140     }
141 
142     @Deprecated
143     @Override
144     public Map<String, String> getPhases( String lifecycle )
145     {
146         return LifecyclePhase.toLegacyMap( getLifecyclePhases( lifecycle ) );
147     }
148 }