View Javadoc
1   package org.apache.maven.lifecycle;
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 org.codehaus.plexus.component.annotations.Component;
23  import org.codehaus.plexus.component.annotations.Requirement;
24  import org.codehaus.plexus.logging.Logger;
25  import org.codehaus.plexus.util.StringUtils;
26  
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.HashMap;
30  import java.util.LinkedHashMap;
31  import java.util.LinkedHashSet;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Set;
35  
36  /**
37   * @since 3.0
38   * @author Jason van Zyl
39   * @author Kristian Rosenvold
40   */
41  // TODO The configuration for the lifecycle needs to be externalized so that I can use the annotations properly for the
42  // wiring and reference and external source for the lifecycle configuration.
43  @Component( role = DefaultLifecycles.class )
44  public class DefaultLifecycles
45  {
46      public static final String[] STANDARD_LIFECYCLES = { "default", "clean", "site" };
47  
48      // @Configuration(source="org/apache/maven/lifecycle/lifecycles.xml")
49  
50      @Requirement( role = Lifecycle.class )
51      private Map<String, Lifecycle> lifecycles;
52  
53      @Requirement
54      private Logger logger;
55  
56      public DefaultLifecycles()
57      {
58      }
59  
60      public DefaultLifecycles( Map<String, Lifecycle> lifecycles, Logger logger )
61      {
62          this.lifecycles = new LinkedHashMap<>();
63          this.logger = logger;
64          this.lifecycles = lifecycles;
65      }
66  
67      public Lifecycle get( String key )
68      {
69          return getPhaseToLifecycleMap().get( key );
70      }
71  
72      /**
73       * We use this to map all phases to the lifecycle that contains it. This is used so that a user can specify the
74       * phase they want to execute and we can easily determine what lifecycle we need to run.
75       *
76       * @return A map of lifecycles, indexed on id
77       */
78      public Map<String, Lifecycle> getPhaseToLifecycleMap()
79      {
80          // If people are going to make their own lifecycles then we need to tell people how to namespace them correctly
81          // so that they don't interfere with internally defined lifecycles.
82  
83          HashMap<String, Lifecycle> phaseToLifecycleMap = new HashMap<>();
84  
85          for ( Lifecycle lifecycle : getLifeCycles() )
86          {
87              if ( logger.isDebugEnabled() )
88              {
89                  logger.debug( "Lifecycle " + lifecycle );
90              }
91  
92              for ( String phase : lifecycle.getPhases() )
93              {
94                  // The first definition wins.
95                  if ( !phaseToLifecycleMap.containsKey( phase ) )
96                  {
97                      phaseToLifecycleMap.put( phase, lifecycle );
98                  }
99                  else
100                 {
101                     Lifecycle original = phaseToLifecycleMap.get( phase );
102                     logger.warn( "Duplicated lifecycle phase " + phase + ". Defined in " + original.getId()
103                         + " but also in " + lifecycle.getId() );
104                 }
105             }
106         }
107 
108         return phaseToLifecycleMap;
109     }
110 
111     public List<Lifecycle> getLifeCycles()
112     {
113         // ensure canonical order of standard lifecycles
114         Map<String, Lifecycle> lifecycles = new LinkedHashMap<>( this.lifecycles );
115 
116         LinkedHashSet<String> lifecycleNames = new LinkedHashSet<>( Arrays.asList( STANDARD_LIFECYCLES ) );
117         lifecycleNames.addAll( lifecycles.keySet() );
118 
119         ArrayList<Lifecycle> result = new ArrayList<>();
120         for ( String name : lifecycleNames )
121         {
122             Lifecycle lifecycle = lifecycles.get( name );
123             if ( lifecycle.getId() == null )
124             {
125                 throw new NullPointerException( "A lifecycle must have an id." );
126             }
127             result.add( lifecycle );
128         }
129 
130         return result;
131     }
132 
133     public String getLifecyclePhaseList()
134     {
135         Set<String> phases = new LinkedHashSet<>();
136 
137         for ( Lifecycle lifecycle : getLifeCycles() )
138         {
139             phases.addAll( lifecycle.getPhases() );
140         }
141 
142         return StringUtils.join( phases.iterator(), ", " );
143     }
144 
145 }