View Javadoc
1   package com.soebes.maven.extensions;
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.Comparator;
24  import java.util.LinkedList;
25  import java.util.List;
26  
27  import org.apache.maven.eventspy.AbstractEventSpy;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  
30  /**
31   * @author Karl Heinz Marbaise <a href="mailto:kama@soebes.de">kama@soebes.de</a>
32   */
33  class LifeCycleOrdering
34      extends AbstractEventSpy
35  {
36      private final List<String> predefinedPhases = new LinkedList<>();
37  
38      private void initializePredefinedPhases()
39      {
40          // We do that explicitly here, cause otherwise
41          // the clean life cycle and site life cycle are positioned
42          // at the end. Looks a bit strange. Technically it's
43          // not a problem.
44          initPredefinedFromTo( LifecyclePhase.PRE_CLEAN, LifecyclePhase.POST_CLEAN );
45          initPredefinedFromTo( LifecyclePhase.VALIDATE, LifecyclePhase.DEPLOY );
46          initPredefinedFromTo( LifecyclePhase.PRE_SITE, LifecyclePhase.SITE_DEPLOY );
47      }
48  
49      private void initPredefinedFromTo( LifecyclePhase from, LifecyclePhase to )
50      {
51          LifecyclePhase[] values = LifecyclePhase.values();
52  
53          for ( int item = from.ordinal(); item <= to.ordinal(); item++ )
54          {
55              predefinedPhases.add( values[item].id() );
56          }
57      }
58  
59      public LifeCycleOrdering()
60      {
61          initializePredefinedPhases();
62      }
63  
64      public List<String> getPredefinedPhases()
65      {
66          return predefinedPhases;
67      }
68  
69      protected void orderLifeCycleOnPreparedOrder( List<String> lifeCyclePhases )
70      {
71          // Sort the lifeCyclePhases based on the given in predefinedPhases.
72          Collections.sort( lifeCyclePhases, new Comparator<String>()
73          {
74              public int compare( String left, String right )
75              {
76                  return Integer.compare( predefinedPhases.indexOf( left ), predefinedPhases.indexOf( right ) );
77              }
78          } );
79      }
80  
81  }