1 package org.apache.maven.lifecycle;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 com.google.common.base.Preconditions;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.LinkedHashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37
38
39
40
41
42
43
44
45 @Component( role = DefaultLifecycles.class )
46 public class DefaultLifecycles
47 {
48 public static final String[] STANDARD_LIFECYCLES = { "default", "clean", "site" };
49
50
51
52 @Requirement( role = Lifecycle.class )
53 private Map<String, Lifecycle> lifecycles;
54
55 @Requirement
56 private Logger logger;
57
58 public DefaultLifecycles()
59 {
60 }
61
62 public DefaultLifecycles( Map<String, Lifecycle> lifecycles, Logger logger )
63 {
64 this.lifecycles = new LinkedHashMap<String, Lifecycle>();
65 this.logger = logger;
66 this.lifecycles = lifecycles;
67 }
68
69 public Lifecycle get( String key )
70 {
71 return getPhaseToLifecycleMap().get( key );
72 }
73
74
75
76
77
78
79
80 public Map<String, Lifecycle> getPhaseToLifecycleMap()
81 {
82
83
84
85 HashMap<String, Lifecycle> phaseToLifecycleMap = new HashMap<String, Lifecycle>();
86
87 for ( Lifecycle lifecycle : getLifeCycles() )
88 {
89 if ( logger.isDebugEnabled() )
90 {
91 logger.debug( "Lifecycle " + lifecycle );
92 }
93
94 for ( String phase : lifecycle.getPhases() )
95 {
96
97 if ( !phaseToLifecycleMap.containsKey( phase ) )
98 {
99 phaseToLifecycleMap.put( phase, lifecycle );
100 }
101 else
102 {
103 Lifecycle original = phaseToLifecycleMap.get( phase );
104 logger.warn( "Duplicated lifecycle phase " + phase + ". Defined in " + original.getId()
105 + " but also in " + lifecycle.getId() );
106 }
107 }
108 }
109
110 return phaseToLifecycleMap;
111 }
112
113 public List<Lifecycle> getLifeCycles()
114 {
115
116 Map<String, Lifecycle> lifecycles = new LinkedHashMap<String, Lifecycle>( this.lifecycles );
117
118 LinkedHashSet<String> lifecycleNames = new LinkedHashSet<String>( Arrays.asList( STANDARD_LIFECYCLES ) );
119 lifecycleNames.addAll( lifecycles.keySet() );
120
121 ArrayList<Lifecycle> result = new ArrayList<Lifecycle>();
122 for ( String name : lifecycleNames )
123 {
124 Lifecycle lifecycle = lifecycles.get( name );
125 Preconditions.checkNotNull( "A lifecycle must have an id.", lifecycle.getId() );
126 result.add( lifecycle );
127 }
128
129 return result;
130 }
131
132 public String getLifecyclePhaseList()
133 {
134 Set<String> phases = new LinkedHashSet<String>();
135
136 for ( Lifecycle lifecycle : getLifeCycles() )
137 {
138 phases.addAll( lifecycle.getPhases() );
139 }
140
141 return StringUtils.join( phases.iterator(), ", " );
142 }
143
144 }