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